124 lines
3.5 KiB
TypeScript
124 lines
3.5 KiB
TypeScript
import { mkdir, writeFile } from 'node:fs/promises';
|
|
import 'reflect-metadata';
|
|
import { SpelunkerModule, type DebuggedTree } from 'nestjs-spelunker';
|
|
import { AppModule } from '../dist/app.module.js';
|
|
|
|
function mermaidId(name: string): string {
|
|
return name.replace(/[^a-zA-Z0-9_]/gu, '_');
|
|
}
|
|
|
|
function findDependencyModule(
|
|
tree: DebuggedTree[],
|
|
dependency: string,
|
|
): DebuggedTree | undefined {
|
|
return tree.find(
|
|
(candidate) =>
|
|
candidate.providers.some((item) => item.name === dependency) ||
|
|
candidate.controllers.some((item) => item.name === dependency),
|
|
);
|
|
}
|
|
|
|
function renderMermaid(tree: DebuggedTree[]): string {
|
|
const lines = [
|
|
'flowchart LR',
|
|
' %% Generated by scripts/graph-modules.ts',
|
|
' %% Solid arrows: module imports; dotted arrows: constructor dependencies',
|
|
];
|
|
const moduleNames = tree.map((module) => module.name);
|
|
|
|
for (const module of tree) {
|
|
const moduleId = mermaidId(module.name);
|
|
lines.push(` subgraph ${moduleId}["${module.name}"]`);
|
|
for (const provider of module.providers) {
|
|
lines.push(
|
|
` ${moduleId}_${mermaidId(provider.name)}["provider: ${provider.name}"]`,
|
|
);
|
|
}
|
|
for (const controller of module.controllers) {
|
|
lines.push(
|
|
` ${moduleId}_${mermaidId(controller.name)}["controller: ${controller.name}"]`,
|
|
);
|
|
}
|
|
lines.push(' end');
|
|
}
|
|
|
|
for (const module of tree) {
|
|
for (const imported of module.imports) {
|
|
if (moduleNames.includes(imported)) {
|
|
lines.push(
|
|
` ${mermaidId(module.name)} -->|imports| ${mermaidId(imported)}`,
|
|
);
|
|
}
|
|
}
|
|
for (const item of [...module.providers, ...module.controllers]) {
|
|
for (const dependency of item.dependencies) {
|
|
const targetModule = findDependencyModule(tree, dependency);
|
|
if (targetModule) {
|
|
const source = `${mermaidId(module.name)}_${mermaidId(item.name)}`;
|
|
const target = `${mermaidId(targetModule.name)}_${mermaidId(dependency)}`;
|
|
lines.push(` ${source} -.->|injects| ${target}`);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return `${lines.join('\n')}\n`;
|
|
}
|
|
|
|
function renderMarkdown(tree: DebuggedTree[], mermaid: string): string {
|
|
const lines = [
|
|
'# NestJS dependency graph',
|
|
'',
|
|
'Generated from `AppModule` with `nestjs-spelunker` static module debugging.',
|
|
'',
|
|
'- Solid arrows: module imports.',
|
|
'- Dotted arrows: constructor dependencies.',
|
|
'- The generator does not boot PostgreSQL or Redis.',
|
|
'',
|
|
'```mermaid',
|
|
mermaid.trim(),
|
|
'```',
|
|
'',
|
|
'## Module details',
|
|
'',
|
|
];
|
|
|
|
for (const module of tree) {
|
|
lines.push(
|
|
`### ${module.name}`,
|
|
'',
|
|
`- Imports: ${module.imports.join(', ') || 'none'}`,
|
|
);
|
|
lines.push(
|
|
`- Exports: ${module.exports.map((item) => `${item.name} (${item.type})`).join(', ') || 'none'}`,
|
|
);
|
|
lines.push('', '| Kind | Name | Dependencies |', '| --- | --- | --- |');
|
|
for (const provider of module.providers) {
|
|
lines.push(
|
|
`| provider | ${provider.name} | ${provider.dependencies.join(', ') || 'none'} |`,
|
|
);
|
|
}
|
|
for (const controller of module.controllers) {
|
|
lines.push(
|
|
`| controller | ${controller.name} | ${controller.dependencies.join(', ') || 'none'} |`,
|
|
);
|
|
}
|
|
lines.push('');
|
|
}
|
|
|
|
return lines.join('\n');
|
|
}
|
|
|
|
const tree = await SpelunkerModule.debug(AppModule);
|
|
const mermaid = renderMermaid(tree);
|
|
await mkdir('../docs/architecture', { recursive: true });
|
|
await writeFile(
|
|
'../docs/architecture/nest-module-details.json',
|
|
JSON.stringify(tree, null, 2),
|
|
);
|
|
await writeFile('../docs/architecture/nest-module-graph.mmd', mermaid);
|
|
await writeFile(
|
|
'../docs/architecture/nest-dependency-graph.md',
|
|
renderMarkdown(tree, mermaid),
|
|
);
|