@@ -50,23 +50,45 @@ class Workspace {
5050 /**
5151 * Delete all files and directories inside the workspace
5252 * @param workspace - The workspace structure
53- * @param exclude - Optional list of file/directory names to exclude from deletion
53+ * @param preserve - Optional list of relative paths (from workspace root) to preserve from deletion
5454 */
55- deleteWorkspace ( workspace : WorkspaceStructure , exclude : string [ ] = [ ] ) : void {
55+ deleteWorkspace ( workspace : WorkspaceStructure , preserve : string [ ] = [ ] ) : void {
5656 try {
57- if ( fs . existsSync ( workspace . root ) ) {
58- // Read all entries in the workspace root
59- const entries = fs . readdirSync ( workspace . root ) ;
57+ if ( ! fs . existsSync ( workspace . root ) ) return ;
6058
61- // Delete each entry
59+ // Build set of normalized relative paths to preserve
60+ const preserveFiles = new Set ( preserve . map ( p => path . normalize ( p ) ) ) ;
61+
62+ // Collect ancestor directories of preserved files
63+ const preserveDirs = new Set < string > ( ) ;
64+ for ( const p of preserveFiles ) {
65+ let dir = path . dirname ( p ) ;
66+ while ( dir !== '.' ) {
67+ preserveDirs . add ( dir ) ;
68+ dir = path . dirname ( dir ) ;
69+ }
70+ }
71+
72+ const deleteRecursive = ( dirPath : string , relativeTo : string = '' ) => {
73+ const entries = fs . readdirSync ( dirPath ) ;
6274 for ( const entry of entries ) {
63- if ( exclude . includes ( entry ) ) {
64- continue ;
75+ const fullPath = path . join ( dirPath , entry ) ;
76+ const relPath = relativeTo ? path . join ( relativeTo , entry ) : entry ;
77+
78+ if ( preserveFiles . has ( relPath ) ) {
79+ continue ; // This file is preserved
80+ }
81+
82+ if ( preserveDirs . has ( relPath ) ) {
83+ // Directory contains preserved files, recurse into it
84+ deleteRecursive ( fullPath , relPath ) ;
85+ } else {
86+ fs . rmSync ( fullPath , { recursive : true , force : true } ) ;
6587 }
66- const fullPath = path . join ( workspace . root , entry ) ;
67- fs . rmSync ( fullPath , { recursive : true , force : true } ) ;
6888 }
69- }
89+ } ;
90+
91+ deleteRecursive ( workspace . root ) ;
7092 } catch ( error ) {
7193 const errorMessage = error instanceof Error ? error . message : String ( error ) ;
7294 logger . printWarnLog ( `Failed to delete workspace: ${ errorMessage } ` ) ;
0 commit comments