nest-graph-inspector 0.8.0-test.2 → 0.8.0-test.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/discovery.d.ts +3 -4
- package/dist/adapters/discovery.js +9 -30
- package/dist/nest-graph-inspector.module.js +2 -0
- package/dist/nest-graph-inspector.setup.d.ts +7 -4
- package/dist/nest-graph-inspector.setup.js +36 -34
- package/dist/source-metadata.service.d.ts +16 -0
- package/dist/source-metadata.service.js +94 -0
- package/package.json +1 -1
|
@@ -4,6 +4,7 @@ import type { NestGraphInspectorModuleOptions } from "../nest-graph-inspector.ty
|
|
|
4
4
|
import { ModuleController } from "../types/module-controller.type";
|
|
5
5
|
import { ModuleProvider } from "../types/module-provider.type";
|
|
6
6
|
import { RuntimeTraceRecorder } from "../runtime-trace.recorder";
|
|
7
|
+
import { SourceMetadataService } from "../source-metadata.service";
|
|
7
8
|
export type ModuleTree = {
|
|
8
9
|
name: string;
|
|
9
10
|
jsdoc?: string;
|
|
@@ -19,15 +20,14 @@ export declare class DiscoveryAdapter {
|
|
|
19
20
|
private readonly options;
|
|
20
21
|
private readonly modulesContainer;
|
|
21
22
|
private readonly runtimeTraceRecorder;
|
|
22
|
-
private readonly
|
|
23
|
+
private readonly sourceMetadata;
|
|
23
24
|
private readonly ignoreProvider;
|
|
24
25
|
private readonly ignoreImport;
|
|
25
26
|
private readonly nestCoreModuleName;
|
|
26
27
|
private readonly nestCoreProviders;
|
|
27
|
-
private readonly tsMorphProject;
|
|
28
28
|
private readonly runtimeTraceInstrumentedInstances;
|
|
29
29
|
private cachedTree;
|
|
30
|
-
constructor(options: NestGraphInspectorModuleOptions, modulesContainer: ModulesContainer, runtimeTraceRecorder: RuntimeTraceRecorder);
|
|
30
|
+
constructor(options: NestGraphInspectorModuleOptions, modulesContainer: ModulesContainer, runtimeTraceRecorder: RuntimeTraceRecorder, sourceMetadata: SourceMetadataService);
|
|
31
31
|
scan(): ModuleTree;
|
|
32
32
|
get tree(): ModuleTree;
|
|
33
33
|
private getRootModule;
|
|
@@ -52,7 +52,6 @@ export declare class DiscoveryAdapter {
|
|
|
52
52
|
private extractControllers;
|
|
53
53
|
private extractModuleMembers;
|
|
54
54
|
private extractModuleMember;
|
|
55
|
-
private createTsMorphProject;
|
|
56
55
|
private extractModuleJsDoc;
|
|
57
56
|
private extractClassJsDoc;
|
|
58
57
|
private extractClassJsDocByName;
|
|
@@ -11,33 +11,30 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
11
11
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
12
12
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
13
13
|
};
|
|
14
|
-
var DiscoveryAdapter_1;
|
|
15
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
15
|
exports.DiscoveryAdapter = void 0;
|
|
17
|
-
const node_fs_1 = require("node:fs");
|
|
18
|
-
const node_path_1 = require("node:path");
|
|
19
16
|
const common_1 = require("@nestjs/common");
|
|
20
17
|
const core_1 = require("@nestjs/core");
|
|
21
18
|
const nest_graph_inspector_config_1 = require("../nest-graph-inspector.config");
|
|
22
19
|
const nest_graph_inspector_module_1 = require("../nest-graph-inspector.module");
|
|
23
20
|
const runtime_trace_recorder_1 = require("../runtime-trace.recorder");
|
|
24
|
-
const
|
|
25
|
-
let DiscoveryAdapter =
|
|
21
|
+
const source_metadata_service_1 = require("../source-metadata.service");
|
|
22
|
+
let DiscoveryAdapter = class DiscoveryAdapter {
|
|
26
23
|
options;
|
|
27
24
|
modulesContainer;
|
|
28
25
|
runtimeTraceRecorder;
|
|
29
|
-
|
|
26
|
+
sourceMetadata;
|
|
30
27
|
ignoreProvider;
|
|
31
28
|
ignoreImport;
|
|
32
29
|
nestCoreModuleName;
|
|
33
30
|
nestCoreProviders;
|
|
34
|
-
tsMorphProject = this.createTsMorphProject();
|
|
35
31
|
runtimeTraceInstrumentedInstances = new WeakSet();
|
|
36
32
|
cachedTree;
|
|
37
|
-
constructor(options, modulesContainer, runtimeTraceRecorder) {
|
|
33
|
+
constructor(options, modulesContainer, runtimeTraceRecorder, sourceMetadata) {
|
|
38
34
|
this.options = options;
|
|
39
35
|
this.modulesContainer = modulesContainer;
|
|
40
36
|
this.runtimeTraceRecorder = runtimeTraceRecorder;
|
|
37
|
+
this.sourceMetadata = sourceMetadata;
|
|
41
38
|
this.ignoreProvider = [
|
|
42
39
|
...(this.options.ignoreProvider ?? nest_graph_inspector_module_1.defaultOptions.ignoreProvider ?? []),
|
|
43
40
|
];
|
|
@@ -343,17 +340,6 @@ let DiscoveryAdapter = DiscoveryAdapter_1 = class DiscoveryAdapter {
|
|
|
343
340
|
dependencies: this.extractDependencies(wrapper, moduleRef),
|
|
344
341
|
};
|
|
345
342
|
}
|
|
346
|
-
createTsMorphProject() {
|
|
347
|
-
const tsConfigFilePath = (0, node_path_1.join)(process.cwd(), "tsconfig.json");
|
|
348
|
-
if (!(0, node_fs_1.existsSync)(tsConfigFilePath)) {
|
|
349
|
-
this.logger.warn(`Could not find tsconfig.json at ${tsConfigFilePath}; JSDoc metadata will be skipped.`);
|
|
350
|
-
return new ts_morph_1.Project();
|
|
351
|
-
}
|
|
352
|
-
return new ts_morph_1.Project({
|
|
353
|
-
tsConfigFilePath,
|
|
354
|
-
skipAddingFilesFromTsConfig: false,
|
|
355
|
-
});
|
|
356
|
-
}
|
|
357
343
|
extractModuleJsDoc(moduleRef) {
|
|
358
344
|
return moduleRef.metatype
|
|
359
345
|
? this.extractClassJsDocByName(moduleRef.metatype.name)
|
|
@@ -364,15 +350,7 @@ let DiscoveryAdapter = DiscoveryAdapter_1 = class DiscoveryAdapter {
|
|
|
364
350
|
return className ? this.extractClassJsDocByName(className) : undefined;
|
|
365
351
|
}
|
|
366
352
|
extractClassJsDocByName(className) {
|
|
367
|
-
|
|
368
|
-
.getSourceFiles()
|
|
369
|
-
.flatMap((sourceFile) => sourceFile.getClasses())
|
|
370
|
-
.find((classDeclaration) => classDeclaration.getName() === className);
|
|
371
|
-
return targetClass
|
|
372
|
-
?.getJsDocs()
|
|
373
|
-
.map((doc) => doc.getCommentText())
|
|
374
|
-
.filter((comment) => !!comment)
|
|
375
|
-
.join("\n");
|
|
353
|
+
return this.sourceMetadata.getClassJsDoc(className);
|
|
376
354
|
}
|
|
377
355
|
wrapperClassName(wrapper) {
|
|
378
356
|
if (wrapper.metatype?.name) {
|
|
@@ -534,9 +512,10 @@ let DiscoveryAdapter = DiscoveryAdapter_1 = class DiscoveryAdapter {
|
|
|
534
512
|
}
|
|
535
513
|
};
|
|
536
514
|
exports.DiscoveryAdapter = DiscoveryAdapter;
|
|
537
|
-
exports.DiscoveryAdapter = DiscoveryAdapter =
|
|
515
|
+
exports.DiscoveryAdapter = DiscoveryAdapter = __decorate([
|
|
538
516
|
(0, common_1.Injectable)(),
|
|
539
517
|
__param(0, (0, common_1.Inject)(nest_graph_inspector_config_1.MODULE_OPTIONS_TOKEN)),
|
|
540
518
|
__metadata("design:paramtypes", [Object, core_1.ModulesContainer,
|
|
541
|
-
runtime_trace_recorder_1.RuntimeTraceRecorder
|
|
519
|
+
runtime_trace_recorder_1.RuntimeTraceRecorder,
|
|
520
|
+
source_metadata_service_1.SourceMetadataService])
|
|
542
521
|
], DiscoveryAdapter);
|
|
@@ -19,6 +19,7 @@ const proxy_adapter_1 = require("./adapters/proxy.adapter");
|
|
|
19
19
|
const http_serve_adapter_1 = require("./adapters/http-serve.adapter");
|
|
20
20
|
const direct_run_output_adapter_1 = require("./adapters/direct-run-output.adapter");
|
|
21
21
|
const runtime_trace_recorder_1 = require("./runtime-trace.recorder");
|
|
22
|
+
const source_metadata_service_1 = require("./source-metadata.service");
|
|
22
23
|
exports.defaultOptions = {
|
|
23
24
|
outputs: [
|
|
24
25
|
{
|
|
@@ -56,6 +57,7 @@ exports.NestGraphInspectorModule = NestGraphInspectorModule = __decorate([
|
|
|
56
57
|
},
|
|
57
58
|
nest_graph_inspector_setup_1.NestGraphInspectorSetup,
|
|
58
59
|
discovery_1.DiscoveryAdapter,
|
|
60
|
+
source_metadata_service_1.SourceMetadataService,
|
|
59
61
|
json_output_adapter_1.JsonOutputAdapter,
|
|
60
62
|
file_output_adapter_1.FileOutputAdapter,
|
|
61
63
|
http_serve_adapter_1.HttpServeAdapter,
|
|
@@ -8,6 +8,7 @@ import { FileOutputAdapter } from "./adapters/file-output.adapter";
|
|
|
8
8
|
import { JsonOutputAdapter } from "./adapters/json-output.adapter";
|
|
9
9
|
import { ViewerOutputAdapter } from "./adapters/viewer-output.adapter";
|
|
10
10
|
import { RuntimeTraceRecorder } from "./runtime-trace.recorder";
|
|
11
|
+
import { SourceMetadataService } from "./source-metadata.service";
|
|
11
12
|
export declare class NestGraphInspectorSetup implements OnModuleInit {
|
|
12
13
|
private readonly options;
|
|
13
14
|
private readonly modulesContainer;
|
|
@@ -17,11 +18,13 @@ export declare class NestGraphInspectorSetup implements OnModuleInit {
|
|
|
17
18
|
private readonly jsonOutputAdapter;
|
|
18
19
|
private readonly viewerOutputAdapter;
|
|
19
20
|
private readonly runtimeTraceRecorder;
|
|
21
|
+
private readonly sourceMetadata;
|
|
20
22
|
private readonly logger;
|
|
21
23
|
private readonly outputAdapters;
|
|
22
|
-
private readonly tsMorphProject;
|
|
23
24
|
private graphOutput;
|
|
24
|
-
|
|
25
|
+
private readonly directRunParameterTypes;
|
|
26
|
+
private directRunProviderInstances;
|
|
27
|
+
constructor(options: NestGraphInspectorModuleOptions, modulesContainer: ModulesContainer, discovery: DiscoveryAdapter, httpOutputAdapter: HttpOutputAdapter, fileOutputAdapter: FileOutputAdapter, jsonOutputAdapter: JsonOutputAdapter, viewerOutputAdapter: ViewerOutputAdapter, runtimeTraceRecorder: RuntimeTraceRecorder, sourceMetadata: SourceMetadataService);
|
|
25
28
|
onModuleInit(): Promise<void>;
|
|
26
29
|
private getGraphOutput;
|
|
27
30
|
private installViewerOutput;
|
|
@@ -36,13 +39,13 @@ export declare class NestGraphInspectorSetup implements OnModuleInit {
|
|
|
36
39
|
private createDiscovery;
|
|
37
40
|
private createModuleMapFromTree;
|
|
38
41
|
private flattenModuleTree;
|
|
39
|
-
private createTsMorphProject;
|
|
40
42
|
private enrichModuleMap;
|
|
41
43
|
private resolveDirectRunProviderMeta;
|
|
42
44
|
private findDirectRunProviderInstance;
|
|
43
|
-
private
|
|
45
|
+
private getDirectRunProviderInstances;
|
|
44
46
|
private getDirectRunMethods;
|
|
45
47
|
private getDirectRunMethodParameterTypes;
|
|
48
|
+
private runtimeParameterTypes;
|
|
46
49
|
private extractMethodParameterTypesFromProject;
|
|
47
50
|
private extractMethodParameterNamesFromFunctionSource;
|
|
48
51
|
private typeToTypeScriptCode;
|
|
@@ -14,7 +14,6 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
14
14
|
var NestGraphInspectorSetup_1;
|
|
15
15
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
16
|
exports.NestGraphInspectorSetup = void 0;
|
|
17
|
-
const node_fs_1 = require("node:fs");
|
|
18
17
|
const node_path_1 = require("node:path");
|
|
19
18
|
const common_1 = require("@nestjs/common");
|
|
20
19
|
const core_1 = require("@nestjs/core");
|
|
@@ -27,6 +26,7 @@ const json_output_adapter_1 = require("./adapters/json-output.adapter");
|
|
|
27
26
|
const viewer_output_adapter_1 = require("./adapters/viewer-output.adapter");
|
|
28
27
|
const ts_morph_1 = require("ts-morph");
|
|
29
28
|
const runtime_trace_recorder_1 = require("./runtime-trace.recorder");
|
|
29
|
+
const source_metadata_service_1 = require("./source-metadata.service");
|
|
30
30
|
const MAX_TYPE_RENDER_DEPTH = 12;
|
|
31
31
|
const MAX_TYPE_RENDER_MEMBERS = 50;
|
|
32
32
|
const MAX_TYPE_RENDER_LENGTH = 8_000;
|
|
@@ -41,11 +41,13 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
41
41
|
jsonOutputAdapter;
|
|
42
42
|
viewerOutputAdapter;
|
|
43
43
|
runtimeTraceRecorder;
|
|
44
|
+
sourceMetadata;
|
|
44
45
|
logger = new common_1.Logger(NestGraphInspectorSetup_1.name);
|
|
45
46
|
outputAdapters;
|
|
46
|
-
tsMorphProject = this.createTsMorphProject();
|
|
47
47
|
graphOutput;
|
|
48
|
-
|
|
48
|
+
directRunParameterTypes = new WeakMap();
|
|
49
|
+
directRunProviderInstances;
|
|
50
|
+
constructor(options, modulesContainer, discovery, httpOutputAdapter, fileOutputAdapter, jsonOutputAdapter, viewerOutputAdapter, runtimeTraceRecorder, sourceMetadata) {
|
|
49
51
|
this.options = options;
|
|
50
52
|
this.modulesContainer = modulesContainer;
|
|
51
53
|
this.discovery = discovery;
|
|
@@ -54,6 +56,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
54
56
|
this.jsonOutputAdapter = jsonOutputAdapter;
|
|
55
57
|
this.viewerOutputAdapter = viewerOutputAdapter;
|
|
56
58
|
this.runtimeTraceRecorder = runtimeTraceRecorder;
|
|
59
|
+
this.sourceMetadata = sourceMetadata;
|
|
57
60
|
this.outputAdapters = {
|
|
58
61
|
http: this.httpOutputAdapter,
|
|
59
62
|
markdown: this.fileOutputAdapter,
|
|
@@ -162,7 +165,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
162
165
|
return this.createModuleMapFromTree(discovery.scan());
|
|
163
166
|
}
|
|
164
167
|
createDiscovery(options) {
|
|
165
|
-
return new discovery_1.DiscoveryAdapter(options, this.modulesContainer, this.runtimeTraceRecorder);
|
|
168
|
+
return new discovery_1.DiscoveryAdapter(options, this.modulesContainer, this.runtimeTraceRecorder, this.sourceMetadata);
|
|
166
169
|
}
|
|
167
170
|
createModuleMapFromTree(moduleTree) {
|
|
168
171
|
return {
|
|
@@ -190,17 +193,6 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
190
193
|
visit(moduleTree);
|
|
191
194
|
return modules;
|
|
192
195
|
}
|
|
193
|
-
createTsMorphProject() {
|
|
194
|
-
const tsConfigFilePath = (0, node_path_1.join)(process.cwd(), "tsconfig.json");
|
|
195
|
-
if (!(0, node_fs_1.existsSync)(tsConfigFilePath)) {
|
|
196
|
-
this.logger.warn(`Could not find tsconfig.json at ${tsConfigFilePath}; JSDoc metadata will be skipped.`);
|
|
197
|
-
return new ts_morph_1.Project();
|
|
198
|
-
}
|
|
199
|
-
return new ts_morph_1.Project({
|
|
200
|
-
tsConfigFilePath,
|
|
201
|
-
skipAddingFilesFromTsConfig: false,
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
196
|
enrichModuleMap(moduleMap) {
|
|
205
197
|
if (!moduleMap.modules) {
|
|
206
198
|
return moduleMap;
|
|
@@ -240,19 +232,26 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
240
232
|
};
|
|
241
233
|
}
|
|
242
234
|
findDirectRunProviderInstance(moduleName, providerName) {
|
|
243
|
-
|
|
235
|
+
const moduleInstances = this.getDirectRunProviderInstances().get(moduleName);
|
|
236
|
+
return moduleInstances?.get(providerName);
|
|
244
237
|
}
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
238
|
+
getDirectRunProviderInstances() {
|
|
239
|
+
const tree = this.discovery.scan();
|
|
240
|
+
if (this.directRunProviderInstances?.tree === tree) {
|
|
241
|
+
return this.directRunProviderInstances.modules;
|
|
248
242
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if (
|
|
252
|
-
|
|
243
|
+
const modules = new Map();
|
|
244
|
+
const visit = (node) => {
|
|
245
|
+
if (!modules.has(node.name)) {
|
|
246
|
+
modules.set(node.name, node.providerInstances);
|
|
253
247
|
}
|
|
254
|
-
|
|
255
|
-
|
|
248
|
+
for (const child of node.children) {
|
|
249
|
+
visit(child);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
visit(tree);
|
|
253
|
+
this.directRunProviderInstances = { tree, modules };
|
|
254
|
+
return modules;
|
|
256
255
|
}
|
|
257
256
|
getDirectRunMethods(instance) {
|
|
258
257
|
if (!instance ||
|
|
@@ -290,15 +289,21 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
290
289
|
}
|
|
291
290
|
getDirectRunMethodParameterTypes(param) {
|
|
292
291
|
const { instance, method, methodName } = param;
|
|
292
|
+
const cachedParameterTypes = this.directRunParameterTypes.get(method);
|
|
293
|
+
if (cachedParameterTypes) {
|
|
294
|
+
return cachedParameterTypes;
|
|
295
|
+
}
|
|
293
296
|
const className = typeof instance === "function"
|
|
294
297
|
? instance.name
|
|
295
298
|
: instance.constructor?.name;
|
|
296
299
|
const sourceTypes = className
|
|
297
300
|
? this.extractMethodParameterTypesFromProject(className, methodName)
|
|
298
301
|
: undefined;
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
+
const parameterTypes = sourceTypes ?? this.runtimeParameterTypes(methodName, method);
|
|
303
|
+
this.directRunParameterTypes.set(method, parameterTypes);
|
|
304
|
+
return parameterTypes;
|
|
305
|
+
}
|
|
306
|
+
runtimeParameterTypes(methodName, method) {
|
|
302
307
|
const runtimeNames = this.extractMethodParameterNamesFromFunctionSource(methodName, method);
|
|
303
308
|
if (!runtimeNames?.length) {
|
|
304
309
|
return "[]";
|
|
@@ -306,11 +311,7 @@ let NestGraphInspectorSetup = NestGraphInspectorSetup_1 = class NestGraphInspect
|
|
|
306
311
|
return `[${runtimeNames.map((name) => `${name}: unknown`).join(", ")}]`;
|
|
307
312
|
}
|
|
308
313
|
extractMethodParameterTypesFromProject(className, methodName) {
|
|
309
|
-
const
|
|
310
|
-
.getSourceFiles()
|
|
311
|
-
.flatMap((sourceFile) => sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.ClassDeclaration))
|
|
312
|
-
.find((classDeclaration) => classDeclaration.getName() === className);
|
|
313
|
-
const method = targetClass?.getInstanceMethod(methodName);
|
|
314
|
+
const method = this.sourceMetadata.getInstanceMethod(className, methodName);
|
|
314
315
|
if (!method) {
|
|
315
316
|
return undefined;
|
|
316
317
|
}
|
|
@@ -779,5 +780,6 @@ exports.NestGraphInspectorSetup = NestGraphInspectorSetup = NestGraphInspectorSe
|
|
|
779
780
|
file_output_adapter_1.FileOutputAdapter,
|
|
780
781
|
json_output_adapter_1.JsonOutputAdapter,
|
|
781
782
|
viewer_output_adapter_1.ViewerOutputAdapter,
|
|
782
|
-
runtime_trace_recorder_1.RuntimeTraceRecorder
|
|
783
|
+
runtime_trace_recorder_1.RuntimeTraceRecorder,
|
|
784
|
+
source_metadata_service_1.SourceMetadataService])
|
|
783
785
|
], NestGraphInspectorSetup);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { MethodDeclaration } from "ts-morph";
|
|
2
|
+
export declare class SourceMetadataService {
|
|
3
|
+
private readonly logger;
|
|
4
|
+
private project;
|
|
5
|
+
private classIndexes;
|
|
6
|
+
private readonly classJsDocCache;
|
|
7
|
+
private readonly methodCache;
|
|
8
|
+
getClassJsDoc(className: string): string | undefined;
|
|
9
|
+
getInstanceMethod(className: string, methodName: string): MethodDeclaration | undefined;
|
|
10
|
+
private getTopLevelClass;
|
|
11
|
+
private getClass;
|
|
12
|
+
private getClassIndexes;
|
|
13
|
+
private buildClassIndexes;
|
|
14
|
+
private getProject;
|
|
15
|
+
private createProject;
|
|
16
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
3
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
4
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
5
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
6
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
7
|
+
};
|
|
8
|
+
var SourceMetadataService_1;
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.SourceMetadataService = void 0;
|
|
11
|
+
const node_fs_1 = require("node:fs");
|
|
12
|
+
const node_path_1 = require("node:path");
|
|
13
|
+
const common_1 = require("@nestjs/common");
|
|
14
|
+
const ts_morph_1 = require("ts-morph");
|
|
15
|
+
let SourceMetadataService = SourceMetadataService_1 = class SourceMetadataService {
|
|
16
|
+
logger = new common_1.Logger(SourceMetadataService_1.name);
|
|
17
|
+
project;
|
|
18
|
+
classIndexes;
|
|
19
|
+
classJsDocCache = new Map();
|
|
20
|
+
methodCache = new Map();
|
|
21
|
+
getClassJsDoc(className) {
|
|
22
|
+
if (this.classJsDocCache.has(className)) {
|
|
23
|
+
return this.classJsDocCache.get(className);
|
|
24
|
+
}
|
|
25
|
+
const jsdoc = this.getTopLevelClass(className)
|
|
26
|
+
?.getJsDocs()
|
|
27
|
+
.map((doc) => doc.getCommentText())
|
|
28
|
+
.filter((comment) => !!comment)
|
|
29
|
+
.join("\n");
|
|
30
|
+
this.classJsDocCache.set(className, jsdoc);
|
|
31
|
+
return jsdoc;
|
|
32
|
+
}
|
|
33
|
+
getInstanceMethod(className, methodName) {
|
|
34
|
+
const key = `${className}\u0000${methodName}`;
|
|
35
|
+
if (this.methodCache.has(key)) {
|
|
36
|
+
return this.methodCache.get(key);
|
|
37
|
+
}
|
|
38
|
+
const method = this.getClass(className)?.getInstanceMethod(methodName);
|
|
39
|
+
this.methodCache.set(key, method);
|
|
40
|
+
return method;
|
|
41
|
+
}
|
|
42
|
+
getTopLevelClass(className) {
|
|
43
|
+
return this.getClassIndexes().topLevel.get(className);
|
|
44
|
+
}
|
|
45
|
+
getClass(className) {
|
|
46
|
+
return this.getClassIndexes().all.get(className);
|
|
47
|
+
}
|
|
48
|
+
getClassIndexes() {
|
|
49
|
+
if (!this.classIndexes) {
|
|
50
|
+
this.classIndexes = this.buildClassIndexes();
|
|
51
|
+
}
|
|
52
|
+
return this.classIndexes;
|
|
53
|
+
}
|
|
54
|
+
buildClassIndexes() {
|
|
55
|
+
const topLevel = new Map();
|
|
56
|
+
const all = new Map();
|
|
57
|
+
for (const sourceFile of this.getProject().getSourceFiles()) {
|
|
58
|
+
for (const classDeclaration of sourceFile.getClasses()) {
|
|
59
|
+
const name = classDeclaration.getName();
|
|
60
|
+
if (name && !topLevel.has(name)) {
|
|
61
|
+
topLevel.set(name, classDeclaration);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
for (const classDeclaration of sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.ClassDeclaration)) {
|
|
65
|
+
const name = classDeclaration.getName();
|
|
66
|
+
if (name && !all.has(name)) {
|
|
67
|
+
all.set(name, classDeclaration);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return { topLevel, all };
|
|
72
|
+
}
|
|
73
|
+
getProject() {
|
|
74
|
+
if (!this.project) {
|
|
75
|
+
this.project = this.createProject();
|
|
76
|
+
}
|
|
77
|
+
return this.project;
|
|
78
|
+
}
|
|
79
|
+
createProject() {
|
|
80
|
+
const tsConfigFilePath = (0, node_path_1.join)(process.cwd(), "tsconfig.json");
|
|
81
|
+
if (!(0, node_fs_1.existsSync)(tsConfigFilePath)) {
|
|
82
|
+
this.logger.warn(`Could not find tsconfig.json at ${tsConfigFilePath}; JSDoc metadata will be skipped.`);
|
|
83
|
+
return new ts_morph_1.Project();
|
|
84
|
+
}
|
|
85
|
+
return new ts_morph_1.Project({
|
|
86
|
+
tsConfigFilePath,
|
|
87
|
+
skipAddingFilesFromTsConfig: false,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
exports.SourceMetadataService = SourceMetadataService;
|
|
92
|
+
exports.SourceMetadataService = SourceMetadataService = SourceMetadataService_1 = __decorate([
|
|
93
|
+
(0, common_1.Injectable)()
|
|
94
|
+
], SourceMetadataService);
|