diff --git a/main.ts b/main.ts index d44197f..6379041 100755 --- a/main.ts +++ b/main.ts @@ -2,13 +2,14 @@ import fs from 'fs-extra'; import mustache from 'mustache'; +import path from 'path'; import { program } from 'commander'; import { log, logSuccess, logInfo, logWarning, logError, colors } from './src/utils/logger'; 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'; @@ -26,7 +27,11 @@ program .version('1.0.0') .option('-i, --input ', 'Archivo OpenAPI/Swagger (yaml o json)', 'swagger.yaml') .option('-o, --output ', 'Directorio de salida', './src/app') - .option('-t, --templates ', 'Directorio de templates personalizados', './templates') + .option( + '-t, --templates ', + 'Directorio de templates personalizados', + path.join(__dirname, 'templates') + ) .option('--skip-install', 'No instalar dependencias') .option('--dry-run', 'Simular sin generar archivos') .parse(process.argv); @@ -79,6 +84,7 @@ async function main(): Promise { const tempDir = generateCode(options.input, options.templates); organizeFiles(tempDir, options.output); + addDtoImports(options.output); generateCleanArchitecture(analysis, options.output, options.templates); cleanup(tempDir); diff --git a/package.json b/package.json index 8b082aa..ae06e6d 100644 --- a/package.json +++ b/package.json @@ -2,14 +2,14 @@ "name": "@blas/openapi-clean-arch-generator", "version": "1.0.0", "description": "Generador de código Angular con Clean Architecture desde OpenAPI/Swagger", - "main": "dist/generate.js", + "main": "dist/main.js", "bin": { - "generate-clean-arch": "./dist/generate.js" + "generate-clean-arch": "./dist/main.js" }, "scripts": { - "build": "tsc", + "build": "tsc && cp -r templates dist/", "prepublishOnly": "npm run build", - "generate": "node dist/generate.js", + "generate": "node dist/main.js", "generate:dev": "ts-node main.ts", "lint": "eslint 'main.ts' 'src/**/*.ts' -f unix", "lint:fix": "eslint 'main.ts' 'src/**/*.ts' --fix -f unix", diff --git a/src/generators/dto.generator.ts b/src/generators/dto.generator.ts index fce38e2..d52d18d 100644 --- a/src/generators/dto.generator.ts +++ b/src/generators/dto.generator.ts @@ -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 = {}; + 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(); + 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`); +} diff --git a/templates/model.mustache b/templates/model.mustache index d9ee6a6..8139b1f 100644 --- a/templates/model.mustache +++ b/templates/model.mustache @@ -5,7 +5,7 @@ * {{#description}}{{description}}{{/description}} * Generated from OpenAPI specification */ -export interface {{classname}}Dto { +export interface {{classname}} { {{#vars}} {{#description}} /**