refactor: normalize array type notation from Array<T> to T[] in generators and type mapper
This commit is contained in:
@@ -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}`);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user