Files
cnc_wams/wasm-port/tools/update_sim_config_boundary_hashes.mjs
2026-07-10 21:26:42 -04:00

82 lines
2.4 KiB
JavaScript
Executable File

#!/usr/bin/env node
import { createHash } from "node:crypto";
import { readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";
import {
projectDisplayPaths,
} from "../runtime/sdk/src/project-config.js";
import {
createProjectNodePaths,
} from "../runtime/sdk/src/project-node-paths.js";
const projectPaths = createProjectNodePaths();
const displayPaths = projectDisplayPaths();
const mode = process.argv.includes("--write") ? "write" : "check";
const artifacts = [
{
displayPath: displayPaths.boundarySummary,
sourceKey: "boundarySummary",
filePath: resolve(projectPaths.wasmPortRoot, "build/wasm/sim-configs-inventory/boundary-summary.tsv"),
},
{
displayPath: displayPaths.iniBoundarySummary,
sourceKey: "iniBoundarySummary",
filePath: resolve(projectPaths.wasmPortRoot, "build/wasm/sim-configs-inventory/ini-boundary-summary.tsv"),
},
];
const targetFiles = [
resolve(projectPaths.wasmPortRoot, "runtime/sdk/src/linuxcnc-hal.js"),
resolve(projectPaths.wasmPortRoot, "runtime/sdk/src/project-release-readiness.js"),
];
function escapeRegExp(text) {
return String(text).replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
function sha256(path) {
return createHash("sha256")
.update(readFileSync(path))
.digest("hex");
}
const hashes = artifacts.map((artifact) => ({
...artifact,
hash: sha256(artifact.filePath),
}));
function replaceHashBlock(text) {
let nextText = text;
for (const { sourceKey, hash } of hashes) {
const pattern = new RegExp(
`(\\[(?:PROJECT_DISPLAY_PATHS|DEFAULT_PROJECT_DISPLAY_PATHS)\\.${escapeRegExp(sourceKey)}\\]:\\s*\\n\\s*")[a-f0-9]{64}(")`,
"g",
);
nextText = nextText.replace(pattern, `$1${hash}$2`);
}
return nextText;
}
const staleFiles = [];
for (const targetFile of targetFiles) {
const text = readFileSync(targetFile, "utf8");
const nextText = replaceHashBlock(text);
if (nextText !== text) {
staleFiles.push(targetFile);
if (mode === "write") {
writeFileSync(targetFile, nextText);
}
}
}
if (staleFiles.length > 0 && mode === "check") {
console.error(`sim_config_boundary_hashes=stale files=${staleFiles.join(",")}`);
console.error("run: wasm-port/tools/update_sim_config_boundary_hashes.sh --write");
process.exit(1);
}
for (const { displayPath, hash } of hashes) {
console.log(`${displayPath} ${hash}`);
}
console.log(`sim_config_boundary_hashes=${staleFiles.length === 0 ? "ok" : "updated"}`);