UNPKG

@weverson_na/prisma-generator-nestjs-dto

Version:

Advanced Prisma Generator with Smart Merge v2: Creates DTO and Entity classes with AST-based preservation, intelligent import management, and modular architecture for NestJS

122 lines (121 loc) 5.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MergeContent = void 0; const ts_morph_1 = require("ts-morph"); const decorator_strategy_1 = require("./generator/decorator-strategy"); class MergeContent { constructor() { this.decoratorStrategy = new decorator_strategy_1.DecoratorStrategy(); } merge(existingText, generatedText) { const project = new ts_morph_1.Project({ useInMemoryFileSystem: true }); const existsFile = project.createSourceFile('exists.ts', existingText); const genFile = project.createSourceFile('gen.ts', generatedText); for (const genCls of genFile.getClasses()) { this.mergeClass(existsFile, genCls); } for (const imp of genFile.getImportDeclarations()) { this.ensureImportsFromDeclaration(existsFile, imp); } return existsFile.getFullText(); } mergeClass(file, genCls) { const name = genCls.getName() || ''; const existCls = file.getClass(name); if (!existCls) { file.addStatements(genCls.getText()); return; } this.mergeProperties(existCls, genCls); } addDecorators(existProp, genProp) { var _a; const genDecorators = genProp.getDecorators(); const existDecorators = existProp.getDecorators().map((d) => d.getName()); for (const genDec of genDecorators) { const name = genDec.getName(); const valid = this.decoratorStrategy.verifyIfDecoratorIsValid(name); if (!valid) continue; if (existDecorators.includes(name)) { (_a = existProp.getDecorator(name)) === null || _a === void 0 ? void 0 : _a.remove(); } existProp.addDecorator({ name, arguments: genDec.getArguments().map((a) => a.getText()), }); const getValidatorAndImports = this.decoratorStrategy.getValidatorAndImports(name); if (!(getValidatorAndImports === null || getValidatorAndImports === void 0 ? void 0 : getValidatorAndImports.importPath)) continue; this.ensureImport(existProp.getSourceFile(), name, getValidatorAndImports.importPath); } } mergeProperties(existCls, genCls) { const genProps = genCls.getProperties(); const genNames = genProps.map((p) => p.getName()); for (const genProp of genProps) { const name = genProp.getName(); const existProp = existCls.getProperty(name); if (!existProp) { existCls.addMember(genProp.getText()); const addedProp = existCls.getProperty(name); if (!addedProp) continue; this.addDecorators(addedProp, genProp); } else { this.updatePropertyType(existProp, genProp); this.addDecorators(existProp, genProp); } } for (const existProp of existCls.getProperties()) { const name = existProp.getName(); if (!genNames.includes(name)) { for (const dec of existProp.getDecorators()) { this.removeImport(existCls.getSourceFile(), dec.getName(), '@nestjs/swagger'); } existProp.remove(); } } } updatePropertyType(existProp, genProp) { var _a, _b; const oldType = (_a = existProp.getTypeNode()) === null || _a === void 0 ? void 0 : _a.getText(); const newType = (_b = genProp.getTypeNode()) === null || _b === void 0 ? void 0 : _b.getText(); if (newType && oldType !== newType) { existProp.setType(newType); } } ensureImportsFromDeclaration(file, imp) { const moduleSpecifier = imp.getModuleSpecifierValue(); for (const named of imp.getNamedImports()) { this.ensureImport(file, named.getName(), moduleSpecifier); } } ensureImport(file, named, moduleSpecifier) { const imp = file.getImportDeclaration((d) => d.getModuleSpecifierValue() === moduleSpecifier); if (!imp) { file.addImportDeclaration({ namedImports: [named], moduleSpecifier }); } else if (!imp.getNamedImports().some((n) => n.getName() === named)) { imp.addNamedImport(named); } } removeImport(file, named, moduleSpecifier) { const imp = file.getImportDeclaration((d) => d.getModuleSpecifierValue() === moduleSpecifier); if (!imp) return; const stillUsed = file .getDescendantsOfKind(ts_morph_1.SyntaxKind.Decorator) .some((dec) => dec.getName() === named); if (stillUsed) return; imp.getNamedImports().forEach((n) => { if (n.getName() === named) n.remove(); }); if (imp.getNamedImports().length === 0) imp.remove(); } } exports.MergeContent = MergeContent;