feat: enhance DTO generation and organization by tag

This commit is contained in:
2026-03-27 14:34:30 +01:00
parent 603feda26d
commit 2db6e95b1c
19 changed files with 411 additions and 256 deletions

View File

@@ -2,6 +2,7 @@ import { execSync } from 'child_process';
import fs from 'fs-extra';
import path from 'path';
import { logStep, logSuccess, logError, logDetail } from '../utils/logger';
import { toPascalCase } from '../utils/name-formatter';
/** Invokes `openapi-generator-cli` to generate DTOs into a temporary directory. */
export function generateCode(swaggerFile: string, templatesDir: string): string {
@@ -35,8 +36,12 @@ export function generateCode(swaggerFile: string, templatesDir: string): string
}
}
/** Copies the generated DTOs from the temporary directory to the output directory. */
export function organizeFiles(tempDir: string, outputDir: string): void {
/** Copies the generated DTOs from the temporary directory to the output directory, organised by tag subfolder. */
export function organizeFiles(
tempDir: string,
outputDir: string,
schemaTagMap: Record<string, string> = {}
): void {
logStep('Organising generated DTO files...');
const sourceDir = path.join(tempDir, 'model');
@@ -49,8 +54,14 @@ export function organizeFiles(tempDir: string, outputDir: string): void {
const files = fs.readdirSync(sourceDir).filter((file) => file.endsWith('.dto.ts'));
files.forEach((file) => {
// file is like "userResponse.dto.ts" → derive PascalCase schema name to look up tag
const camelName = file.replace('.dto.ts', '');
const pascalName = toPascalCase(camelName);
const tagFolder = schemaTagMap[pascalName] || 'shared';
const sourcePath = path.join(sourceDir, file);
const destPath = path.join(destDir, file);
const destPath = path.join(destDir, tagFolder, file);
fs.ensureDirSync(path.dirname(destPath));
fs.copySync(sourcePath, destPath);
filesMoved++;
logDetail('dto', `${file}${path.relative(process.cwd(), destPath)}`);
@@ -65,58 +76,69 @@ export function addDtoImports(outputDir: string): void {
logStep('Post-processing generated DTOs...');
const dtosDir = path.join(outputDir, 'data/dtos');
if (!fs.existsSync(dtosDir)) return;
const files = fs.readdirSync(dtosDir).filter((f) => f.endsWith('.dto.ts'));
// Collect all .dto.ts files from all subfolders (1 level deep)
const allFiles: { subfolder: string; file: string; fullPath: string }[] = [];
// 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', '');
const entries = fs.readdirSync(dtosDir);
entries.forEach((entry) => {
const entryPath = path.join(dtosDir, entry);
if (fs.statSync(entryPath).isDirectory()) {
fs.readdirSync(entryPath)
.filter((f) => f.endsWith('.dto.ts'))
.forEach((file) =>
allFiles.push({ subfolder: entry, file, fullPath: path.join(entryPath, file) })
);
} else if (entry.endsWith('.dto.ts')) {
allFiles.push({ subfolder: '', file: entry, fullPath: entryPath });
}
});
// Build map: ClassName → { subfolder, fileBase }
const dtoMap: Record<string, { subfolder: string; fileBase: string }> = {};
allFiles.forEach(({ subfolder, file, fullPath }) => {
const content = fs.readFileSync(fullPath, 'utf8');
const match = content.match(/export interface (\w+)/);
if (match) dtoMap[match[1]] = { subfolder, fileBase: file.replace('.ts', '') };
});
let filesProcessed = 0;
files.forEach((file) => {
const filePath = path.join(dtosDir, file);
const originalContent = fs.readFileSync(filePath, 'utf8');
allFiles.forEach(({ subfolder, file, fullPath }) => {
const originalContent = fs.readFileSync(fullPath, '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;
let match;
while ((match = typeRegex.exec(content)) !== null) {
if (match[1] !== selfName) {
references.add(match[1]);
}
if (match[1] !== selfName) references.add(match[1]);
}
// 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]}';`);
const { subfolder: refSubfolder, fileBase: refFileBase } = dtoMap[ref];
const fromDir = subfolder ? path.join(dtosDir, subfolder) : dtosDir;
const toFile = refSubfolder
? path.join(dtosDir, refSubfolder, refFileBase)
: path.join(dtosDir, refFileBase);
let relPath = path.relative(fromDir, toFile).replace(/\\/g, '/');
if (!relPath.startsWith('.')) relPath = './' + relPath;
imports.push(`import { ${ref} } from '${relPath}';`);
}
});
if (imports.length > 0) {
content = imports.join('\n') + '\n' + content;
}
if (imports.length > 0) content = imports.join('\n') + '\n' + content;
if (content !== originalContent) {
fs.writeFileSync(filePath, content);
fs.writeFileSync(fullPath, content);
filesProcessed++;
logDetail('dto', `Post-processed ${file} (added ${imports.length} import(s))`);
}