-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathpurge-cache.ts
39 lines (32 loc) · 1.25 KB
/
purge-cache.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { BuilderContext } from '@angular-devkit/architect';
import { existsSync, promises as fsPromises } from 'fs';
import { join } from 'path';
import { normalizeCacheOptions } from './normalize-cache';
/** Delete stale cache directories used by previous versions of build-angular. */
export async function purgeStaleBuildCache(context: BuilderContext): Promise<void> {
const projectName = context.target?.project;
if (!projectName) {
return;
}
const metadata = await context.getProjectMetadata(projectName);
const { basePath, path, enabled } = normalizeCacheOptions(metadata, context.workspaceRoot);
if (!enabled || !existsSync(basePath)) {
return;
}
const entriesToDelete = (await fsPromises.readdir(basePath, { withFileTypes: true }))
.filter((d) => join(basePath, d.name) !== path && d.isDirectory())
.map((d) => {
const subPath = join(basePath, d.name);
return fsPromises
.rm(subPath, { force: true, recursive: true, maxRetries: 3 })
.catch(() => void 0);
});
await Promise.all(entriesToDelete);
}