All files / src/cli/setup wizard.ts

90.56% Statements 48/53
77.77% Branches 21/27
100% Functions 2/2
90.56% Lines 48/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136          2x   2x 2x 2x 2x           2x       9x     9x 9x   9x   9x     9x 9x         9x   7x   2x 2x       2x           9x   2x 2x     6x 6x     1x 1x       9x 7x   7x 1x 1x     7x       7x 2x 1x   1x     9x             2x     2x 1x 1x 1x   1x       1x             2x           2x           2x         2x       2x    
/**
 * Main unified setup wizard orchestration.
 * Entry point for `gitlab-mcp setup` command.
 */
 
import * as p from "@clack/prompts";
import { SetupMode, SetupResult } from "./types";
import { runDiscovery, formatDiscoverySummary } from "./discovery";
import { runLocalSetupFlow } from "./flows/local-setup";
import { runServerSetupFlow } from "./flows/server-setup";
import { runConfigureExistingFlow } from "./flows/configure-existing";
 
/**
 * Run the unified setup wizard.
 * Performs environment discovery, then presents mode selection based on results.
 */
export async function runSetupWizard(options?: {
  /** Skip to specific mode (for alias commands) */
  mode?: SetupMode;
}): Promise<SetupResult> {
  p.intro("GitLab MCP Setup Wizard");
 
  // Phase 1: Discovery
  const spinner = p.spinner();
  spinner.start("Detecting environment...");
 
  const discovery = runDiscovery();
 
  spinner.stop("Environment detected");
 
  // Display discovery summary
  const summary = formatDiscoverySummary(discovery);
  p.log.info(summary);
 
  // Phase 2: Mode selection
  let mode: SetupMode;
 
  if (options?.mode) {
    // Mode specified via alias (init → local, docker init → server)
    mode = options.mode;
  } else {
    const selected = await selectMode(discovery);
    Iif (!selected) {
      p.outro("Setup cancelled.");
      return { success: false, error: "Cancelled" };
    }
    mode = selected;
  }
 
  // Phase 3: Execute selected flow
  let result: SetupResult;
 
  switch (mode) {
    case "configure-existing":
      result = await runConfigureExistingFlow(discovery);
      break;
 
    case "local":
      result = await runLocalSetupFlow(discovery);
      break;
 
    case "server":
      result = await runServerSetupFlow(discovery);
      break;
  }
 
  // Phase 4: Summary
  if (result.success) {
    const parts: string[] = ["Setup complete!"];
 
    if (result.configuredClients && result.configuredClients.length > 0) {
      parts.push(`Configured ${result.configuredClients.length} client(s).`);
      parts.push("Restart your MCP clients to apply changes.");
    }
 
    Iif (result.dockerConfig) {
      parts.push(`Docker container on port ${result.dockerConfig.port}.`);
    }
 
    p.outro(parts.join(" "));
  } else if (result.error !== "Cancelled") {
    p.outro(`Setup failed: ${result.error ?? "Unknown error"}`);
  } else {
    p.outro("Setup cancelled.");
  }
 
  return result;
}
 
/**
 * Present mode selection based on discovery results
 */
async function selectMode(discovery: ReturnType<typeof runDiscovery>): Promise<SetupMode | null> {
  const options: { value: SetupMode; label: string; hint?: string }[] = [];
 
  // If existing setup found, show configure-existing first
  if (discovery.summary.hasExistingSetup) {
    const parts: string[] = [];
    Eif (discovery.summary.configuredCount > 0) {
      parts.push(`${discovery.summary.configuredCount} client(s)`);
    }
    Iif (discovery.summary.containerExists) {
      parts.push("1 docker service");
    }
 
    options.push({
      value: "configure-existing",
      label: "Configure existing",
      hint: parts.join(", "),
    });
  }
 
  options.push({
    value: "local",
    label: "New local setup (stdio)",
    hint: "For AI IDE clients (Claude, Cursor, VS Code, etc.)",
  });
 
  options.push({
    value: "server",
    label: "New server setup (HTTP/SSE)",
    hint: "Docker-based for shared/team access",
  });
 
  const mode = await p.select({
    message: "What would you like to do?",
    options,
  });
 
  Iif (p.isCancel(mode)) {
    return null;
  }
 
  return mode;
}