gen-module-graph.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. 'plan',
  32. 'cordis',
  33. 'hooks',
  34. 'session-persistence',
  35. 'session-query',
  36. 'session-title',
  37. 'support',
  38. 'ui',
  39. ]
  40. function packageLink(pkg: Pkg): string {
  41. return `[\`${pkg.short}\`](../${pkg.rel})`
  42. }
  43. /** Render the full docs/module-graph.md content (pure, deterministic). */
  44. function render(pkgs: Pkg[]): string {
  45. const edges: string[] = []
  46. for (const p of pkgs) {
  47. for (const d of p.deps) edges.push(` ${nodeId('pkg', p.short)} --> ${nodeId('pkg', d)}`)
  48. }
  49. const byShort = new Map(pkgs.map(pkg => [pkg.short, pkg]))
  50. const groups = [...new Set(pkgs.map(pkg => pkg.group))].sort((a, b) => {
  51. const ia = GROUP_ORDER.indexOf(a)
  52. const ib = GROUP_ORDER.indexOf(b)
  53. const na = ia === -1 ? Number.MAX_SAFE_INTEGER : ia
  54. const nb = ib === -1 ? Number.MAX_SAFE_INTEGER : ib
  55. return na - nb || a.localeCompare(b)
  56. })
  57. const groupBlocks: string[] = []
  58. for (const group of groups) {
  59. groupBlocks.push(` subgraph ${nodeId('group', group)}["packages/${escLabel(group)}"]`)
  60. for (const pkg of pkgs.filter(p => p.group === group).sort((a, b) => a.short.localeCompare(b.short))) {
  61. groupBlocks.push(` ${nodeId('pkg', pkg.short)}["${escLabel(pkg.short)}"]`)
  62. }
  63. groupBlocks.push(' end')
  64. }
  65. const rows = pkgs.map((p) => {
  66. const deps = p.deps.length ? p.deps.map((d) => {
  67. const dep = byShort.get(d)
  68. return dep ? packageLink(dep) : `\`${d}\``
  69. }).join(', ') : '—'
  70. return `| ${packageLink(p)} | \`${p.group}\` | ${deps} |`
  71. })
  72. return [
  73. '<!-- Generated by scripts/gen-module-graph.ts — do not edit by hand.',
  74. ' Run `pnpm run gen-module-graph` to regenerate. -->',
  75. '',
  76. '# Module dependency graph',
  77. '',
  78. '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.',
  79. '',
  80. '```mermaid',
  81. 'flowchart TD',
  82. ...groupBlocks,
  83. ...edges,
  84. '```',
  85. '',
  86. '| Package | Group | Depends on |',
  87. '| --- | --- | --- |',
  88. ...rows,
  89. '',
  90. ].join('\n')
  91. }
  92. const content = render(collectPackageGraph(root, GROUP_ORDER, 'gen-module-graph'))
  93. if (process.argv.includes('--check')) {
  94. let committed: string | null = null
  95. try {
  96. committed = readFileSync(resolve(root, OUT), 'utf8')
  97. } catch {
  98. // A missing artifact is the expected read failure. Any read failure has the
  99. // same remedy here—regenerate—so it is reported as stale below.
  100. committed = null
  101. }
  102. if (committed === content) {
  103. console.log(`gen-module-graph: ${OUT} is up to date.`)
  104. process.exit(0)
  105. }
  106. console.error(`gen-module-graph: ${OUT} is stale. Run \`pnpm run gen-module-graph\` and commit ${OUT}.`)
  107. process.exit(1)
  108. }
  109. writeFileSync(resolve(root, OUT), content)
  110. console.log(`gen-module-graph: wrote ${OUT}.`)