/** * ───────────────────────────────────────────── * SPB Git — Personal Git Platform * ───────────────────────────────────────────── * Author : Simon-Pierre Boucher * Contact : contact@spboucher.ai * File : cli/commands/doctor.mjs * Purpose : `spbgit doctor` — diagnose config, git, server, token * License : MIT © Simon-Pierre Boucher * ───────────────────────────────────────────── */ import { existsSync } from 'node:fs'; import pc from 'picocolors'; import { loadCliConfig, CONFIG_PATH } from '../lib/config.mjs'; import { git } from '../lib/git.mjs'; import { EXIT } from '../lib/api.mjs'; export function registerDoctor(program) { program .command('doctor') .description('diagnose the local setup end to end') .action(async () => { let failures = 0; const check = (ok, label, hint) => { console.log(`${ok ? pc.green('✓') : pc.red('✗')} ${label}${ok || !hint ? '' : pc.dim(` — ${hint}`)}`); if (!ok) failures += 1; }; const config = loadCliConfig(); check(Boolean(config), `config present (${CONFIG_PATH})`, 'run: spbgit init'); const gitVersion = await git(process.cwd(), ['--version']); check(gitVersion.ok, `git installed ${gitVersion.ok ? pc.dim(gitVersion.stdout.trim()) : ''}`, 'install git'); if (config) { check(existsSync(config.workspace), `workspace exists (${config.workspace})`, 'run: spbgit init'); let health = null; try { const response = await fetch(`${config.server}/healthz`); health = response.ok ? await response.json() : null; } catch { health = null; } check(Boolean(health), `server reachable (${config.server})`, 'check network / server process'); if (health) { console.log(pc.dim(` uptime ${health.uptimeSeconds}s · ${health.repos} repos · cache ${health.cache.files} files`)); } if (config.token) { let who = null; try { const response = await fetch(`${config.server}/api/v1/whoami`, { headers: { Authorization: `Bearer ${config.token}` }, }); who = response.ok ? await response.json() : null; } catch { who = null; } check(Boolean(who), `token valid${who ? pc.dim(` (label: ${who.tokenLabel})`) : ''}`, 'run: spbgit token new, then spbgit init'); } else { check(false, 'token configured', 'run: spbgit init'); } } console.log(''); if (failures === 0) console.log(pc.green('All checks passed — you are ready to push.')); else { console.log(pc.red(`${failures} check(s) failed.`)); process.exit(EXIT.NETWORK); } }); }