feat: Add DTO imports processing and update model template for cleaner architecture
This commit is contained in:
3
main.ts
3
main.ts
@@ -8,7 +8,7 @@ import { log, logSuccess, logInfo, logWarning, logError, colors } from './src/ut
|
||||
import { checkOpenApiGenerator, installOpenApiGenerator } from './src/utils/openapi-generator';
|
||||
import { createDirectoryStructure, cleanup } from './src/utils/filesystem';
|
||||
import { analyzeSwagger } from './src/swagger/analyzer';
|
||||
import { generateCode, organizeFiles } from './src/generators/dto.generator';
|
||||
import { generateCode, organizeFiles, addDtoImports } from './src/generators/dto.generator';
|
||||
import { generateCleanArchitecture } from './src/generators/clean-arch.generator';
|
||||
import { generateReport } from './src/generators/report.generator';
|
||||
import type { CliOptions } from './src/types';
|
||||
@@ -79,6 +79,7 @@ async function main(): Promise<void> {
|
||||
|
||||
const tempDir = generateCode(options.input, options.templates);
|
||||
organizeFiles(tempDir, options.output);
|
||||
addDtoImports(options.output);
|
||||
generateCleanArchitecture(analysis, options.output, options.templates);
|
||||
cleanup(tempDir);
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@ export function generateCode(swaggerFile: string, templatesDir: string): string
|
||||
--global-property models \
|
||||
-t "${templatesDir}" \
|
||||
-o "${tempDir}" \
|
||||
--additional-properties=ngVersion=17.0.0,modelFileSuffix=.dto`;
|
||||
--additional-properties=ngVersion=17.0.0,modelFileSuffix=.dto,modelNameSuffix=Dto`;
|
||||
|
||||
execSync(command, { stdio: 'inherit' });
|
||||
logSuccess('Código generado correctamente');
|
||||
@@ -60,3 +60,63 @@ export function organizeFiles(tempDir: string, outputDir: string): void {
|
||||
|
||||
logSuccess(`${filesMoved} DTOs movidos correctamente`);
|
||||
}
|
||||
|
||||
/** Post-procesa los DTOs generados añadiendo los imports de tipos referenciados. */
|
||||
export function addDtoImports(outputDir: string): void {
|
||||
logStep('Añadiendo imports a los DTOs generados...');
|
||||
|
||||
const dtosDir = path.join(outputDir, 'data/dtos');
|
||||
|
||||
if (!fs.existsSync(dtosDir)) return;
|
||||
|
||||
const files = fs.readdirSync(dtosDir).filter((f) => f.endsWith('.dto.ts'));
|
||||
|
||||
// Build a map of DTO classname → file base name (without .ts)
|
||||
const dtoMap: Record<string, string> = {};
|
||||
files.forEach((file) => {
|
||||
const content = fs.readFileSync(path.join(dtosDir, file), 'utf8');
|
||||
const match = content.match(/export interface (\w+)/);
|
||||
if (match) {
|
||||
dtoMap[match[1]] = file.replace('.ts', '');
|
||||
}
|
||||
});
|
||||
|
||||
let filesProcessed = 0;
|
||||
|
||||
files.forEach((file) => {
|
||||
const filePath = path.join(dtosDir, file);
|
||||
let content = fs.readFileSync(filePath, 'utf8');
|
||||
|
||||
const selfMatch = content.match(/export interface (\w+)/);
|
||||
const selfName = selfMatch ? selfMatch[1] : '';
|
||||
|
||||
// Find all Dto type references in the file body (excluding the interface name itself)
|
||||
const references = new Set<string>();
|
||||
const typeRegex = /\b(\w+Dto)\b/g;
|
||||
let match;
|
||||
while ((match = typeRegex.exec(content)) !== null) {
|
||||
if (match[1] !== selfName) {
|
||||
references.add(match[1]);
|
||||
}
|
||||
}
|
||||
|
||||
if (references.size === 0) return;
|
||||
|
||||
// Build import lines for each referenced type that exists in the dtoMap
|
||||
const imports: string[] = [];
|
||||
references.forEach((ref) => {
|
||||
if (dtoMap[ref]) {
|
||||
imports.push(`import { ${ref} } from './${dtoMap[ref]}';`);
|
||||
}
|
||||
});
|
||||
|
||||
if (imports.length > 0) {
|
||||
content = imports.join('\n') + '\n' + content;
|
||||
fs.writeFileSync(filePath, content);
|
||||
filesProcessed++;
|
||||
logInfo(` Imports añadidos a ${file}`);
|
||||
}
|
||||
});
|
||||
|
||||
logSuccess(`Imports añadidos a ${filesProcessed} DTOs`);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
* {{#description}}{{description}}{{/description}}
|
||||
* Generated from OpenAPI specification
|
||||
*/
|
||||
export interface {{classname}}Dto {
|
||||
export interface {{classname}} {
|
||||
{{#vars}}
|
||||
{{#description}}
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user