Skip to content

Commit

Permalink
Fix: make multiple passes to delete empty directories (#1197)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Jul 4, 2024
1 parent 81c98e8 commit b0e1011
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/modules/directoryCleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,14 @@ export default class DirectoryCleaner extends Module {
}

try {
const emptyDirs = await DirectoryCleaner.getEmptyDirs(dirsToClean);
await this.progressBar.reset(emptyDirs.length);
this.progressBar.logTrace(`cleaning ${emptyDirs.length.toLocaleString()} empty director${emptyDirs.length !== 1 ? 'ies' : 'y'}`);
await this.trashOrDelete(emptyDirs);
let emptyDirs = await DirectoryCleaner.getEmptyDirs(dirsToClean);
while (emptyDirs.length > 0) {
await this.progressBar.reset(emptyDirs.length);
this.progressBar.logTrace(`cleaning ${emptyDirs.length.toLocaleString()} empty director${emptyDirs.length !== 1 ? 'ies' : 'y'}`);
await this.trashOrDelete(emptyDirs);
// Deleting some empty directories could leave others newly empty
emptyDirs = await DirectoryCleaner.getEmptyDirs(dirsToClean);
}
} catch (error) {
this.progressBar.logError(`failed to clean empty directories: ${error}`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/polyfill/fsPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ export default class FsPoly {

try {
await fs.promises.access(pathLike); // throw if file doesn't exist
} catch (error) {
} catch {
if (optionsWithRetry?.force) {
return;
}
throw error;
throw new Error(`can't rm, path doesn't exist: ${pathLike}`);
}

if (await this.isDirectory(pathLike)) {
Expand Down
2 changes: 1 addition & 1 deletion test/modules/directoryCleaner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function runOutputCleaner(
const after = await fsPoly.walk(tempDir);

// Test cleanup
await fsPoly.rm(tempDir, { recursive: true });
await fsPoly.rm(tempDir, { recursive: true, force: true });

return after
.map((pathLike) => pathLike.replace(tempDir + path.sep, ''))
Expand Down

0 comments on commit b0e1011

Please sign in to comment.