Files
pikasTech-unidesk/scripts/cli-light-import-boundary-contract-test.ts
T
2026-06-11 03:32:51 +08:00

56 lines
2.4 KiB
TypeScript

import { readFileSync } from "node:fs";
function assertCondition(condition: unknown, message: string, detail: unknown = {}): void {
if (!condition) throw new Error(`${message}: ${JSON.stringify(detail)}`);
}
function source(path: string): string {
return readFileSync(path, "utf8");
}
function staticImports(text: string): string[] {
return [...text.matchAll(/^import\s+(?:type\s+)?(?:[\s\S]*?)\s+from\s+["']([^"']+)["'];/gm)].map((match) => match[1] ?? "");
}
const cli = source("scripts/cli.ts");
const help = source("scripts/src/help.ts");
const cliStaticImports = staticImports(cli);
const helpStaticImports = staticImports(help);
const forbiddenCliStaticImports = ["./src/e2e", "./src/hwlab-cd", "./src/hwlab-g14", "./src/hwlab-node", "./src/agentrun", "./src/platform-infra"];
const forbiddenHelpStaticImports = ["./hwlab-cd", "./hwlab-g14", "./hwlab-node", "./agentrun", "./platform-infra"];
for (const modulePath of forbiddenCliStaticImports) {
assertCondition(!cliStaticImports.includes(modulePath), "ordinary CLI entry must not statically import heavy command modules", { modulePath, cliStaticImports });
}
for (const modulePath of forbiddenHelpStaticImports) {
assertCondition(!helpStaticImports.includes(modulePath), "generic help module must not statically import heavy command modules", { modulePath, helpStaticImports });
}
for (const modulePath of forbiddenCliStaticImports) {
assertCondition(cli.includes(`await import("${modulePath}")`), "heavy CLI command module must still be loaded on demand", { modulePath });
}
for (const modulePath of forbiddenHelpStaticImports) {
assertCondition(help.includes(`await import("${modulePath}")`), "heavy help module must still be loaded on demand", { modulePath });
}
const ghBranchIndex = cli.indexOf('if (top === "gh")');
const configReadIndex = cli.indexOf("const config = readConfig();");
assertCondition(ghBranchIndex !== -1 && configReadIndex !== -1 && ghBranchIndex < configReadIndex, "gh commands must dispatch before generic config/runtime initialization", {
ghBranchIndex,
configReadIndex,
});
console.log(JSON.stringify({
ok: true,
checks: [
"cli static imports exclude e2e/HWLAB/G14/AgentRun/platform-infra modules",
"help static imports exclude HWLAB/G14/AgentRun/platform-infra modules",
"heavy command modules remain available through dynamic imports",
"gh dispatch remains before generic config initialization",
],
}));