forked from CatsMiaow/nestjs-project-structure
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.explorer.ts
70 lines (57 loc) · 2.27 KB
/
debug.explorer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import { Inject, Injectable, Type } from '@nestjs/common';
import { MODULE_METADATA } from '@nestjs/common/constants';
import { DiscoveryService, Reflector } from '@nestjs/core';
import type { InstanceWrapper } from '@nestjs/core/injector/instance-wrapper';
import { DebugLog } from './debug-log.decorator';
import { DEBUG_METADATA } from './debug.constant';
import type { DebugModuleOptions, DebugOptions, Metatype } from './debug.interface';
import { MODULE_OPTIONS_TOKEN } from './debug.module-definition';
@Injectable()
export class DebugExplorer {
private exclude: Set<string> = new Set(['Logger', 'ConfigService']);
constructor(
@Inject(MODULE_OPTIONS_TOKEN) private options: DebugModuleOptions,
private discoveryService: DiscoveryService,
private reflector: Reflector,
) {
this.addExcludeOption();
const instanceWrappers: InstanceWrapper[] = [
...this.discoveryService.getControllers(),
...this.discoveryService.getProviders(),
];
for (const wrapper of instanceWrappers.filter((wrap: InstanceWrapper) => !wrap.isNotMetatype)) {
const { instance, metatype } = wrapper;
if (!instance || !Object.getPrototypeOf(instance)) {
continue;
}
const metadata = this.reflector.get<DebugOptions | undefined>(DEBUG_METADATA, metatype);
if (!metadata) {
continue;
}
this.applyDecorator(metatype, metadata);
}
}
private addExcludeOption(): void {
if (!Array.isArray(this.options.exclude)) {
return;
}
this.options.exclude.forEach((type: string) => this.exclude.add(type));
}
private applyDecorator(metatype: Metatype, metadata: DebugOptions): void {
const instanceMetatypes: Type[] = [
...(this.reflector.get(MODULE_METADATA.CONTROLLERS, metatype) || []),
...(this.reflector.get(MODULE_METADATA.PROVIDERS, metatype) || []),
];
for (const meta of instanceMetatypes) {
if (typeof meta !== 'function' || this.exclude.has(meta.name) || metadata.exclude?.includes(meta)) {
continue;
}
this.exclude.add(meta.name);
DebugLog(metadata.context)(meta);
}
const imports = this.reflector.get<Type[] | undefined>('imports', metatype) || [];
for (const module of imports) {
this.applyDecorator(module, metadata);
}
}
}