File size: 2,692 Bytes
5448d8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env node
/**

 * Render every Mermaid source in docs/diagrams/*.mmd into docs/diagrams/exported/*.svg

 *

 * Usage:

 *   npm run docs:render-diagrams

 *

 * Requirements:

 *   - @mermaid-js/mermaid-cli (`mmdc`) on PATH or installed globally.

 *     `npm install -g @mermaid-js/mermaid-cli` if missing.

 *

 * Notes:

 *   - Puppeteer needs `--no-sandbox` on Ubuntu 23.10+ / WSL. A temp config file

 *     is written automatically.

 *   - Each diagram is rendered with `--backgroundColor white` so the SVG works

 *     against both light and dark themes.

 *   - The script exits non-zero on first failure so CI / pre-commit hooks can

 *     gate on it.

 */
import { spawnSync } from "node:child_process";
import { existsSync, mkdirSync, readdirSync, writeFileSync } from "node:fs";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { tmpdir } from "node:os";

const __dirname = dirname(fileURLToPath(import.meta.url));
const repoRoot = resolve(__dirname, "..", "..");
const srcDir = resolve(repoRoot, "docs", "diagrams");
const outDir = resolve(srcDir, "exported");

if (!existsSync(srcDir)) {
  console.error(`[render-diagrams] missing source dir: ${srcDir}`);
  process.exit(1);
}
if (!existsSync(outDir)) {
  mkdirSync(outDir, { recursive: true });
}

// Puppeteer needs --no-sandbox on many Linux distros (Ubuntu 23.10+, WSL).
const puppeteerConfigPath = join(tmpdir(), "omniroute-mmdc-puppeteer.json");
writeFileSync(
  puppeteerConfigPath,
  JSON.stringify({ args: ["--no-sandbox", "--disable-setuid-sandbox"] }, null, 2)
);

const sources = readdirSync(srcDir)
  .filter((f) => f.endsWith(".mmd"))
  .sort();

if (sources.length === 0) {
  console.error(`[render-diagrams] no .mmd files in ${srcDir}`);
  process.exit(1);
}

console.log(`[render-diagrams] rendering ${sources.length} diagram(s)`);
let failures = 0;
for (const src of sources) {
  const input = join(srcDir, src);
  const output = join(outDir, src.replace(/\.mmd$/, ".svg"));
  console.log(`  - ${src} -> ${output.replace(repoRoot + "/", "")}`);
  const result = spawnSync(
    "mmdc",
    [
      "-i",
      input,
      "-o",
      output,
      "--backgroundColor",
      "white",
      "--puppeteerConfigFile",
      puppeteerConfigPath,
    ],
    { stdio: "inherit" }
  );
  if (result.status !== 0) {
    console.error(`    [FAIL] ${src} (exit ${result.status})`);
    failures += 1;
  }
}

if (failures > 0) {
  console.error(`[render-diagrams] ${failures} failure(s)`);
  process.exit(1);
}
console.log(`[render-diagrams] all ${sources.length} diagram(s) rendered.`);