Files
openclaw/scripts/lib/cli-startup-root-help-bundle.ts
clawsweeper[bot] c3d17e84f4 fix(build): select the root-help bundle that exports outputRootHelp (#119590)
Co-authored-by: clawsweeper <274271284+clawsweeper[bot]@users.noreply.github.com>
2026-08-05 21:10:48 +08:00

48 lines
1.7 KiB
TypeScript

// Resolves the generated root-help bundle identity for CLI startup metadata caching.
import { createHash } from "node:crypto";
import { readdirSync, readFileSync } from "node:fs";
import path from "node:path";
const OUTPUT_ROOT_HELP_EXPORT_RE =
/\bexport\s+(?:(?:async\s+)?function|class|const|let|var)\s+outputRootHelp\b|\bexport\s*\{[^}]*\boutputRootHelp\s*(?=[,}])/u;
export function resolveCliStartupRootHelpBundleIdentity(
distDir: string,
): { bundleName: string; signature: string } | null {
for (const bundleName of readdirSync(distDir).toSorted()) {
if (!bundleName.startsWith("root-help-") || !bundleName.endsWith(".js")) {
continue;
}
const bundleContents = readFileSync(path.join(distDir, bundleName), "utf8");
// The build emits multiple root-help chunks; only the renderer owns this cache identity.
// Selecting a helper chunk forces metadata generation onto its expensive source fallback.
if (!OUTPUT_ROOT_HELP_EXPORT_RE.test(bundleContents)) {
continue;
}
const buildInfo = readBuildIdentity(distDir);
return {
bundleName,
signature: createHash("sha1")
.update(bundleContents)
.update(JSON.stringify(buildInfo))
.digest("hex"),
};
}
return null;
}
function readBuildIdentity(distDir: string): { version: string | null; commit: string | null } {
try {
const parsed = JSON.parse(readFileSync(path.join(distDir, "build-info.json"), "utf8")) as {
commit?: unknown;
version?: unknown;
};
return {
version: typeof parsed.version === "string" ? parsed.version : null,
commit: typeof parsed.commit === "string" ? parsed.commit : null,
};
} catch {
return { version: null, commit: null };
}
}