generate-mermaid.mjs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Build-time generator: renders every site-graph fixture to `charts/<name>.md` with
  2. // its source and its graphs — the phase quotient (the GUI's primary picture, only for a
  3. // fixture that declares markers), the causality graph, the site graph, and the derived
  4. // actor summary — as mermaid diagrams, for live viewing (any
  5. // mermaid preview / GitHub). Runs on the BUILT package
  6. // (`../dist/index.js`) so it needs no TS tooling at runtime; `pnpm charts` builds
  7. // first. The `charts/` directory is a generated artifact (gitignored) and is wiped
  8. // and recreated on every run.
  9. import { mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
  10. import { dirname, join } from "node:path";
  11. import { fileURLToPath } from "node:url";
  12. import {
  13. actorGraphToMermaid,
  14. analyzeWorkflowScript,
  15. causalityGraphToMermaid,
  16. phaseGraphToMermaid,
  17. siteGraphToMermaid,
  18. toActorGraph,
  19. } from "../dist/index.js";
  20. const scriptDir = dirname(fileURLToPath(import.meta.url));
  21. const packageRoot = join(scriptDir, "..");
  22. const fixturesDir = join(packageRoot, "tests", "graphs");
  23. const chartsDir = join(packageRoot, "charts");
  24. const fixtures = readdirSync(fixturesDir)
  25. .filter((name) => name.endsWith(".ts"))
  26. .sort();
  27. rmSync(chartsDir, { force: true, recursive: true });
  28. mkdirSync(chartsDir, { recursive: true });
  29. const indexEntries = [];
  30. for (const fixture of fixtures) {
  31. const base = fixture.replace(/\.ts$/, "");
  32. const source = readFileSync(join(fixturesDir, fixture), "utf8");
  33. const result = analyzeWorkflowScript(source);
  34. // Every graph fixture is well-typed by construction; fail loudly (and list the
  35. // diagnostics) rather than silently skipping if one regresses.
  36. if (!result.ok || result.graph === undefined || result.causality === undefined) {
  37. const detail = result.diagnostics.map((d) => ` ${d.line}:${d.column} [TS${d.code}] ${d.message}`).join("\n");
  38. throw new Error(`fixture ${fixture} did not analyze clean:\n${detail}`);
  39. }
  40. const causality = causalityGraphToMermaid(result.causality);
  41. // 阶段图只在词汇表在场时有内容——零标记脚本这一节整段缺席,与 UI 的视图切换同条件。
  42. const phases = result.causality.phases;
  43. const site = siteGraphToMermaid(result.graph);
  44. const actor = actorGraphToMermaid(toActorGraph(result.graph));
  45. const page = [
  46. `# ${base}`,
  47. "",
  48. "```ts",
  49. source.trimEnd(),
  50. "```",
  51. "",
  52. ...(phases === undefined || phases.length === 0
  53. ? []
  54. : ["## Phase graph", "", "```mermaid", phaseGraphToMermaid(result.causality).trimEnd(), "```", ""]),
  55. "## Causality graph",
  56. "",
  57. "```mermaid",
  58. causality.trimEnd(),
  59. "```",
  60. "",
  61. "## Site graph",
  62. "",
  63. "```mermaid",
  64. site.trimEnd(),
  65. "```",
  66. "",
  67. "## Actor graph",
  68. "",
  69. "```mermaid",
  70. actor.trimEnd(),
  71. "```",
  72. "",
  73. ].join("\n");
  74. writeFileSync(join(chartsDir, `${base}.md`), page);
  75. indexEntries.push(`- [${base}](./${base}.md)`);
  76. }
  77. const readme = [
  78. "# Workflow graph charts",
  79. "",
  80. "Generated by `pnpm charts` from every `tests/graphs/*.ts` fixture. Do not edit by",
  81. "hand: this directory is wiped and rebuilt on each run, and is gitignored. Each page",
  82. "renders the fixture source, its causality graph, its site graph, and the derived",
  83. "actor summary as mermaid.",
  84. "",
  85. ...indexEntries,
  86. "",
  87. ].join("\n");
  88. writeFileSync(join(chartsDir, "README.md"), readme);
  89. console.log(`generated ${fixtures.length} fixture charts into ${chartsDir}`);