@methodus/codex
Version:
Code visualization tool
124 lines • 5.83 kB
JavaScript
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
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;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MapperService = void 0;
const ts_morph_1 = require("ts-morph");
const common_1 = require("@nestjs/common");
let MapperService = class MapperService {
constructor() {
this.modulesMap = {};
this.controllersMap = {};
this.providersMap = {};
this.relationsMap = {};
}
buildMap(scanPath) {
this.scanPath = scanPath;
this.mapResources();
this.addParameters();
return {
relations: this.relationsMap,
modules: this.modulesMap,
controllers: this.controllersMap,
providers: this.providersMap,
};
}
addParameters() {
var _a, _b;
for (const sourceFile of this.getSources()) {
for (const classDeclaration of sourceFile.getClasses()) {
const className = (_a = classDeclaration.getName()) !== null && _a !== void 0 ? _a : 'unknown';
this.handleRelationsByDeclarations(sourceFile, className);
for (const constructorDeclaration of classDeclaration.getConstructors()) {
for (const parameter of constructorDeclaration.getParameters()) {
const parameterKey = (_b = parameter.getType().compilerType.getSymbol()) === null || _b === void 0 ? void 0 : _b.getName();
this.relationsMap[className] = this.relationsMap[className] || {};
this.relationsMap[className][parameterKey] = { name: parameterKey };
const instance = this.modulesMap[className] ||
this.controllersMap[className] ||
this.providersMap[className];
if (instance) {
instance.parameters[parameterKey] = { name: parameter.getName(), type: parameterKey };
}
}
}
}
}
}
mapResources() {
var _a;
for (const sourceFile of this.getSources()) {
//.slice(0, 30)
for (const classDeclaration of sourceFile.getClasses()) {
const className = (_a = classDeclaration.getName()) !== null && _a !== void 0 ? _a : 'unknown';
for (const decorator of classDeclaration.getDecorators()) {
switch (decorator.getName()) {
case 'Module':
this.modulesMap[className] = { name: className, parameters: {}, class: classDeclaration };
break;
case 'Controller':
this.controllersMap[className] = { name: className, parameters: {} };
break;
case 'Injectable':
this.providersMap[className] = { name: className, parameters: {} };
break;
}
}
}
}
}
handleRelationsByDeclarations(sourceFile, className) {
for (const importDeclaration of sourceFile.getImportDeclarations()) {
for (const namedImport of importDeclaration.getNamedImports()) {
const nameKey = namedImport.getName();
if (this.controllersMap[nameKey] || this.providersMap[nameKey]) {
this.relationsMap[className] = this.relationsMap[className] || {};
this.relationsMap[className][nameKey] = { name: nameKey };
}
}
}
}
buildModulesMap() {
const siteMap = {};
this.getSources()
//.slice(0, 30)
.forEach((sourceFile) => {
sourceFile.getClasses().forEach((classDeclaration) => {
var _a;
const className = (_a = classDeclaration.getName()) !== null && _a !== void 0 ? _a : 'unknown';
const scopedClass = (siteMap[className] = {
type: 'class',
name: className,
properties: [],
methods: [],
parameters: [],
decorators: [],
});
classDeclaration.getConstructors().forEach((constructorDeclaration) => {
scopedClass.parameters = constructorDeclaration
.getParameters()
.map((p) => ({ name: p.getName() }));
});
scopedClass.properties = classDeclaration.getProperties().map((p) => ({ name: p.getName() }));
scopedClass.methods = classDeclaration.getMethods().map((p) => ({ name: p.getName() }));
});
});
}
getSources() {
const project = new ts_morph_1.Project({
tsConfigFilePath: this.scanPath + '/tsconfig.json',
});
// add source files
// project.addSourceFilesAtPaths('src/**/*.ts');
return project.getSourceFiles();
}
};
exports.MapperService = MapperService;
exports.MapperService = MapperService = __decorate([
(0, common_1.Injectable)()
], MapperService);
//# sourceMappingURL=mapper.service.js.map