Files
rea/src/cli/cliLoader.ts
T
morlutoandjianghh 6a5121363c perf(cli): answer a standalone --version without loading the command registry (#11312)
A plain `rea --version` reached loadCliRuntime with no command name, so
selectCliRegistration returned undefined and the complete CLI, with every
registrar and its schemas, loaded only for Incur to print the version.
selectCliRegistrations now returns the registrars an invocation needs:
none for a standalone --version, one for a recorded command, and
undefined when the complete CLI must answer. Combined flags, help and
shell completion keep the complete CLI.

Ported from #4083 onto main's lazy registration loader.

Co-authored-by: jianghh <3232826884@qq.com>
2026-11-16 00:17:39 +08:00

105 lines
3.7 KiB
TypeScript

import {
cliCompletionShell,
splitIncurBuiltinArguments,
} from "../cliBuiltinArguments.js";
import { RECORDED_CLI_COMMAND_REGISTRATIONS } from "../recordedCliCommandRegistrations.js";
import {
CLI_COMMAND_REGISTRATIONS,
type CliCommandRegistrationId,
} from "./cliCommandRegistrations.js";
import type { CliResultOutput } from "./cliResultOutput.js";
import type { CliInstance } from "./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",
]);
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. A standalone `--version` needs none: Incur answers
* it from the CLI identity alone. Help, schemas, `--llms`, completions, other
* Incur builtins, unknown names ("did you mean"), and a missing or unusable
* recording all describe or search every command.
*/
export const selectCliRegistrations = (
arguments_: readonly string[],
environment: Readonly<NodeJS.ProcessEnv>,
recorded: Readonly<Record<string, string>> | undefined,
): readonly CliCommandRegistrationId[] | undefined => {
if (cliCompletionShell(environment) !== undefined) return undefined;
const { command, flags } = splitIncurBuiltinArguments(arguments_);
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) && !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 query loads
* none, and anything else loads the complete CLI.
*/
export const loadCliRuntime = async (
arguments_: readonly string[],
environment: NodeJS.ProcessEnv,
): Promise<CliRuntime> => {
const ids = selectCliRegistrations(
arguments_,
environment,
RECORDED_CLI_COMMAND_REGISTRATIONS,
);
if (ids === undefined) {
const { createCli, closeCliResources } = await import("../cli.js");
return {
createCli: (resultOutput) => createCli(environment, resultOutput),
closeCliResources,
};
}
const selected = ids.map((id) => CLI_COMMAND_REGISTRATIONS[id]);
const [modules, { createCliShell }, { closeCliResources }, analysis] =
await Promise.all([
Promise.all(selected.map((registration) => registration.load())),
import("./cliShell.js"),
import("./cliResources.js"),
selected.some((registration) => registration.analysis)
? import("../composition/directAnalysis.js")
: undefined,
]);
return {
createCli: (resultOutput) =>
createCliShell({
selectedEnvironment: environment,
resultOutput,
registrations: selected.map((registration, index) => ({
registration,
module: modules[index],
})),
...(analysis === undefined
? {}
: { createAnalysis: analysis.createDirectAnalysis }),
}),
closeCliResources,
};
};