- Added `clean-arch.generator.ts` for generating models, mappers, repositories, use cases, and providers based on OpenAPI specs. - Introduced `dto.generator.ts` to invoke `openapi-generator-cli` for generating DTOs and organizing them. - Created `report.generator.ts` to generate a JSON report of the generation process. - Implemented `analyzer.ts` for parsing OpenAPI/Swagger files and extracting relevant data. - Defined new types in `cli.types.ts`, `generation.types.ts`, `openapi.types.ts`, and `swagger.types.ts` for better type safety. - Added utility functions in `filesystem.ts` for creating directory structures and cleaning up temporary files. - Developed logging utilities in `logger.ts` for better console output. - Included OpenAPI generator checks and installation in `openapi-generator.ts`. - Added type mapping utility in `type-mapper.ts` for converting OpenAPI types to TypeScript types. - Updated `package.json` scripts to lint all TypeScript files. - Modified `tsconfig.json` to include all TypeScript files in the project.
63 lines
1.9 KiB
TypeScript
63 lines
1.9 KiB
TypeScript
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`);
|
|
}
|