Skip to content

Commit 8a29b61

Browse files
clydinalan-agius4
authored andcommitted
refactor(@angular-devkit/build-angular): remove extra exist check when purging stale build caches
When purging stale build cache entries, the `readdir` call will fail if the cache base path does not exist. This allows for a single check and the removal of the previous separate `existSync` call. Additionally, a repeat join call has also been removed during the stale directory check.
1 parent a0ed46b commit 8a29b61

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

packages/angular_devkit/build_angular/src/utils/purge-cache.ts

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88

99
import { BuilderContext } from '@angular-devkit/architect';
10-
import { existsSync, promises as fsPromises } from 'fs';
11-
import { join } from 'path';
10+
import { readdir, rm } from 'node:fs/promises';
11+
import { join } from 'node:path';
1212
import { normalizeCacheOptions } from './normalize-cache';
1313

1414
/** Delete stale cache directories used by previous versions of build-angular. */
@@ -21,19 +21,23 @@ export async function purgeStaleBuildCache(context: BuilderContext): Promise<voi
2121
const metadata = await context.getProjectMetadata(projectName);
2222
const { basePath, path, enabled } = normalizeCacheOptions(metadata, context.workspaceRoot);
2323

24-
if (!enabled || !existsSync(basePath)) {
24+
if (!enabled) {
2525
return;
2626
}
2727

28-
const entriesToDelete = (await fsPromises.readdir(basePath, { withFileTypes: true }))
29-
.filter((d) => join(basePath, d.name) !== path && d.isDirectory())
30-
.map((d) => {
31-
const subPath = join(basePath, d.name);
28+
let baseEntries;
29+
try {
30+
baseEntries = await readdir(basePath, { withFileTypes: true });
31+
} catch {
32+
// No purging possible if base path does not exist or cannot otherwise be accessed
33+
return;
34+
}
3235

33-
return fsPromises
34-
.rm(subPath, { force: true, recursive: true, maxRetries: 3 })
35-
.catch(() => void 0);
36-
});
36+
const entriesToDelete = baseEntries
37+
.filter((d) => d.isDirectory())
38+
.map((d) => join(basePath, d.name))
39+
.filter((cachePath) => cachePath !== path)
40+
.map((stalePath) => rm(stalePath, { force: true, recursive: true, maxRetries: 3 }));
3741

38-
await Promise.all(entriesToDelete);
42+
await Promise.allSettled(entriesToDelete);
3943
}

0 commit comments

Comments
 (0)