import { execSync } from 'child_process'; import fs from 'fs-extra'; import path from 'path'; import { logStep, logSuccess, logError, logInfo } from '../utils/logger'; /** Invoca `openapi-generator-cli` para generar DTOs en un directorio temporal. */ export function generateCode(swaggerFile: string, templatesDir: string): string { logStep('Generando código desde OpenAPI...'); const tempDir = path.join(process.cwd(), '.temp-generated'); if (fs.existsSync(tempDir)) { fs.removeSync(tempDir); } try { const command = `openapi-generator-cli generate \ -i "${swaggerFile}" \ -g typescript-angular \ --global-property models \ -t "${templatesDir}" \ -o "${tempDir}" \ --additional-properties=ngVersion=17.0.0,modelFileSuffix=.dto`; execSync(command, { stdio: 'inherit' }); logSuccess('Código generado correctamente'); return tempDir; } catch (_error) { logError('Error al generar código'); if (fs.existsSync(tempDir)) { fs.removeSync(tempDir); } process.exit(1); } } /** Copia los DTOs generados desde el directorio temporal al directorio de salida. */ export function organizeFiles(tempDir: string, outputDir: string): void { logStep('Organizando archivos DTO generados...'); const sourceDir = path.join(tempDir, 'model'); const destDir = path.join(outputDir, 'data/dtos'); let filesMoved = 0; if (fs.existsSync(sourceDir)) { fs.ensureDirSync(destDir); const files = fs.readdirSync(sourceDir).filter((file) => file.endsWith('.dto.ts')); files.forEach((file) => { const sourcePath = path.join(sourceDir, file); const destPath = path.join(destDir, file); fs.copySync(sourcePath, destPath); filesMoved++; logInfo(` ${file} → ${path.relative(process.cwd(), destPath)}`); }); } logSuccess(`${filesMoved} DTOs movidos correctamente`); }