Merge pull request 'chore: fix-build' (#27) from chore/fix-build into main
Reviewed-on: #27
This commit was merged in pull request #27.
This commit is contained in:
10
main.ts
10
main.ts
@@ -2,13 +2,14 @@
|
|||||||
|
|
||||||
import fs from 'fs-extra';
|
import fs from 'fs-extra';
|
||||||
import mustache from 'mustache';
|
import mustache from 'mustache';
|
||||||
|
import path from 'path';
|
||||||
import { program } from 'commander';
|
import { program } from 'commander';
|
||||||
|
|
||||||
import { log, logSuccess, logInfo, logWarning, logError, colors } from './src/utils/logger';
|
import { log, logSuccess, logInfo, logWarning, logError, colors } from './src/utils/logger';
|
||||||
import { checkOpenApiGenerator, installOpenApiGenerator } from './src/utils/openapi-generator';
|
import { checkOpenApiGenerator, installOpenApiGenerator } from './src/utils/openapi-generator';
|
||||||
import { createDirectoryStructure, cleanup } from './src/utils/filesystem';
|
import { createDirectoryStructure, cleanup } from './src/utils/filesystem';
|
||||||
import { analyzeSwagger } from './src/swagger/analyzer';
|
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 { generateCleanArchitecture } from './src/generators/clean-arch.generator';
|
||||||
import { generateReport } from './src/generators/report.generator';
|
import { generateReport } from './src/generators/report.generator';
|
||||||
import type { CliOptions } from './src/types';
|
import type { CliOptions } from './src/types';
|
||||||
@@ -26,7 +27,11 @@ program
|
|||||||
.version('1.0.0')
|
.version('1.0.0')
|
||||||
.option('-i, --input <file>', 'Archivo OpenAPI/Swagger (yaml o json)', 'swagger.yaml')
|
.option('-i, --input <file>', 'Archivo OpenAPI/Swagger (yaml o json)', 'swagger.yaml')
|
||||||
.option('-o, --output <dir>', 'Directorio de salida', './src/app')
|
.option('-o, --output <dir>', 'Directorio de salida', './src/app')
|
||||||
.option('-t, --templates <dir>', 'Directorio de templates personalizados', './templates')
|
.option(
|
||||||
|
'-t, --templates <dir>',
|
||||||
|
'Directorio de templates personalizados',
|
||||||
|
path.join(__dirname, 'templates')
|
||||||
|
)
|
||||||
.option('--skip-install', 'No instalar dependencias')
|
.option('--skip-install', 'No instalar dependencias')
|
||||||
.option('--dry-run', 'Simular sin generar archivos')
|
.option('--dry-run', 'Simular sin generar archivos')
|
||||||
.parse(process.argv);
|
.parse(process.argv);
|
||||||
@@ -79,6 +84,7 @@ async function main(): Promise<void> {
|
|||||||
|
|
||||||
const tempDir = generateCode(options.input, options.templates);
|
const tempDir = generateCode(options.input, options.templates);
|
||||||
organizeFiles(tempDir, options.output);
|
organizeFiles(tempDir, options.output);
|
||||||
|
addDtoImports(options.output);
|
||||||
generateCleanArchitecture(analysis, options.output, options.templates);
|
generateCleanArchitecture(analysis, options.output, options.templates);
|
||||||
cleanup(tempDir);
|
cleanup(tempDir);
|
||||||
|
|
||||||
|
|||||||
@@ -2,14 +2,14 @@
|
|||||||
"name": "@blas/openapi-clean-arch-generator",
|
"name": "@blas/openapi-clean-arch-generator",
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"description": "Generador de código Angular con Clean Architecture desde OpenAPI/Swagger",
|
"description": "Generador de código Angular con Clean Architecture desde OpenAPI/Swagger",
|
||||||
"main": "dist/generate.js",
|
"main": "dist/main.js",
|
||||||
"bin": {
|
"bin": {
|
||||||
"generate-clean-arch": "./dist/generate.js"
|
"generate-clean-arch": "./dist/main.js"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc && cp -r templates dist/",
|
||||||
"prepublishOnly": "npm run build",
|
"prepublishOnly": "npm run build",
|
||||||
"generate": "node dist/generate.js",
|
"generate": "node dist/main.js",
|
||||||
"generate:dev": "ts-node main.ts",
|
"generate:dev": "ts-node main.ts",
|
||||||
"lint": "eslint 'main.ts' 'src/**/*.ts' -f unix",
|
"lint": "eslint 'main.ts' 'src/**/*.ts' -f unix",
|
||||||
"lint:fix": "eslint 'main.ts' 'src/**/*.ts' --fix -f unix",
|
"lint:fix": "eslint 'main.ts' 'src/**/*.ts' --fix -f unix",
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ export function generateCode(swaggerFile: string, templatesDir: string): string
|
|||||||
--global-property models \
|
--global-property models \
|
||||||
-t "${templatesDir}" \
|
-t "${templatesDir}" \
|
||||||
-o "${tempDir}" \
|
-o "${tempDir}" \
|
||||||
--additional-properties=ngVersion=17.0.0,modelFileSuffix=.dto`;
|
--additional-properties=ngVersion=17.0.0,modelFileSuffix=.dto,modelNameSuffix=Dto`;
|
||||||
|
|
||||||
execSync(command, { stdio: 'inherit' });
|
execSync(command, { stdio: 'inherit' });
|
||||||
logSuccess('Código generado correctamente');
|
logSuccess('Código generado correctamente');
|
||||||
@@ -60,3 +60,63 @@ export function organizeFiles(tempDir: string, outputDir: string): void {
|
|||||||
|
|
||||||
logSuccess(`${filesMoved} DTOs movidos correctamente`);
|
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}}
|
* {{#description}}{{description}}{{/description}}
|
||||||
* Generated from OpenAPI specification
|
* Generated from OpenAPI specification
|
||||||
*/
|
*/
|
||||||
export interface {{classname}}Dto {
|
export interface {{classname}} {
|
||||||
{{#vars}}
|
{{#vars}}
|
||||||
{{#description}}
|
{{#description}}
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user