import { createCli } from "./cli.js"; import { classifyInvocation } from "./classify.js"; import { openDesktopWithProject } from "./commands/open.js"; export interface RunCliOptions { cwd?: string; nodeArgv?: [string, string]; } export function createCliParseArgv(input: { argv: string[]; cwd: string; nodeArgv?: [string, string]; }): string[] | { kind: "open-project"; resolvedPath: string } { const program = createCli(); const knownCommands = new Set(program.commands.map((command) => command.name())); const invocation = classifyInvocation({ argv: input.argv, knownCommands, cwd: input.cwd, }); if (invocation.kind === "open-project") { return invocation; } const nodeArgv = input.nodeArgv ?? ["paseo", "paseo"]; const cliArgv = invocation.argv.length === 0 ? ["onboard"] : invocation.argv; return [...nodeArgv, ...cliArgv]; } export async function runCli(argv: string[], options: RunCliOptions = {}): Promise { const parseArgv = createCliParseArgv({ argv, cwd: options.cwd ?? process.cwd(), nodeArgv: options.nodeArgv, }); if (!Array.isArray(parseArgv)) { await openDesktopWithProject(parseArgv.resolvedPath); return typeof process.exitCode === "number" ? process.exitCode : 0; } const program = createCli(); await program.parseAsync(parseArgv, { from: "node" }); return typeof process.exitCode === "number" ? process.exitCode : 0; }