gen-module-graph.ts 3.6 KB

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