gen-module-graph.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /**
  2. * Generate `docs/module-graph.md` from in-repo `peerDependencies`, the canonical
  3. * runtime edges. The deterministic output groups packages by directory and
  4. * renders both Mermaid and a dependency table; `--check` verifies freshness.
  5. */
  6. import { resolve } from 'node:path'
  7. import { readFileSync, writeFileSync } from 'node:fs'
  8. import {
  9. collectPackageGraph,
  10. escapeMermaidLabel as escLabel,
  11. graphNodeId as nodeId,
  12. type PackageGraphNode,
  13. } from './package-graph.ts'
  14. const root = resolve(import.meta.dirname, '..')
  15. const OUT = 'docs/module-graph.md'
  16. type Pkg = PackageGraphNode
  17. const GROUP_ORDER = [
  18. 'util',
  19. 'llm',
  20. 'core',
  21. 'goal',
  22. 'bash',
  23. 'fs',
  24. 'skill',
  25. 'compact',
  26. 'subagent',
  27. 'web',
  28. 'spill',
  29. 'timeout',
  30. 'todo',
  31. 'cordis',
  32. 'hooks',
  33. 'session-persistence',
  34. 'session-query',
  35. 'session-title',
  36. 'support',
  37. 'ui',
  38. ]
  39. function packageLink(pkg: Pkg): string {
  40. return `[\`${pkg.short}\`](../${pkg.rel})`
  41. }
  42. /** Render the full docs/module-graph.md content (pure, deterministic). */
  43. function render(pkgs: Pkg[]): string {
  44. const edges: string[] = []
  45. for (const p of pkgs) {
  46. for (const d of p.deps) edges.push(` ${nodeId('pkg', p.short)} --> ${nodeId('pkg', d)}`)
  47. }
  48. const byShort = new Map(pkgs.map(pkg => [pkg.short, pkg]))
  49. const groups = [...new Set(pkgs.map(pkg => pkg.group))].sort((a, b) => {
  50. const ia = GROUP_ORDER.indexOf(a)
  51. const ib = GROUP_ORDER.indexOf(b)
  52. const na = ia === -1 ? Number.MAX_SAFE_INTEGER : ia
  53. const nb = ib === -1 ? Number.MAX_SAFE_INTEGER : ib
  54. return na - nb || a.localeCompare(b)
  55. })
  56. const groupBlocks: string[] = []
  57. for (const group of groups) {
  58. groupBlocks.push(` subgraph ${nodeId('group', group)}["packages/${escLabel(group)}"]`)
  59. for (const pkg of pkgs.filter(p => p.group === group).sort((a, b) => a.short.localeCompare(b.short))) {
  60. groupBlocks.push(` ${nodeId('pkg', pkg.short)}["${escLabel(pkg.short)}"]`)
  61. }
  62. groupBlocks.push(' end')
  63. }
  64. const rows = pkgs.map((p) => {
  65. const deps = p.deps.length ? p.deps.map((d) => {
  66. const dep = byShort.get(d)
  67. return dep ? packageLink(dep) : `\`${d}\``
  68. }).join(', ') : '—'
  69. return `| ${packageLink(p)} | \`${p.group}\` | ${deps} |`
  70. })
  71. return [
  72. '<!-- Generated by scripts/gen-module-graph.ts — do not edit by hand.',
  73. ' Run `pnpm run gen-module-graph` to regenerate. -->',
  74. '',
  75. '# Module dependency graph',
  76. '',
  77. 'Inter-package dependencies among the `@deepseek-ai/dsh-*` harness packages, derived from each package\'s `peerDependencies` (the canonical runtime-dependency signal) and grouped by the `packages/<group>/<pkg>` hierarchy. An edge `a --> b` means package `a` depends on package `b`. Names have the `@deepseek-ai/dsh-` prefix stripped.',
  78. '',
  79. '```mermaid',
  80. 'flowchart TD',
  81. ...groupBlocks,
  82. ...edges,
  83. '```',
  84. '',
  85. '| Package | Group | Depends on |',
  86. '| --- | --- | --- |',
  87. ...rows,
  88. '',
  89. ].join('\n')
  90. }
  91. const content = render(collectPackageGraph(root, GROUP_ORDER, 'gen-module-graph'))
  92. if (process.argv.includes('--check')) {
  93. let committed: string | null = null
  94. try {
  95. committed = readFileSync(resolve(root, OUT), 'utf8')
  96. } catch {
  97. // A missing artifact is the expected read failure. Any read failure has the
  98. // same remedy here—regenerate—so it is reported as stale below.
  99. committed = null
  100. }
  101. if (committed === content) {
  102. console.log(`gen-module-graph: ${OUT} is up to date.`)
  103. process.exit(0)
  104. }
  105. console.error(`gen-module-graph: ${OUT} is stale. Run \`pnpm run gen-module-graph\` and commit ${OUT}.`)
  106. process.exit(1)
  107. }
  108. writeFileSync(resolve(root, OUT), content)
  109. console.log(`gen-module-graph: wrote ${OUT}.`)