Files
reverse-engineering-anything/scripts/record-cli-commands.mjs
T
gensokyo-devandgensokyo-dev b990eb496d perf(cli): load only the registrar a one-shot command needs
Every rea process imported and registered all 52 command registrars
(about 1,900 modules) before running any command. A one-shot command
now loads only the registrar that defines its top-level command.

- src/cli/cliCommandRegistrations.ts holds every registrar in one table,
  grouped by tool family, with its module loader and its call. The
  complete createCli registers the table with static imports, and the
  types check that each entry matches its module.
- src/cli/cliShell.ts owns what does not depend on which commands are
  registered: identity, globals, argument policy, completions and the
  registration builtins. The full and lazy paths share it.
- scripts/record-cli-commands.mjs records, after tsc, which registrar
  defines each top-level command, from the same compiled table. The
  bundle keeps the record as an external dist module.
- cli/cliLoader.ts selects the recorded registrar when a known top-level
  command runs. It loads the complete CLI for help, --schema, --llms,
  completions, builtins, unknown names ("did you mean"), or a missing
  record.
- Process-owned CLI resources register their closers when their module
  loads (cli/cliResources.ts), so the shell no longer imports
  JavaScript-analysis and guidance owners just to close them.

Measured on one pinned core (median of 5 warm runs, base main vs this
change):
- guidance: 1.95 s / 253 MB → 0.42 s / 101 MB
- list-cutter-sessions: 1.97 s → 0.49 s
- inspect-go-binary: 1.95 s → 0.95 s
- setup --dry-run: 2.40 s → 1.88 s
- analyze: 2.39 s → 1.78 s
- --help: 1.99 s → 2.04 s (complete CLI, as before)

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-14 20:25:11 +08:00

68 lines
2.4 KiB
JavaScript

import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { Cli } from "incur";
// Record which registrar defines each top-level CLI command. Run after `tsc`
// and before bundling, so the record comes from the same compiled registration
// table that one-shot commands load from.
const compiled = resolve(dirname(fileURLToPath(import.meta.url)), "../dist");
const read = (name) => import(pathToFileURL(join(compiled, name)).href);
const [
{ CLI_COMMAND_REGISTRATIONS, CLI_REGISTRATION_ORDER },
{ createCliShell },
{ createDirectAnalysis },
] = await Promise.all([
read("cli/cliCommandRegistrations.js"),
read("cli/cliShell.js"),
read("composition/directAnalysis.js"),
]);
// Registration is independent of the host account and user configuration.
const home = await mkdtemp(join(tmpdir(), "rea-cli-commands-"));
const recorded = {};
try {
const registrations = await Promise.all(
CLI_REGISTRATION_ORDER.map(async (id) => {
const registration = CLI_COMMAND_REGISTRATIONS[id];
return {
module: await registration.load(),
registration: {
...registration,
register(module, context) {
const before = new Set(Cli.toCommands.get(context.cli)?.keys());
registration.register(module, context);
for (const name of Cli.toCommands.get(context.cli)?.keys() ?? [])
if (!before.has(name)) recorded[name] = id;
},
},
};
}),
);
createCliShell({
selectedEnvironment: {
PATH: process.env.PATH,
HOME: home,
USERPROFILE: home,
XDG_CONFIG_HOME: join(home, "config"),
XDG_CACHE_HOME: join(home, "cache"),
XDG_STATE_HOME: join(home, "state"),
},
registrations,
createAnalysis: createDirectAnalysis,
});
} finally {
await rm(home, { recursive: true, force: true });
}
if (Object.keys(recorded).length === 0)
throw new Error("No CLI commands were registered");
const sorted = Object.fromEntries(
Object.entries(recorded).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)),
);
await writeFile(
join(compiled, "recordedCliCommandRegistrations.js"),
`/** Recorded from the compiled CLI registration table by scripts/record-cli-commands.mjs; do not edit. */\nexport const RECORDED_CLI_COMMAND_REGISTRATIONS = Object.freeze(${JSON.stringify(sorted)});\n`,
"utf8",
);