2026-06-16 21:00:24 +08:00
/ * *
* Generate ( and verify ) the module dependency graph in docs / module - graph . md .
*
* The architectural shape of the harness lives implicitly in each package ' s
* ` peerDependencies ` — the canonical runtime - dependency signal ( devDeps mirror
* these as ` workspace:^ ` plus test - only extras , which would add noise ) . This
Reorganize packages into a modular hierarchy
Move the 18 flat packages/<name> packages into role-grouped dirs:
core/, llm/, bash/, session-persistence/, ui/, support/. Group dirs are
pure containers; each package keeps its @deepseek-ai/dsh-* name.
Collapse the per-package tsconfig paths maps (base + typecheck) into one
@deepseek-ai/dsh-* wildcard with a candidate per group, and derive the
publint list from the hierarchy. Update all depth-coupled globs/configs
(workspace, tsdown, vitest, eslint, knip, tsconfig includes/refs,
per-package tsconfigs, generators, doc-script scopes, type-equiv manifest)
and the cross-package/script relative imports in tests.
Fix doc-typecheck's workspacePaths() to parse tsconfig JSONC via the
TypeScript API instead of a regex comment-strip, which corrupted the
new wildcard `/*/` path candidates.
WIP: doc cross-links and package/RFC docs still to update.
2026-06-20 22:55:20 +08:00
* script reads every ` packages/* /* /package.json ` , keeps only the
2026-06-16 21:00:24 +08:00
* ` @deepseek-ai/dsh-* ` peer edges ( dropping the ` cordis ` peer ) , and renders a
2026-07-05 01:25:58 +08:00
* GitHub - viewable Mermaid graph grouped by ` packages/<group>/ ` plus a
* dependency table .
2026-06-16 21:00:24 +08:00
*
* The file is fully generated — never hand - edit it . Output is deterministic
* ( packages and edges sorted ) so a regenerate - and - diff freshness check is
* stable .
*
* ` tsx scripts/gen-module-graph.ts ` → write docs / module - graph . md
* ` tsx scripts/gen-module-graph.ts --check ` → exit 1 if the committed file
* is stale ( CI / pre - push gate )
* /
2026-07-14 00:24:04 +08:00
import { resolve } from 'node:path'
import { readFileSync , writeFileSync } from 'node:fs'
import {
collectPackageGraph ,
escapeMermaidLabel as escLabel ,
graphNodeId as nodeId ,
type PackageGraphNode ,
} from './package-graph.ts'
2026-06-16 21:00:24 +08:00
const root = resolve ( import . meta . dirname , '..' )
const OUT = 'docs/module-graph.md'
2026-07-14 00:24:04 +08:00
type Pkg = PackageGraphNode
2026-06-16 21:00:24 +08:00
2026-07-05 01:25:58 +08:00
const GROUP_ORDER = [
'util' ,
'llm' ,
'core' ,
'bash' ,
'fs' ,
2026-07-10 14:19:06 +08:00
'skill' ,
2026-07-05 01:25:58 +08:00
'compact' ,
'subagent' ,
'web' ,
2026-07-08 10:06:07 +08:00
'timeout' ,
2026-07-05 01:25:58 +08:00
'todo' ,
2026-07-08 11:50:12 +08:00
'cordis' ,
2026-07-05 01:25:58 +08:00
'hooks' ,
'session-persistence' ,
'support' ,
'ui' ,
]
function packageLink ( pkg : Pkg ) : string {
return ` [ \` ${ pkg . short } \` ](../ ${ pkg . rel } ) `
}
2026-06-16 21:00:24 +08:00
/** Render the full docs/module-graph.md content (pure, deterministic). */
function render ( pkgs : Pkg [ ] ) : string {
const edges : string [ ] = [ ]
for ( const p of pkgs ) {
2026-07-05 01:25:58 +08:00
for ( const d of p . deps ) edges . push ( ` ${ nodeId ( 'pkg' , p . short ) } --> ${ nodeId ( 'pkg' , d ) } ` )
}
const byShort = new Map ( pkgs . map ( pkg = > [ pkg . short , pkg ] ) )
const groups = [ . . . new Set ( pkgs . map ( pkg = > pkg . group ) ) ] . sort ( ( a , b ) = > {
const ia = GROUP_ORDER . indexOf ( a )
const ib = GROUP_ORDER . indexOf ( b )
const na = ia === - 1 ? Number . MAX_SAFE_INTEGER : ia
const nb = ib === - 1 ? Number . MAX_SAFE_INTEGER : ib
return na - nb || a . localeCompare ( b )
} )
const groupBlocks : string [ ] = [ ]
for ( const group of groups ) {
groupBlocks . push ( ` subgraph ${ nodeId ( 'group' , group ) } ["packages/ ${ escLabel ( group ) } "] ` )
for ( const pkg of pkgs . filter ( p = > p . group === group ) . sort ( ( a , b ) = > a . short . localeCompare ( b . short ) ) ) {
groupBlocks . push ( ` ${ nodeId ( 'pkg' , pkg . short ) } [" ${ escLabel ( pkg . short ) } "] ` )
}
groupBlocks . push ( ' end' )
2026-06-16 21:00:24 +08:00
}
2026-07-05 01:25:58 +08:00
const rows = pkgs . map ( ( p ) = > {
const deps = p . deps . length ? p . deps . map ( ( d ) = > {
const dep = byShort . get ( d )
return dep ? packageLink ( dep ) : ` \` ${ d } \` `
} ) . join ( ', ' ) : '—'
return ` | ${ packageLink ( p ) } | \` ${ p . group } \` | ${ deps } | `
} )
2026-06-16 21:00:24 +08:00
return [
'<!-- Generated by scripts/gen-module-graph.ts — do not edit by hand.' ,
' Run `pnpm run gen-module-graph` to regenerate. -->' ,
'' ,
'# Module dependency graph' ,
'' ,
2026-07-05 01:25:58 +08:00
'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.' ,
2026-06-16 21:00:24 +08:00
'' ,
'```mermaid' ,
2026-07-05 01:25:58 +08:00
'flowchart TD' ,
. . . groupBlocks ,
2026-06-16 21:00:24 +08:00
. . . edges ,
'```' ,
'' ,
2026-07-05 01:25:58 +08:00
'| Package | Group | Depends on |' ,
'| --- | --- | --- |' ,
2026-06-16 21:00:24 +08:00
. . . rows ,
'' ,
] . join ( '\n' )
}
2026-07-14 00:24:04 +08:00
const content = render ( collectPackageGraph ( root , GROUP_ORDER , 'gen-module-graph' ) )
2026-06-16 21:00:24 +08:00
if ( process . argv . includes ( '--check' ) ) {
let committed : string | null = null
try {
committed = readFileSync ( resolve ( root , OUT ) , 'utf8' )
} catch {
// Only an ENOENT (file not yet generated) is expected here; readFileSync of
// a present-but-unreadable file is not a state this repo produces. Either
// way the remedy is the same — regenerate — so we treat a read failure as
// "stale" and fall through to the failure branch below.
committed = null
}
if ( committed === content ) {
console . log ( ` gen-module-graph: ${ OUT } is up to date. ` )
process . exit ( 0 )
}
console . error ( ` gen-module-graph: ${ OUT } is stale. Run \` pnpm run gen-module-graph \` and commit ${ OUT } . ` )
process . exit ( 1 )
}
writeFileSync ( resolve ( root , OUT ) , content )
console . log ( ` gen-module-graph: wrote ${ OUT } . ` )