@@ -1055,24 +1055,20 @@ export async function generateODataTypes(
10551055 const resolvedOutputPath = resolve ( cwd , outputPath ) ;
10561056 await mkdir ( resolvedOutputPath , { recursive : true } ) ;
10571057
1058- if ( clearOldFiles ) {
1059- // Clear the directory if requested (but keep the directory itself)
1060- fs . emptyDirSync ( resolvedOutputPath ) ;
1061- }
1062-
10631058 // Create ts-morph project for file manipulation
10641059 const project = new Project ( { } ) ;
10651060
10661061 // Generate one file per table occurrence
10671062 const exportStatements : string [ ] = [ ] ;
1063+ const keptFiles = new Set < string > ( ) ;
10681064
10691065 for ( const generated of generatedTOs ) {
10701066 const fileName = `${ sanitizeFileName ( generated . varName ) } .ts` ;
10711067 const filePath = join ( resolvedOutputPath , fileName ) ;
10721068
10731069 // Check if file exists and parse it
10741070 let existingFields : ParsedTableOccurrence | undefined ;
1075- if ( fs . existsSync ( filePath ) && ! clearOldFiles ) {
1071+ if ( fs . existsSync ( filePath ) ) {
10761072 try {
10771073 const existingSourceFile = project . addSourceFileAtPath ( filePath ) ;
10781074 const parsed = parseExistingTableFile ( existingSourceFile ) ;
@@ -1389,6 +1385,7 @@ export async function generateODataTypes(
13891385 project . createSourceFile ( filePath , fileContent , {
13901386 overwrite : true ,
13911387 } ) ;
1388+ keptFiles . add ( filePath ) ;
13921389
13931390 // Collect export statement for index file
13941391 exportStatements . push ( `export { ${ regenerated . varName } } from "./${ sanitizeFileName ( regenerated . varName ) } ";` ) ;
@@ -1406,7 +1403,32 @@ ${exportStatements.join("\n")}
14061403 project . createSourceFile ( indexPath , indexContent , {
14071404 overwrite : true ,
14081405 } ) ;
1406+ keptFiles . add ( indexPath ) ;
14091407
14101408 // Format and save files
14111409 await formatAndSaveSourceFiles ( project , cwd ) ;
1410+
1411+ if ( clearOldFiles ) {
1412+ // For fmodata generation, preserve customizations by merging first,
1413+ // then remove files/directories that were not regenerated this run.
1414+ const keepSet = new Set ( Array . from ( keptFiles ) . map ( ( p ) => resolve ( p ) ) ) ;
1415+ const deleteStaleEntries = ( dirPath : string ) : void => {
1416+ const entries = fs . readdirSync ( dirPath , { withFileTypes : true } ) ;
1417+ for ( const entry of entries ) {
1418+ const entryPath = resolve ( dirPath , entry . name ) ;
1419+ if ( entry . isDirectory ( ) ) {
1420+ deleteStaleEntries ( entryPath ) ;
1421+ const remainingEntries = fs . readdirSync ( entryPath ) ;
1422+ if ( remainingEntries . length === 0 && ! keepSet . has ( entryPath ) ) {
1423+ fs . removeSync ( entryPath ) ;
1424+ }
1425+ continue ;
1426+ }
1427+ if ( ! keepSet . has ( entryPath ) ) {
1428+ fs . removeSync ( entryPath ) ;
1429+ }
1430+ }
1431+ } ;
1432+ deleteStaleEntries ( resolvedOutputPath ) ;
1433+ }
14121434}
0 commit comments