Files
openclaw/scripts/build-diffs-viewer-runtime.mjs
2026-05-27 19:30:27 +02:00

81 lines
2.1 KiB
JavaScript

#!/usr/bin/env node
import fs from "node:fs/promises";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { build } from "esbuild";
const modulePath = fileURLToPath(import.meta.url);
const repoRoot = path.resolve(path.dirname(modulePath), "..");
const targets = {
curated: {
entry: "extensions/diffs/src/viewer-client.ts",
output: "extensions/diffs/assets/viewer-runtime.js",
shikiAlias: "scripts/diffs-shiki-curated.ts",
},
full: {
entry: "extensions/diffs/src/viewer-client.ts",
output: "extensions/diffs-language-pack/assets/viewer-runtime.js",
},
};
export async function buildDiffsViewerRuntime(targetName) {
const target = targets[targetName];
if (!target) {
throw new Error(
`Usage: node scripts/build-diffs-viewer-runtime.mjs ${Object.keys(targets).join("|")}`,
);
}
const outputPath = path.join(repoRoot, target.output);
await fs.mkdir(path.dirname(outputPath), { recursive: true });
const result = await build({
entryPoints: [path.join(repoRoot, target.entry)],
bundle: true,
platform: "browser",
target: "es2020",
format: "esm",
minify: true,
legalComments: "none",
outfile: outputPath,
write: false,
plugins: target.shikiAlias
? [
{
name: "openclaw-diffs-curated-shiki",
setup(buildContext) {
buildContext.onResolve({ filter: /^shiki$/ }, () => ({
path: path.join(repoRoot, target.shikiAlias),
}));
},
},
]
: [],
});
const outputFile = result.outputFiles?.[0];
if (!outputFile) {
throw new Error(`esbuild did not produce ${target.output}`);
}
const runtime = outputFile.text.replace(/[ \t]+$/gm, "");
let previousRuntime = null;
try {
previousRuntime = await fs.readFile(outputPath, "utf8");
} catch (error) {
if (error?.code !== "ENOENT") {
throw error;
}
}
if (previousRuntime !== runtime) {
await fs.writeFile(outputPath, runtime);
}
}
if (process.argv[1] === modulePath) {
await buildDiffsViewerRuntime(process.argv[2]);
}