refactor: normalize array type notation from Array<T> to T[] in generators and type mapper

This commit is contained in:
didavila
2026-03-24 16:27:10 +01:00
parent 73dcb6f701
commit e008144813
3 changed files with 15 additions and 10 deletions

View File

@@ -61,7 +61,7 @@ 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. */
/** Post-procesa los DTOs generados añadiendo imports y normalizando Array<T> → T[]. */
export function addDtoImports(outputDir: string): void {
logStep('Añadiendo imports a los DTOs generados...');
@@ -85,11 +85,15 @@ export function addDtoImports(outputDir: string): void {
files.forEach((file) => {
const filePath = path.join(dtosDir, file);
let content = fs.readFileSync(filePath, 'utf8');
const originalContent = fs.readFileSync(filePath, 'utf8');
let content = originalContent;
const selfMatch = content.match(/export interface (\w+)/);
const selfName = selfMatch ? selfMatch[1] : '';
// Normalize Array<T> → T[] (openapi-generator-cli always outputs Array<T>)
content = content.replace(/Array<(\w+)>/g, '$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;
@@ -100,8 +104,6 @@ export function addDtoImports(outputDir: string): void {
}
}
if (references.size === 0) return;
// Build import lines for each referenced type that exists in the dtoMap
const imports: string[] = [];
references.forEach((ref) => {
@@ -112,9 +114,12 @@ export function addDtoImports(outputDir: string): void {
if (imports.length > 0) {
content = imports.join('\n') + '\n' + content;
}
if (content !== originalContent) {
fs.writeFileSync(filePath, content);
filesProcessed++;
logInfo(` Imports añadidos a ${file}`);
logInfo(` Procesado ${file}`);
}
});