|
| 1 | +/** |
| 2 | + * Init command — interactive project onboarding. |
| 3 | + * |
| 4 | + * Usage: |
| 5 | + * toban init |
| 6 | + * |
| 7 | + * Creates `.toban/config.json` in the current directory with API credentials |
| 8 | + * and workspace configuration. |
| 9 | + */ |
| 10 | + |
| 11 | +import * as p from "@clack/prompts"; |
| 12 | +import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs"; |
| 13 | +import { join } from "node:path"; |
| 14 | +import { createApiClient, type WorkspaceInfo } from "../api-client.js"; |
| 15 | + |
| 16 | +// --------------------------------------------------------------------------- |
| 17 | +// Config types |
| 18 | +// --------------------------------------------------------------------------- |
| 19 | + |
| 20 | +export interface TobanConfig { |
| 21 | + api_url: string; |
| 22 | + api_key: string; |
| 23 | + workspace_id: string; |
| 24 | + project_name: string; |
| 25 | + created_at: string; |
| 26 | +} |
| 27 | + |
| 28 | +const CONFIG_DIR = ".toban"; |
| 29 | +const CONFIG_FILE = "config.json"; |
| 30 | + |
| 31 | +// --------------------------------------------------------------------------- |
| 32 | +// Helpers |
| 33 | +// --------------------------------------------------------------------------- |
| 34 | + |
| 35 | +function configPath(cwd: string): string { |
| 36 | + return join(cwd, CONFIG_DIR, CONFIG_FILE); |
| 37 | +} |
| 38 | + |
| 39 | +export function loadConfig(cwd: string): TobanConfig | null { |
| 40 | + const path = configPath(cwd); |
| 41 | + if (!existsSync(path)) return null; |
| 42 | + try { |
| 43 | + return JSON.parse(readFileSync(path, "utf-8")) as TobanConfig; |
| 44 | + } catch { |
| 45 | + return null; |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +function isCancel(value: unknown): value is symbol { |
| 50 | + return p.isCancel(value); |
| 51 | +} |
| 52 | + |
| 53 | +async function validateApiKey(apiUrl: string, apiKey: string): Promise<WorkspaceInfo | null> { |
| 54 | + try { |
| 55 | + const api = createApiClient(apiUrl, apiKey); |
| 56 | + return await api.fetchWorkspace(); |
| 57 | + } catch { |
| 58 | + return null; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +// --------------------------------------------------------------------------- |
| 63 | +// Main |
| 64 | +// --------------------------------------------------------------------------- |
| 65 | + |
| 66 | +export async function handleInit(): Promise<void> { |
| 67 | + const cwd = process.cwd(); |
| 68 | + |
| 69 | + p.intro("toban init"); |
| 70 | + |
| 71 | + // Check for existing config |
| 72 | + const existing = loadConfig(cwd); |
| 73 | + if (existing) { |
| 74 | + const overwrite = await p.confirm({ |
| 75 | + message: `.toban/config.json already exists (workspace: ${existing.project_name}). Overwrite?`, |
| 76 | + initialValue: false, |
| 77 | + }); |
| 78 | + if (isCancel(overwrite) || !overwrite) { |
| 79 | + p.outro("Cancelled."); |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // 1. API URL |
| 85 | + const apiUrl = await p.text({ |
| 86 | + message: "API URL", |
| 87 | + placeholder: "https://api.toban.dev", |
| 88 | + defaultValue: "https://api.toban.dev", |
| 89 | + validate: (v) => { |
| 90 | + if (!v) return "API URL is required"; |
| 91 | + try { new URL(v); } catch { return "Invalid URL"; } |
| 92 | + }, |
| 93 | + }); |
| 94 | + if (isCancel(apiUrl)) { p.outro("Cancelled."); return; } |
| 95 | + |
| 96 | + // 2. API Key |
| 97 | + const apiKey = await p.text({ |
| 98 | + message: "API key", |
| 99 | + placeholder: "tb_xxx", |
| 100 | + validate: (v) => { |
| 101 | + if (!v) return "API key is required"; |
| 102 | + if (!v.startsWith("tb_")) return "API key must start with tb_"; |
| 103 | + }, |
| 104 | + }); |
| 105 | + if (isCancel(apiKey)) { p.outro("Cancelled."); return; } |
| 106 | + |
| 107 | + // 3. Validate key |
| 108 | + const spin = p.spinner(); |
| 109 | + spin.start("Validating API key..."); |
| 110 | + const workspace = await validateApiKey(apiUrl, apiKey); |
| 111 | + if (!workspace) { |
| 112 | + spin.stop("Validation failed"); |
| 113 | + p.log.error("Could not connect to the API. Check your API URL and key."); |
| 114 | + p.outro("Setup failed."); |
| 115 | + process.exit(1); |
| 116 | + } |
| 117 | + spin.stop(`Connected to workspace: ${workspace.name}`); |
| 118 | + |
| 119 | + // 4. Show workspace info |
| 120 | + const infoLines = [ |
| 121 | + `Name: ${workspace.name}`, |
| 122 | + `ID: ${workspace.id}`, |
| 123 | + ]; |
| 124 | + if (workspace.github_repo) { |
| 125 | + infoLines.push(`Repo: ${workspace.github_repo}`); |
| 126 | + } |
| 127 | + if (workspace.language) { |
| 128 | + infoLines.push(`Lang: ${workspace.language}`); |
| 129 | + } |
| 130 | + p.note(infoLines.join("\n"), "Workspace"); |
| 131 | + |
| 132 | + // 5. Project name |
| 133 | + const projectName = await p.text({ |
| 134 | + message: "Project name (for this directory)", |
| 135 | + defaultValue: workspace.name, |
| 136 | + placeholder: workspace.name, |
| 137 | + }); |
| 138 | + if (isCancel(projectName)) { p.outro("Cancelled."); return; } |
| 139 | + |
| 140 | + // 6. Save config |
| 141 | + const config: TobanConfig = { |
| 142 | + api_url: apiUrl, |
| 143 | + api_key: apiKey, |
| 144 | + workspace_id: workspace.id, |
| 145 | + project_name: projectName, |
| 146 | + created_at: new Date().toISOString(), |
| 147 | + }; |
| 148 | + |
| 149 | + const dir = join(cwd, CONFIG_DIR); |
| 150 | + mkdirSync(dir, { recursive: true }); |
| 151 | + writeFileSync(configPath(cwd), JSON.stringify(config, null, 2) + "\n"); |
| 152 | + |
| 153 | + p.log.success(`Config saved to ${CONFIG_DIR}/${CONFIG_FILE}`); |
| 154 | + |
| 155 | + // 7. Optionally create first sprint |
| 156 | + const createSprint = await p.confirm({ |
| 157 | + message: "Create and start the first sprint now?", |
| 158 | + initialValue: false, |
| 159 | + }); |
| 160 | + if (!isCancel(createSprint) && createSprint) { |
| 161 | + const api = createApiClient(apiUrl, apiKey); |
| 162 | + spin.start("Starting sprint..."); |
| 163 | + try { |
| 164 | + const result = await api.startSprint(); |
| 165 | + spin.stop(`Sprint #${result.sprint.number} started (${result.tasks.length} task(s))`); |
| 166 | + } catch (err) { |
| 167 | + spin.stop("Could not start sprint"); |
| 168 | + p.log.warning(`${err instanceof Error ? err.message : String(err)}`); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + p.outro("Done! Run `toban start` to begin."); |
| 173 | +} |
0 commit comments