* Delete all files and directories inside the workspace * @param workspace - The workspace structure * @param preserve - Optional list of relative paths (from workspace root) to preserve from deletion
(workspace: WorkspaceStructure, preserve: string[] = [])
| 53 | * @param preserve - Optional list of relative paths (from workspace root) to preserve from deletion |
| 54 | */ |
| 55 | deleteWorkspace(workspace: WorkspaceStructure, preserve: string[] = []): void { |
| 56 | try { |
| 57 | if (!fs.existsSync(workspace.root)) return; |
| 58 | |
| 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); |
| 74 | for (const entry of entries) { |
| 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 }); |
| 87 | } |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | deleteRecursive(workspace.root); |
| 92 | } catch (error) { |
| 93 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 94 | logger.printWarnLog(`Failed to delete workspace: ${errorMessage}`); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Resolve the absolute path to the source code directory |
no outgoing calls
no test coverage detected