| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- // Build-time generator: renders every site-graph fixture to `charts/<name>.md` with
- // its source and its graphs — the phase quotient (the GUI's primary picture, only for a
- // fixture that declares markers), the causality graph, the site graph, and the derived
- // actor summary — as mermaid diagrams, for live viewing (any
- // mermaid preview / GitHub). Runs on the BUILT package
- // (`../dist/index.js`) so it needs no TS tooling at runtime; `pnpm charts` builds
- // first. The `charts/` directory is a generated artifact (gitignored) and is wiped
- // and recreated on every run.
- import { mkdirSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
- import { dirname, join } from "node:path";
- import { fileURLToPath } from "node:url";
- import {
- actorGraphToMermaid,
- analyzeWorkflowScript,
- causalityGraphToMermaid,
- phaseGraphToMermaid,
- siteGraphToMermaid,
- toActorGraph,
- } from "../dist/index.js";
- const scriptDir = dirname(fileURLToPath(import.meta.url));
- const packageRoot = join(scriptDir, "..");
- const fixturesDir = join(packageRoot, "tests", "graphs");
- const chartsDir = join(packageRoot, "charts");
- const fixtures = readdirSync(fixturesDir)
- .filter((name) => name.endsWith(".ts"))
- .sort();
- rmSync(chartsDir, { force: true, recursive: true });
- mkdirSync(chartsDir, { recursive: true });
- const indexEntries = [];
- for (const fixture of fixtures) {
- const base = fixture.replace(/\.ts$/, "");
- const source = readFileSync(join(fixturesDir, fixture), "utf8");
- const result = analyzeWorkflowScript(source);
- // Every graph fixture is well-typed by construction; fail loudly (and list the
- // diagnostics) rather than silently skipping if one regresses.
- if (!result.ok || result.graph === undefined || result.causality === undefined) {
- const detail = result.diagnostics.map((d) => ` ${d.line}:${d.column} [TS${d.code}] ${d.message}`).join("\n");
- throw new Error(`fixture ${fixture} did not analyze clean:\n${detail}`);
- }
- const causality = causalityGraphToMermaid(result.causality);
- // 阶段图只在词汇表在场时有内容——零标记脚本这一节整段缺席,与 UI 的视图切换同条件。
- const phases = result.causality.phases;
- const site = siteGraphToMermaid(result.graph);
- const actor = actorGraphToMermaid(toActorGraph(result.graph));
- const page = [
- `# ${base}`,
- "",
- "```ts",
- source.trimEnd(),
- "```",
- "",
- ...(phases === undefined || phases.length === 0
- ? []
- : ["## Phase graph", "", "```mermaid", phaseGraphToMermaid(result.causality).trimEnd(), "```", ""]),
- "## Causality graph",
- "",
- "```mermaid",
- causality.trimEnd(),
- "```",
- "",
- "## Site graph",
- "",
- "```mermaid",
- site.trimEnd(),
- "```",
- "",
- "## Actor graph",
- "",
- "```mermaid",
- actor.trimEnd(),
- "```",
- "",
- ].join("\n");
- writeFileSync(join(chartsDir, `${base}.md`), page);
- indexEntries.push(`- [${base}](./${base}.md)`);
- }
- const readme = [
- "# Workflow graph charts",
- "",
- "Generated by `pnpm charts` from every `tests/graphs/*.ts` fixture. Do not edit by",
- "hand: this directory is wiped and rebuilt on each run, and is gitignored. Each page",
- "renders the fixture source, its causality graph, its site graph, and the derived",
- "actor summary as mermaid.",
- "",
- ...indexEntries,
- "",
- ].join("\n");
- writeFileSync(join(chartsDir, "README.md"), readme);
- console.log(`generated ${fixtures.length} fixture charts into ${chartsDir}`);
|