Files
mtusi2026lab5/app/scripts/graph-modules.ts
T
ilya.tyunin 1982bdb690 lab5: базовый коммит из lab4 (abba248)
Перенесено дерево lab4 целиком: NestJS-приложение с вложениями и S3,
Dockerfile, compose.yaml, infra/{haproxy,minio}, стендовые скрипты,
scripts/lab4, evidence/lab4 и contracts/extensions.openapi.json.

Задание lab5 оставлено своим: README, REPORT, COURSE, lab.json,
contracts/{openapi,infrastructure,jobs.schema,attachment-states,examples},
tests, compose.example.yaml, .github/pull_request_template.md.

Адаптации при переносе:
- docker-проект mtusi-lab4 -> mtusi-lab5 и тег образа mtusi-messenger:lab5,
  чтобы тома и контейнеры не пересекались с lab4 (scripts/stop-one.sh:
  LAB_PROJECT вместо LAB4_PROJECT);
- Makefile: цели lab5 сохранены, добавлены стендовые (stand-up/down,
  wait-ready, init-env, test-e2e, test-dependencies, s3-security,
  consistency, bench);
- .env.example: реальные переменные приложения и стенда lab4 (без них
  compose.yaml не поднимается) + блок lab5 (BROKER_URL_FILE, WORKER_*).
2026-11-04 23:44:11 +03:00

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),
);