Nestjs 非常方便地提供开箱即用的 DI 容器,但随着项目变得越来越复杂,很容易丢失所有依赖项及其顺序的完整情况。
问题:有没有办法以可以放入树/图中的方式获取所有依赖项(模块、提供程序、控制器...)的列表?我确信 Nestjs 在内部跟踪这些信息,我想知道访问它的最佳方式是什么。
(我找到了[
nestjs-dependency-graph
],但它手动扫描模块以获取元数据,没有找到所有依赖项,并且往往会进入无限循环)
我已经尝试了nestjs-spelunker,就像@Jay 的评论中提到的那样,它真的很可靠。
您只需生成图形文本并将其粘贴到mermaid-live-editor
import { NestFactory } from '@nestjs/core';
import { SpelunkerModule } from 'nestjs-spelunker';
async function bootstrap() {
const app = await NestFactory.create<NestFastifyApplication>(AppModule);
// 1. Generate the tree as text
const tree = SpelunkerModule.explore(app);
const root = SpelunkerModule.graph(tree);
const edges = SpelunkerModule.findGraphEdges(root);
const mermaidEdges = edges
.filter( // I'm just filtering some extra Modules out
({ from, to }) =>
!(
from.module.name === 'ConfigHostModule' ||
from.module.name === 'LoggerModule' ||
to.module.name === 'ConfigHostModule' ||
to.module.name === 'LoggerModule'
),
)
.map(({ from, to }) => `${from.module.name}-->${to.module.name}`);
console.log(`graph TD\n\t${mermaidEdges.join('\n\t')}`);
// 2. Copy and paste the log content in "https://mermaid.live/"
}
bootstrap();
这还不是一个完整的解决方案,但有一个我一直在开发的包nestjs-spelunker。您可以通读自述文件,看看它是否符合您的要求
来自 NestJS 的 Devtools https://devtools.nestjs.com/ 但它是付费的