Files
reverse-engineering-anything/scripts/lib/catalog-core.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

56 lines
1.9 KiB
JavaScript

import { createHash } from "node:crypto";
import { join } from "node:path";
import { pathToFileURL } from "node:url";
import canonicalize from "canonicalize";
const load = (root, path) => import(pathToFileURL(join(root, path)).href);
/** Sort tool/provider names alphabetically. */
export const names = (contracts) =>
contracts
.map(({ name }) => name)
.sort((left, right) => left.localeCompare(right));
/** Return the stable digest for a generated catalog projection. */
export const digest = (value) => {
const encoded = canonicalize(value);
if (encoded === undefined)
throw new TypeError("Catalog projection is not canonical JSON");
return createHash("sha256").update(encoded).digest("hex");
};
/** Throw when two name lists diverge. */
export const assertSameNames = (label, actual, expected) => {
const actualSet = new Set(actual);
const expectedSet = new Set(expected);
const missing = [...expectedSet].filter((name) => !actualSet.has(name));
const extra = [...actualSet].filter((name) => !expectedSet.has(name));
if (missing.length === 0 && extra.length === 0) return;
throw new Error(
`${label} drifted (missing: ${missing.join(", ") || "none"}; extra: ${extra.join(", ") || "none"})`,
);
};
const SOURCE_PATHS = {
packageMetadata: "dist/generatedPackageMetadata.js",
catalogIdentity: "dist/contracts/catalog/catalogIdentity.js",
cli: "dist/cli.js",
cliRegistry: "dist/cli/commandRegistry.js",
toolContracts: "dist/contracts/toolContracts.js",
toolFamilies: "dist/contracts/toolFamilies.js",
capabilityDeclarations: "dist/composition/capabilityDeclarations.js",
supportedClients: "dist/application/SupportedClients.js",
};
/** Load every compiled source module referenced by the catalog. */
export const loadSources = async (root) =>
Object.fromEntries(
await Promise.all(
Object.entries(SOURCE_PATHS).map(async ([key, path]) => [
key,
await load(root, path),
]),
),
);