-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
cache-utils.ts
328 lines (291 loc) · 10.1 KB
/
cache-utils.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as cache from '@actions/cache';
import * as glob from '@actions/glob';
import path from 'path';
import fs from 'fs';
export interface PackageManagerInfo {
name: string;
lockFilePatterns: Array<string>;
getCacheFolderPath: (projectDir?: string) => Promise<string>;
}
interface SupportedPackageManagers {
npm: PackageManagerInfo;
pnpm: PackageManagerInfo;
yarn: PackageManagerInfo;
}
// for testing purposes
export const npmGetCacheFolderCommand = 'npm config get cache';
export const pnpmGetCacheFolderCommand = 'pnpm store path --silent';
export const yarn1GetCacheFolderCommand = 'yarn cache dir';
export const yarn2GetCacheFolderCommand = 'yarn config get cacheFolder';
export const supportedPackageManagers: SupportedPackageManagers = {
npm: {
name: 'npm',
lockFilePatterns: ['package-lock.json', 'npm-shrinkwrap.json', 'yarn.lock'],
getCacheFolderPath: () =>
getCommandOutputGuarded(
npmGetCacheFolderCommand,
'Could not get npm cache folder path'
)
},
pnpm: {
name: 'pnpm',
lockFilePatterns: ['pnpm-lock.yaml'],
getCacheFolderPath: () =>
getCommandOutputGuarded(
pnpmGetCacheFolderCommand,
'Could not get pnpm cache folder path'
)
},
yarn: {
name: 'yarn',
lockFilePatterns: ['yarn.lock'],
getCacheFolderPath: async projectDir => {
const yarnVersion = await getCommandOutputGuarded(
`yarn --version`,
'Could not retrieve version of yarn',
projectDir
);
core.debug(
`Consumed yarn version is ${yarnVersion} (working dir: "${projectDir}")`
);
const stdOut = yarnVersion.startsWith('1.')
? await getCommandOutput(yarn1GetCacheFolderCommand, projectDir)
: await getCommandOutput(yarn2GetCacheFolderCommand, projectDir);
if (!stdOut) {
throw new Error(
`Could not get yarn cache folder path for ${projectDir}`
);
}
return stdOut;
}
}
};
export const getCommandOutput = async (
toolCommand: string,
cwd?: string
): Promise<string> => {
let {stdout, stderr, exitCode} = await exec.getExecOutput(
toolCommand,
undefined,
{ignoreReturnCode: true, ...(cwd && {cwd})}
);
if (exitCode) {
stderr = !stderr.trim()
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
: stderr;
throw new Error(stderr);
}
return stdout.trim();
};
export const getCommandOutputGuarded = async (
toolCommand: string,
error: string,
cwd?: string
): Promise<string> => {
const stdOut = getCommandOutput(toolCommand, cwd);
if (!stdOut) {
throw new Error(error);
}
return stdOut;
};
export const getPackageManagerInfo = async (packageManager: string) => {
if (packageManager === 'npm') {
return supportedPackageManagers.npm;
} else if (packageManager === 'pnpm') {
return supportedPackageManagers.pnpm;
} else if (packageManager === 'yarn') {
return supportedPackageManagers.yarn;
} else {
return null;
}
};
export const expandedPatternsMemoized: Record<string, string[]> = {};
/**
* Wrapper around `glob.create(pattern).glob()` with the memoization
* @param pattern is expected to be a globed path
* @return list of files or directories expanded from glob
*/
const globPatternToArray = async (pattern: string): Promise<string[]> => {
const memoized = expandedPatternsMemoized[pattern];
if (memoized) return Promise.resolve(memoized);
const globber = await glob.create(pattern);
const expanded = await globber.glob();
expandedPatternsMemoized[pattern] = expanded;
return expanded;
};
/**
* Expands (converts) the string input `cache-dependency-path` to list of files' paths
* First it breaks the input by new lines and then expand glob patterns if any
* @param cacheDependencyPath - either a single string or multiline string with possible glob patterns
* @return list of files on which the cache depends
*/
export const expandCacheDependencyPath = async (
cacheDependencyPath: string
): Promise<string[]> => {
const multilinePaths = cacheDependencyPath
.split(/\r?\n/)
.map(path => path.trim())
.filter(path => Boolean(path));
const expandedPathsPromises: Promise<string[]>[] = multilinePaths.map(path =>
path.includes('*') ? globPatternToArray(path) : Promise.resolve([path])
);
const expandedPaths: string[][] = await Promise.all(expandedPathsPromises);
return expandedPaths.length === 0 ? [''] : expandedPaths.flat();
};
/**
* Converts dependency file to the directory it resides in and ensures the directory exists
* @param cacheDependencyPath - file name
* @return either directory containing file or null
*/
const cacheDependencyPathToProjectDirectory = (
cacheDependencyPath: string
): string | null => {
const projectDirectory = path.dirname(cacheDependencyPath);
if (
fs.existsSync(projectDirectory) &&
fs.lstatSync(projectDirectory).isDirectory()
) {
core.debug(
`Project directory "${projectDirectory}" derived from cache-dependency-path: "${cacheDependencyPath}"`
);
return projectDirectory;
} else {
core.debug(
`No project directory found for cache-dependency-path: "${cacheDependencyPath}", will be skipped`
);
return null;
}
};
/**
* Expands (converts) the string input `cache-dependency-path` to list of directories that
* may be project roots
* @param cacheDependencyPath
* @return list of directories and possible
*/
const cacheDependencyPathToProjectsDirectories = async (
cacheDependencyPath: string
): Promise<string[]> => {
const cacheDependenciesPaths = await expandCacheDependencyPath(
cacheDependencyPath
);
const existingDirectories: string[] = cacheDependenciesPaths
.map(cacheDependencyPath =>
cacheDependencyPathToProjectDirectory(cacheDependencyPath)
)
.filter(path => path !== null) as string[];
if (existingDirectories.length === 0)
throw Error(
'No existing directories found containing `cache-dependency-path`="${cacheDependencyPath}"'
);
// uniq
return existingDirectories.filter(
(cachePath, i, result) =>
cachePath != null && result.indexOf(cachePath) === i
);
};
const projectDirectoryToCacheFolderPath = async (
packageManagerInfo: PackageManagerInfo,
projectDirectory: string
): Promise<string> => {
const cacheFolderPath = await packageManagerInfo.getCacheFolderPath(
projectDirectory
);
core.debug(
`${packageManagerInfo.name}'s cache folder "${cacheFolderPath}" configured for the directory "${projectDirectory}"`
);
return cacheFolderPath;
};
const projectDirectoriesToCacheFoldersPaths = async (
packageManagerInfo: PackageManagerInfo,
projectDirectories: string[]
): Promise<string[]> => {
const cacheFoldersPaths = await Promise.all(
projectDirectories.map(projectDirectory =>
projectDirectoryToCacheFolderPath(packageManagerInfo, projectDirectory)
)
);
return cacheFoldersPaths.filter(
(cachePath, i, result) => result.indexOf(cachePath) === i
);
};
const cacheDependencyPathToCacheFoldersPaths = async (
packageManagerInfo: PackageManagerInfo,
cacheDependencyPath: string
): Promise<string[]> => {
const projectDirectories = await cacheDependencyPathToProjectsDirectories(
cacheDependencyPath
);
return projectDirectoriesToCacheFoldersPaths(
packageManagerInfo,
projectDirectories
);
};
const cacheFoldersPathsForRoot = async (
packageManagerInfo: PackageManagerInfo
): Promise<string[]> => {
const cacheFolderPath = await packageManagerInfo.getCacheFolderPath();
core.debug(
`${packageManagerInfo.name}'s cache folder "${cacheFolderPath}" configured for the root directory`
);
return [cacheFolderPath];
};
export const getCacheDirectoriesPaths = async (
packageManagerInfo: PackageManagerInfo,
cacheDependencyPath: string
): Promise<string[]> =>
// TODO: multiple directories limited to yarn so far
packageManagerInfo === supportedPackageManagers.yarn
? cacheDependencyPathToCacheFoldersPaths(
packageManagerInfo,
cacheDependencyPath
)
: cacheFoldersPathsForRoot(packageManagerInfo);
export function isGhes(): boolean {
const ghUrl = new URL(
process.env['GITHUB_SERVER_URL'] || 'https://github.com'
);
return ghUrl.hostname.toUpperCase() !== 'GITHUB.COM';
}
export function isCacheFeatureAvailable(): boolean {
if (cache.isFeatureAvailable()) return true;
if (isGhes()) {
core.warning(
'Cache action is only supported on GHES version >= 3.5. If you are on version >=3.5 Please check with GHES admin if Actions cache service is enabled or not.'
);
return false;
}
core.warning(
'The runner was not able to contact the cache service. Caching will be skipped'
);
return false;
}
// a hack for further possible compatibility issues and a syntax sugar
const getYarn3ProjectDirectoryPaths = () =>
getCacheDirectoriesPaths(
supportedPackageManagers.yarn,
core.getInput('cache-dependency-path')
);
// see https://yarnpkg.com/features/offline-cache
let isYarn3OfflineCacheMemoized: boolean | null = null;
export const isYarn3OfflineCache = async (
packageManagerInfo: PackageManagerInfo
): Promise<boolean> => {
if (isYarn3OfflineCacheMemoized !== null) return isYarn3OfflineCacheMemoized;
isYarn3OfflineCacheMemoized = false;
if (packageManagerInfo.name !== 'yarn') return isYarn3OfflineCacheMemoized;
const cacheDependencyPath = core.getInput('cache-dependency-path');
const yarnDirs = await getYarn3ProjectDirectoryPath();
const workDir = yarnDir || process.env.GITHUB_WORKSPACE!;
const yarnCacheFile = path.join(workDir, '.yarn', 'cache');
if (fs.existsSync(yarnCacheFile) && fs.lstatSync(yarnCacheFile).isDirectory())
return isYarn3OfflineCacheMemoized;
const yarnRcPath = path.join(workDir, '.yarnrc.yml');
if (!(fs.existsSync(yarnRcPath) && fs.lstatSync(yarnRcPath).isFile()))
return isYarn3OfflineCacheMemoized;
const yarnRc = fs.readFileSync(path.join(workDir, '.yarnrc.yml')).toString();
const globalCache = yarnRc.match(/^enableGlobalCache:\s*true/m);
isYarn3OfflineCacheMemoized = !globalCache;
return isYarn3OfflineCacheMemoized;
};