Repository navigation
Expand file tree
/
Copy pathnormalize-cache.ts
More file actions
152 lines (134 loc) · 5.1 KB
/
Copy pathnormalize-cache.ts
File metadata and controls
152 lines (134 loc) · 5.1 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @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.dev/license
*/
import { existsSync, readFileSync, statSync } from 'node:fs';
import { dirname, isAbsolute, join, relative, resolve } from 'node:path';
/** Version placeholder is replaced during the build process with actual package version */
const VERSION = '0.0.0-PLACEHOLDER';
export interface NormalizedCachedOptions {
/** Whether disk cache is enabled. */
enabled: boolean;
/** Disk cache path. Example: `/.angular/cache/v12.0.0`. */
path: string;
/** Disk cache base path. Example: `/.angular/cache`. */
basePath: string;
/**
* Workspace-local disk cache path. Example: `/.angular/cache/v12.0.0`.
* Always resolves relative to the current workspace root, even within a Git worktree.
*/
localPath?: string;
/**
* Workspace-local disk cache base path. Example: `/.angular/cache`.
* Always resolves relative to the current workspace root, even within a Git worktree.
*/
localBasePath?: string;
}
interface CacheMetadata {
enabled?: boolean;
environment?: 'local' | 'ci' | 'all';
path?: string;
}
function hasCacheMetadata(value: unknown): value is { cli: { cache: CacheMetadata } } {
return (
!!value &&
typeof value === 'object' &&
'cli' in value &&
!!value['cli'] &&
typeof value['cli'] === 'object' &&
'cache' in value['cli']
);
}
function getCacheBasePath(workspaceRoot: string, cachePathSetting: string): string {
if (isAbsolute(cachePathSetting)) {
return cachePathSetting;
}
try {
// Find the git directory, walking up from workspaceRoot if necessary
let currentDir = workspaceRoot;
while (true) {
const gitPath = join(currentDir, '.git');
if (existsSync(gitPath)) {
const stat = statSync(gitPath);
if (stat.isFile()) {
// Could be a git worktree (or submodule)
const content = readFileSync(gitPath, 'utf8');
const match = /^gitdir:\s*(.+)$/m.exec(content);
if (match) {
const gitdir = resolve(currentDir, match[1].trim());
const commondirPath = join(gitdir, 'commondir');
if (existsSync(commondirPath)) {
// It's a git worktree
const commondir = readFileSync(commondirPath, 'utf8').trim();
const commonGitDir = resolve(gitdir, commondir);
const relativeWorkspacePath = relative(currentDir, workspaceRoot);
const mainWorkspaceRoot = resolve(dirname(commonGitDir), relativeWorkspacePath);
if (existsSync(mainWorkspaceRoot)) {
return resolve(mainWorkspaceRoot, cachePathSetting);
}
}
}
}
break;
}
const parentDir = dirname(currentDir);
if (parentDir === currentDir) {
break;
}
currentDir = parentDir;
}
} catch {}
return resolve(workspaceRoot, cachePathSetting);
}
/**
* Normalizes the persistent disk cache configuration for a project or workspace.
*
* Resolves whether disk caching is enabled based on the CLI cache metadata, current runtime
* environment (local vs. CI, or WebContainers), and computes the versioned cache directory paths
* (resolving shared Git worktree paths when applicable).
*
* @param projectMetadata The project or workspace metadata object containing optional `cli.cache` settings.
* @param workspaceRoot The absolute path to the workspace root directory.
* @param projectName Optional name of the project used to scope the resolved cache path.
* @param builderName Optional name of the builder or tool used to further scope the resolved cache path.
* @returns The normalized disk cache options including enabled state and resolved directory paths.
*/
export function normalizeCacheOptions(
projectMetadata: unknown,
workspaceRoot: string,
projectName = '',
builderName = '',
): NormalizedCachedOptions {
const cacheMetadata = hasCacheMetadata(projectMetadata) ? projectMetadata.cli.cache : {};
const {
// Webcontainers do not currently benefit from persistent disk caching and can lead to increased browser memory usage
enabled = !process.versions.webcontainer,
environment = 'local',
path = '.angular/cache',
} = cacheMetadata;
const isCI = process.env['CI'] === '1' || process.env['CI']?.toLowerCase() === 'true';
let cacheEnabled = enabled;
if (cacheEnabled) {
switch (environment) {
case 'ci':
cacheEnabled = isCI;
break;
case 'local':
cacheEnabled = !isCI;
break;
}
}
const cacheBasePath = getCacheBasePath(workspaceRoot, path);
const localCacheBasePath = isAbsolute(path) ? path : resolve(workspaceRoot, path);
const normalizedBuilderName = builderName.replace('@', '').replaceAll(/[:/]/g, '-');
return {
enabled: cacheEnabled,
basePath: cacheBasePath,
path: join(cacheBasePath, VERSION, projectName, normalizedBuilderName),
localBasePath: localCacheBasePath,
localPath: join(localCacheBasePath, VERSION, projectName, normalizedBuilderName),
};
}