Files
paseo/packages/cli/src/run.ts
T
Mohamed Boudra 264276880e fix(daemon): keep lifecycle operations on the selected instance
Restart reconstructed launch inputs and address fallback could redirect a home-selected shutdown to another daemon. Let supervisors publish bound endpoints, retain their worker restart contract, and share captured local lifecycle ownership between CLI and Desktop.
2026-09-09 14:09:53 +02:00

65 lines
1.9 KiB
TypeScript

import { renderError, toCommandError, defaultOutputOptions } from "./output/render.js";
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 isOnboardRootFlag = invocation.argv[0] === "--relay" || invocation.argv[0] === "--no-relay";
let cliArgv = invocation.argv;
if (invocation.argv.length === 0) {
cliArgv = ["onboard"];
} else if (isOnboardRootFlag) {
cliArgv = ["onboard", ...invocation.argv];
}
return [...nodeArgv, ...cliArgv];
}
export async function runCli(argv: string[], options: RunCliOptions = {}): Promise<number> {
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();
try {
await program.parseAsync(parseArgv, { from: "node" });
} catch (error) {
process.stderr.write(
renderError(toCommandError(error), {
...defaultOutputOptions,
format: argv.includes("--json") ? "json" : "table",
}) + "\n",
);
return 1;
}
return typeof process.exitCode === "number" ? process.exitCode : 0;
}