Fix desktop CLI passthrough tty handling (#791)
* Fix desktop CLI passthrough tty handling * Fix desktop daemon manager test drift * Update daemon stop test after main merge * test(cli): isolate npx cache per CLI test
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { resolvePassthroughCliEntrypoint } from "./entrypoints.js";
|
||||
|
||||
const DESKTOP_CLI_ENV = "PASEO_DESKTOP_CLI";
|
||||
const IGNORED_ARG_PREFIXES = ["-psn_", "--no-sandbox"];
|
||||
|
||||
export type PassthroughCliRunner = (argv: string[]) => Promise<number>;
|
||||
|
||||
export function parsePassthroughCliArgs(input: {
|
||||
argv: string[];
|
||||
isDefaultApp: boolean;
|
||||
forceCli: boolean;
|
||||
}): string[] | null {
|
||||
const startIndex = input.isDefaultApp ? 2 : 1;
|
||||
const effective: string[] = [];
|
||||
|
||||
for (const arg of input.argv.slice(startIndex)) {
|
||||
if (IGNORED_ARG_PREFIXES.some((prefix) => arg.startsWith(prefix))) {
|
||||
continue;
|
||||
}
|
||||
effective.push(arg);
|
||||
}
|
||||
|
||||
if (input.forceCli) {
|
||||
return effective;
|
||||
}
|
||||
|
||||
return effective.length > 0 ? effective : null;
|
||||
}
|
||||
|
||||
export function parsePassthroughCliArgsFromArgv(argv: string[]): string[] | null {
|
||||
return parsePassthroughCliArgs({
|
||||
argv,
|
||||
isDefaultApp: process.defaultApp,
|
||||
forceCli: process.env[DESKTOP_CLI_ENV] === "1",
|
||||
});
|
||||
}
|
||||
|
||||
async function importPassthroughCliRunner(): Promise<PassthroughCliRunner> {
|
||||
const entrypoint = resolvePassthroughCliEntrypoint();
|
||||
const imported = (await import(pathToFileURL(entrypoint).href)) as {
|
||||
runCli?: unknown;
|
||||
};
|
||||
if (typeof imported.runCli !== "function") {
|
||||
throw new Error(`Passthrough CLI entrypoint did not export runCli: ${entrypoint}`);
|
||||
}
|
||||
return imported.runCli as PassthroughCliRunner;
|
||||
}
|
||||
|
||||
export async function runPassthroughCli(
|
||||
args: string[],
|
||||
options: { runCli?: PassthroughCliRunner } = {},
|
||||
): Promise<number> {
|
||||
const runCli = options.runCli ?? (await importPassthroughCliRunner());
|
||||
return await runCli(args);
|
||||
}
|
||||
Reference in New Issue
Block a user