Files
reverse-engineering-anything/scripts/record-cli-commands.mjs
T
Ysopwnableandgensokyo-dev 4ea24b6f8e refactor(cli): derive build catalogs from the REA command registry
Read CLI catalogs and registrar ownership from the REA command records, type-check the catalog helper and consolidate registrar types. Preserve the generated catalogs, mapping and CLI parity.

Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-16 18:57:12 +08:00

71 lines
2.5 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";
// 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 },
{ cliCommands },
] = await Promise.all([
read("cli/cliCommandRegistrations.js"),
read("cli/cliShell.js"),
read("composition/directAnalysis.js"),
read("cli/commandRegistry.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(
cliCommands(context.cli).map(({ name }) => name),
);
registration.register(module, context);
for (const { name } of cliCommands(context.cli))
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",
);