Files
reverse-engineering-anything/src/cliLoader.ts
T
Vincent Koc bb3bf139ea perf(cli): isolate JavaScript application command startup (#15209)
* perf(cli): isolate JavaScript application command startup

* fix(cli): finalize direct JavaScript analysis before returning
2026-12-01 19:31:28 +08:00

146 lines
5.2 KiB
TypeScript

import type { CliGlobalArguments } from "./cliBuiltinArguments.js";
import { RECORDED_CLI_COMMAND_REGISTRATIONS } from "./recordedCliCommandRegistrations.js";
import {
CLI_COMMAND_REGISTRATIONS,
type CliCommandRegistrationId,
} from "./cli/cliCommandRegistrations.js";
import type { CliResultOutput } from "./cli/cliResultOutput.js";
import type { CliInstance } from "./cli/types.js";
/** Global switches that change only how a command's result is written. */
const COMMAND_OUTPUT_FLAGS: ReadonlySet<string> = new Set([
"--json",
"--full-output",
"--token-count",
"--timings",
"--no-timings",
"--no-retain",
]);
/** Global switches that inspect a single known command without listing others. */
const COMMAND_DISCOVERY_FLAGS: ReadonlySet<string> = new Set([
"--help",
"-h",
"--schema",
"--llms",
"--llms-full",
]);
const isRegistrationId = (id: string): id is CliCommandRegistrationId =>
Object.hasOwn(CLI_COMMAND_REGISTRATIONS, id);
/**
* The registrars the command these arguments run needs, or undefined when the
* complete CLI must answer, as it does for shell completion, which has no
* global arguments. A standalone version or retired per-command MCP query
* needs no registrar. A recorded top-level command's `--help`, `-h`, `--schema`,
* `--llms`, or `--llms-full` only needs that command's registrar. Root help/
* manifests, unknown names ("did you mean"), other CLI builtins, and a missing
* or unusable recording all describe or search every command.
*/
const selectCliRegistrations = (
global: CliGlobalArguments | undefined,
recorded: Readonly<Record<string, string>> | undefined,
): readonly CliCommandRegistrationId[] | undefined => {
if (global === undefined) return undefined;
const { command, flags } = global;
if (flags.has("--mcp") && command.length > 0) return [];
if (command.length === 0 && flags.size === 1 && flags.has("--version"))
return [];
if (recorded === undefined) return undefined;
for (const flag of flags)
if (
!COMMAND_OUTPUT_FLAGS.has(flag) &&
!COMMAND_DISCOVERY_FLAGS.has(flag) &&
!flag.startsWith("--timings=")
)
return undefined;
const name = command[0];
if (name === undefined || !Object.hasOwn(recorded, name)) return undefined;
const id = recorded[name]!;
return isRegistrationId(id) ? [id] : undefined;
};
/** The command runtime one invocation loaded, before its CLI is built. */
export interface CliRuntime {
createCli(resultOutput: CliResultOutput | undefined): CliInstance;
/** Closes the process-owned resources of this runtime's commands. */
closeCliResources(): Promise<void>;
}
/**
* Load the command runtime for one invocation. A recorded top-level command
* loads only its registrar's module graph. A standalone version or retired
* mode query loads none; anything else loads the complete CLI.
*/
export const loadCliRuntime = async (
global: CliGlobalArguments | undefined,
environment: NodeJS.ProcessEnv,
savedDefaults: ReadonlySet<string> | undefined,
options: { readonly finalizeJavaScriptAnalysis?: boolean } = {},
): Promise<CliRuntime> => {
const ids = selectCliRegistrations(
global,
RECORDED_CLI_COMMAND_REGISTRATIONS,
);
// Load the JavaScript worker owner only for invocations that can run its
// registrars; other commands keep their recorded module graph.
const javascriptAnalysis =
options.finalizeJavaScriptAnalysis === true &&
(ids === undefined ||
ids.includes("coreAnalysis") ||
ids.includes("javaScriptApplication"))
? new (
await import("./cli/javascriptApplicationAnalysis.js")
).CliJavaScriptAnalysisHost("invocation")
: undefined;
const { createCliResourceCloser, closeCliResources: closeProcessResources } =
await import("./cli/cliResources.js");
const retainInvocationOwner = (close: () => Promise<void>) =>
createCliResourceCloser(
close,
...(javascriptAnalysis === undefined
? []
: [() => javascriptAnalysis.close()]),
);
if (ids === undefined) {
const { createCli, closeCliResources } = await import("./cli.js");
return {
createCli: (resultOutput) =>
createCli(
environment,
resultOutput,
undefined,
savedDefaults,
javascriptAnalysis,
),
closeCliResources: retainInvocationOwner(closeCliResources),
};
}
const selected = ids.map((id) => CLI_COMMAND_REGISTRATIONS[id]);
const [modules, { createCliShell }, analysis] = await Promise.all([
Promise.all(selected.map((registration) => registration.load())),
import("./cli/cliShell.js"),
selected.some((registration) => registration.analysis)
? import("./composition/directAnalysis.js")
: undefined,
]);
return {
createCli: (resultOutput) =>
createCliShell({
selectedEnvironment: environment,
savedDefaults,
resultOutput,
javascriptAnalysis,
registrations: selected.map((registration, index) => ({
registration,
module: modules[index],
})),
...(analysis === undefined
? {}
: { createAnalysis: analysis.createDirectAnalysis }),
}),
closeCliResources: retainInvocationOwner(closeProcessResources),
};
};