feat: Implement Clean Architecture code generation with Mustache templates
- 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.
This commit is contained in:
31
src/utils/filesystem.ts
Normal file
31
src/utils/filesystem.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import { logSuccess, logInfo } from './logger';
|
||||
|
||||
/** Crea la estructura de directorios necesaria para Clean Architecture (idempotente). */
|
||||
export function createDirectoryStructure(baseDir: string): void {
|
||||
const dirs = [
|
||||
path.join(baseDir, 'data/dtos'),
|
||||
path.join(baseDir, 'data/repositories'),
|
||||
path.join(baseDir, 'data/mappers'),
|
||||
path.join(baseDir, 'domain/repositories'),
|
||||
path.join(baseDir, 'domain/use-cases'),
|
||||
path.join(baseDir, 'di/repositories'),
|
||||
path.join(baseDir, 'di/use-cases'),
|
||||
path.join(baseDir, 'entities/models')
|
||||
];
|
||||
|
||||
dirs.forEach((dir) => {
|
||||
fs.ensureDirSync(dir);
|
||||
});
|
||||
|
||||
logSuccess('Estructura de directorios creada');
|
||||
}
|
||||
|
||||
/** Elimina un directorio temporal y todo su contenido. */
|
||||
export function cleanup(tempDir: string): void {
|
||||
if (fs.existsSync(tempDir)) {
|
||||
fs.removeSync(tempDir);
|
||||
logInfo('Archivos temporales eliminados');
|
||||
}
|
||||
}
|
||||
43
src/utils/logger.ts
Normal file
43
src/utils/logger.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
const colors = {
|
||||
reset: '\x1b[0m',
|
||||
bright: '\x1b[1m',
|
||||
green: '\x1b[32m',
|
||||
blue: '\x1b[34m',
|
||||
yellow: '\x1b[33m',
|
||||
red: '\x1b[31m',
|
||||
cyan: '\x1b[36m'
|
||||
} as const;
|
||||
|
||||
type Color = keyof typeof colors;
|
||||
|
||||
/** Imprime un mensaje en consola con el color ANSI indicado. */
|
||||
export function log(message: string, color: Color = 'reset'): void {
|
||||
console.log(`${colors[color]}${message}${colors.reset}`);
|
||||
}
|
||||
|
||||
/** Imprime un mensaje de éxito (verde). */
|
||||
export function logSuccess(message: string): void {
|
||||
log(`✅ ${message}`, 'green');
|
||||
}
|
||||
|
||||
/** Imprime un mensaje informativo (azul). */
|
||||
export function logInfo(message: string): void {
|
||||
log(`ℹ️ ${message}`, 'blue');
|
||||
}
|
||||
|
||||
/** Imprime un mensaje de advertencia (amarillo). */
|
||||
export function logWarning(message: string): void {
|
||||
log(`⚠️ ${message}`, 'yellow');
|
||||
}
|
||||
|
||||
/** Imprime un mensaje de error (rojo). */
|
||||
export function logError(message: string): void {
|
||||
log(`❌ ${message}`, 'red');
|
||||
}
|
||||
|
||||
/** Imprime un encabezado de paso/etapa (cian). */
|
||||
export function logStep(message: string): void {
|
||||
log(`\n🚀 ${message}`, 'cyan');
|
||||
}
|
||||
|
||||
export { colors };
|
||||
24
src/utils/openapi-generator.ts
Normal file
24
src/utils/openapi-generator.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { execSync } from 'child_process';
|
||||
import { logStep, logSuccess, logError } from './logger';
|
||||
|
||||
/** Verifica si `openapi-generator-cli` está disponible en el PATH. */
|
||||
export function checkOpenApiGenerator(): boolean {
|
||||
try {
|
||||
execSync('openapi-generator-cli version', { stdio: 'ignore' });
|
||||
return true;
|
||||
} catch (_error) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** Instala `@openapitools/openapi-generator-cli` de forma global vía npm. */
|
||||
export function installOpenApiGenerator(): void {
|
||||
logStep('Instalando @openapitools/openapi-generator-cli...');
|
||||
try {
|
||||
execSync('npm install -g @openapitools/openapi-generator-cli', { stdio: 'inherit' });
|
||||
logSuccess('OpenAPI Generator CLI instalado correctamente');
|
||||
} catch (_error) {
|
||||
logError('Error al instalar OpenAPI Generator CLI');
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
14
src/utils/type-mapper.ts
Normal file
14
src/utils/type-mapper.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/** Traduce un tipo primitivo de OpenAPI/Swagger al equivalente TypeScript. */
|
||||
export function mapSwaggerTypeToTs(type?: string): string {
|
||||
if (!type) return 'unknown';
|
||||
|
||||
const typeMap: Record<string, string> = {
|
||||
integer: 'number',
|
||||
string: 'string',
|
||||
boolean: 'boolean',
|
||||
number: 'number',
|
||||
array: 'Array<unknown>',
|
||||
object: 'unknown'
|
||||
};
|
||||
return typeMap[type] || 'unknown';
|
||||
}
|
||||
Reference in New Issue
Block a user