gen-module-graph.ts 3.6 KB

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