spb/spbgit Public MIT
SPB Git — the platform hosting itself
JavaScript 73.9%
CSS 11.7%
Nunjucks 11.6%
Shell 2.7%
1/**2 * ─────────────────────────────────────────────3 * SPB Git — Personal Git Platform4 * ─────────────────────────────────────────────5 * Author : Simon-Pierre Boucher6 * Contact : contact@spboucher.ai7 * File : cli/commands/init.mjs8 * Purpose : `spbgit init` — interactive setup wizard9 * License : MIT © Simon-Pierre Boucher10 * ─────────────────────────────────────────────11 */1213import { mkdirSync } from 'node:fs';14import pc from 'picocolors';15import { loadCliConfig, saveCliConfig, expandHome, CONFIG_PATH } from '../lib/config.mjs';16import { prompt } from '../lib/ui.mjs';17import { git } from '../lib/git.mjs';18import { api } from '../lib/api.mjs';1920export function registerInit(program) {21 program22 .command('init')23 .description('interactive setup: server URL, token, workspace directory')24 .action(async () => {25 const existing = loadCliConfig() ?? {};26 console.log(pc.bold('SPB Git CLI setup\n'));27 const server = await prompt('Server URL', existing.server ?? 'https://git.spboucher.ai');28 const token = await prompt('Personal access token (paste, hidden after save)', existing.token ? '(keep current)' : '');29 const workspace = expandHome(await prompt('Workspace directory', existing.workspace ?? '~/code'));3031 const config = {32 server: server.replace(/\/+$/, ''),33 token: token === '(keep current)' ? existing.token : token,34 workspace,35 author: existing.author ?? { name: 'Simon-Pierre Boucher', email: 'contact@spboucher.ai' },36 };37 mkdirSync(workspace, { recursive: true });38 saveCliConfig(config);39 console.log(`\n${pc.green('✓')} Config written to ${CONFIG_PATH} (mode 600)`);4041 // Ensure global git identity exists.42 const name = await git(process.cwd(), ['config', '--global', 'user.name']);43 if (!name.ok || !name.stdout.trim()) {44 await git(process.cwd(), ['config', '--global', 'user.name', config.author.name]);45 console.log(`${pc.green('✓')} git user.name set to ${config.author.name}`);46 }47 const email = await git(process.cwd(), ['config', '--global', 'user.email']);48 if (!email.ok || !email.stdout.trim()) {49 await git(process.cwd(), ['config', '--global', 'user.email', config.author.email]);50 console.log(`${pc.green('✓')} git user.email set to ${config.author.email}`);51 }5253 if (config.token) {54 try {55 const who = await api(config, 'GET', '/api/v1/whoami');56 console.log(`${pc.green('✓')} Token valid — authenticated as ${who.owner} (${who.tokenLabel})`);57 } catch (err) {58 console.log(`${pc.yellow('!')} Could not verify token: ${err.message}`);59 }60 } else {61 console.log(`${pc.yellow('!')} No token saved — read-only operations only. Get one with: spbgit token new`);62 }63 });64}65