import { resolve } from "node:path"; import { stat } from "node:fs/promises"; import { Command } from "commander"; import { initCommand } from "./commands/init.js"; import { upgradeCommand } from "./commands/upgrade.js"; import { doctorCommand } from "./commands/doctor.js"; import { checkUpdatesCommand } from "./commands/check-updates.js"; import { statusCommand } from "./commands/status.js"; import { implementCommand } from "./commands/implement.js"; import { resetCommand } from "./commands/reset.js"; import { watchCommand } from "./commands/watch.js"; import { runCommand } from "./commands/run.js"; import { setVerbose, setQuiet } from "./utils/logger.js"; import { getPackageVersion } from "./installer.js"; import { isEnoent } from "./utils/errors.js"; const program = new Command(); program .name("bmalph") .description("BMAD-METHOD + Ralph integration — structured planning to autonomous implementation") .version(await getPackageVersion()) .option("--verbose", "Enable debug logging") .option("--no-color", "Disable colored output") .option("--quiet", "Suppress non-essential output") .option("-C, --project-dir ", "Run in specified directory") .hook("preAction", () => { const opts = program.opts(); if (opts.verbose) { setVerbose(true); } if (opts.quiet) { setQuiet(true); } if (opts.color === false) { process.env.FORCE_COLOR = "0"; } }); function resolveProjectDir(): string { const dir = program.opts().projectDir as string | undefined; return dir ? resolve(dir) : process.cwd(); } async function resolveAndValidateProjectDir(): Promise { const dir = resolveProjectDir(); try { const stats = await stat(dir); if (!stats.isDirectory()) { throw new Error(`Path is not a directory: ${dir}`); } } catch (err) { if (isEnoent(err)) { throw new Error(`Project directory not found: ${dir}`, { cause: err }); } throw err; } return dir; } program .command("init") .description("Initialize bmalph in the current project") .option("-n, --name ", "Project name") .option("-d, --description ", "Project description") .option( "--platform ", "Target platform (claude-code, codex, opencode, cursor, windsurf, copilot, aider)" ) .option("--dry-run", "Preview changes without writing files") .action( async (opts: { name?: string; description?: string; platform?: string; dryRun?: boolean }) => initCommand({ ...opts, projectDir: await resolveAndValidateProjectDir() }) ); program .command("upgrade") .description("Update bundled assets to current version") .option("--dry-run", "Preview changes without writing files") .option("--force", "Skip confirmation prompts") .action(async (opts: { dryRun?: boolean; force?: boolean }) => upgradeCommand({ ...opts, projectDir: await resolveAndValidateProjectDir() }) ); program .command("doctor") .description("Check installation health") .option("--json", "Output as JSON") .action(async (opts: { json?: boolean }) => doctorCommand({ ...opts, projectDir: await resolveAndValidateProjectDir() }) ); program .command("check-updates") .description("Check if bundled BMAD version is up to date with upstream") .option("--json", "Output as JSON") .action(checkUpdatesCommand); program .command("status") .description("Show current project status and phase") .option("--json", "Output as JSON") .action(async (opts: { json?: boolean }) => statusCommand({ ...opts, projectDir: await resolveAndValidateProjectDir() }) ); program .command("implement") .description("Transition BMAD planning artifacts to Ralph implementation format") .option("--force", "Override pre-flight validation errors") .action(async (opts: { force?: boolean }) => implementCommand({ ...opts, projectDir: await resolveAndValidateProjectDir() }) ); program .command("reset") .description("Remove all bmalph files from the project") .option("--dry-run", "Preview changes without removing files") .option("--force", "Skip confirmation prompt") .action(async (opts: { dryRun?: boolean; force?: boolean }) => resetCommand({ ...opts, projectDir: await resolveAndValidateProjectDir() }) ); program .command("watch") .description("[deprecated] Use 'bmalph run' instead") .option("--interval ", "Refresh interval in milliseconds (default: 2000)") .action(async (opts: { interval?: string }) => watchCommand({ ...opts, projectDir: await resolveAndValidateProjectDir() }) ); program .command("run") .description("Start Ralph loop with live dashboard") .option( "--driver ", "Override platform driver (claude-code, codex, opencode, copilot, cursor)" ) .option("--interval ", "Dashboard refresh interval in milliseconds (default: 2000)") .option("--no-dashboard", "Run Ralph without the dashboard overlay") .option("--review [mode]", "Quality review: enhanced (~10-14% tokens) or ultimate (~20-30%)") .option("--no-review", "Disable code review") .option("--swarm [count]", "Run N parallel workers in git worktrees (default: 2, max: 6)") .action( async (opts: { driver?: string; interval?: string; dashboard: boolean; review?: boolean | string; swarm?: boolean | string; }) => runCommand({ ...opts, projectDir: await resolveAndValidateProjectDir() }) ); void program.parseAsync();