返回 CodeWhale
build-desktop-media.mjs
根目录 / web / scripts / build-desktop-media.mjs
1 #!/usr/bin/env node
2 import { createHash } from "node:crypto";
3 import { existsSync, readFileSync, readdirSync, writeFileSync, mkdirSync } from "node:fs";
4 import path from "node:path";
5 import { fileURLToPath } from "node:url";
6
7 const webRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
8 const manifestPath = path.join(webRoot, "data", "desktop-media.json");
9 const THEMES = ["light", "dark"];
10 const FAKE = /scripted|website-demo|simulator|fixture|loopback/i;
11
12 const args = process.argv.slice(2);
13 const manifest = JSON.parse(readFileSync(manifestPath, "utf8"));
14 const outDir = path.join(webRoot, manifest.outDir);
15 const sha = (bytes) => createHash("sha256").update(bytes).digest("hex");
16 const fileName = (shot, theme, width, format) => `${shot.id}-${theme}-${width}.${format}`;
17
18 function expectedFiles() {
19 return manifest.shots.flatMap((shot) =>
20 THEMES.flatMap((theme) => manifest.widths.flatMap((width) => manifest.formats.map((format) => fileName(shot, theme, width, format)))),
21 );
22 }
23
24 function check() {
25 const errors = [];
26 if (manifest.capture.simulator !== false) errors.push("capture.simulator must be false");
27 for (const key of ["model", "provider", "engine", "app"]) {
28 if (FAKE.test(String(manifest.capture[key] ?? ""))) errors.push(`capture.${key} names a simulated or scripted source`);
29 }
30 const wanted = new Set(expectedFiles());
31 for (const name of wanted) {
32 const record = manifest.outputs[name];
33 const file = path.join(outDir, name);
34 if (!record) errors.push(`${name}: not recorded; run with --source`);
35 else if (!existsSync(file)) errors.push(`${name}: missing from ${manifest.outDir}`);
36 else {
37 const bytes = readFileSync(file);
38 if (sha(bytes) !== record.sha256) errors.push(`${name}: bytes differ from the recorded SHA-256`);
39 if (bytes.length > manifest.budgetBytes) errors.push(`${name}: ${bytes.length} bytes is over the ${manifest.budgetBytes} budget`);
40 }
41 }
42 for (const name of Object.keys(manifest.outputs)) if (!wanted.has(name)) errors.push(`${name}: recorded but no shot produces it`);
43 if (existsSync(outDir)) {
44 for (const name of readdirSync(outDir)) if (!wanted.has(name)) errors.push(`${name}: stray file in ${manifest.outDir}`);
45 }
46 if (errors.length) {
47 console.error(`desktop media check failed:\n- ${errors.join("\n- ")}`);
48 process.exit(1);
49 }
50 console.log(`desktop media is current (${wanted.size} files)`);
51 }
52
53 async function build(source) {
54 const { default: sharp } = await import("sharp");
55 mkdirSync(outDir, { recursive: true });
56 const outputs = {};
57 for (const shot of manifest.shots) {
58 for (const theme of THEMES) {
59 const input = path.join(source, `${shot.source}-${theme}.png`);
60 if (!existsSync(input)) throw new Error(`missing source ${input}`);
61 for (const width of manifest.widths) {
62 for (const format of manifest.formats) {
63 let pipeline = sharp(input);
64 if (shot.crop) pipeline = pipeline.extract(shot.crop);
65 pipeline = pipeline.resize({ width, withoutEnlargement: true });
66 pipeline = format === "avif" ? pipeline.avif({ quality: 58, effort: 6 }) : pipeline.webp({ quality: 80, effort: 6 });
67 const { data, info } = await pipeline.toBuffer({ resolveWithObject: true });
68 const name = fileName(shot, theme, width, format);
69 writeFileSync(path.join(outDir, name), data);
70 outputs[name] = { width: info.width, height: info.height, bytes: data.length, sha256: sha(data) };
71 }
72 }
73 }
74 }
75 manifest.outputs = outputs;
76 writeFileSync(manifestPath, `${JSON.stringify(manifest, null, 2)}\n`);
77 check();
78 }
79
80 if (args.includes("--source")) await build(path.resolve(args[args.indexOf("--source") + 1]));
81 else if (args.includes("--check") || args.length === 0) check();
82 else {
83 console.error("usage: build-desktop-media.mjs --source <dir> | --check");
84 process.exit(2);
85 }
86
86 lines Plain Text