|
| 1 | +/** |
| 2 | + * `bootcamp completion <shell>` command. |
| 3 | + * |
| 4 | + * Prints a shell completion script (bash/zsh/fish) to stdout, derived from the |
| 5 | + * live Commander program so it always matches the real command surface. This |
| 6 | + * module handles validation and process wiring; the script generation lives in |
| 7 | + * `src/completion.ts`. Dependencies are injectable for unit testing. |
| 8 | + */ |
| 9 | + |
| 10 | +import chalk from "chalk"; |
| 11 | +import type { Command } from "commander"; |
| 12 | + |
| 13 | +import { |
| 14 | + collectCompletionSpec, |
| 15 | + isSupportedShell, |
| 16 | + renderCompletion, |
| 17 | + SUPPORTED_SHELLS, |
| 18 | +} from "../completion.js"; |
| 19 | + |
| 20 | +export interface CompletionCommandOptions { |
| 21 | + shell?: string; |
| 22 | +} |
| 23 | + |
| 24 | +export interface CompletionCommandDeps { |
| 25 | + log?: (message: string) => void; |
| 26 | + error?: (message: string) => void; |
| 27 | + exit?: (code: number) => void; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Run the `completion` command against a configured `program`. |
| 32 | + * |
| 33 | + * Prints the requested shell's completion script to stdout. On an unknown or |
| 34 | + * missing shell, writes an error to stderr and exits non-zero. |
| 35 | + */ |
| 36 | +export function runCompletionCommand( |
| 37 | + program: Command, |
| 38 | + options: CompletionCommandOptions, |
| 39 | + deps: CompletionCommandDeps = {} |
| 40 | +): void { |
| 41 | + const log = deps.log ?? ((msg: string) => console.log(msg)); |
| 42 | + const error = deps.error ?? ((msg: string) => console.error(msg)); |
| 43 | + const exit = deps.exit ?? ((code: number) => process.exit(code)); |
| 44 | + |
| 45 | + const shell = (options.shell ?? "").toLowerCase(); |
| 46 | + |
| 47 | + if (!shell) { |
| 48 | + error(chalk.red(`Missing shell. Choose one of: ${SUPPORTED_SHELLS.join(", ")}`)); |
| 49 | + exit(1); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (!isSupportedShell(shell)) { |
| 54 | + error( |
| 55 | + chalk.red(`Unsupported shell: ${shell}.`) + |
| 56 | + chalk.dim(` Choose one of: ${SUPPORTED_SHELLS.join(", ")}`) |
| 57 | + ); |
| 58 | + exit(1); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const spec = collectCompletionSpec(program); |
| 63 | + log(renderCompletion(shell, spec)); |
| 64 | +} |
0 commit comments