Skip to content

Commit 90501c0

Browse files
resolving comments
1 parent 0535369 commit 90501c0

File tree

8 files changed

+140
-93
lines changed

8 files changed

+140
-93
lines changed

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ inputs:
3030
runs:
3131
using: 'node12'
3232
main: 'dist/setup/index.js'
33-
post: 'dist/saveCache/index.js'
34-
post-if: success()
33+
post: 'dist/save-cache/index.js'
34+
post-if: success() && inputs.cache != null

dist/saveCache/index.js renamed to dist/save-cache/index.js

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39274,12 +39274,21 @@ const fs = __importStar(__webpack_require__(747));
3927439274
const stream = __importStar(__webpack_require__(794));
3927539275
const util = __importStar(__webpack_require__(669));
3927639276
const path = __importStar(__webpack_require__(622));
39277-
const toolCacheCommands = {
39278-
npm: 'npm config get cache',
39279-
yarn1: 'yarn cache dir',
39280-
yarn2: 'yarn config get cacheFolder'
39277+
exports.supportedPackageManagers = {
39278+
npm: {
39279+
lockFilePatterns: ['package-lock.json', 'yarn.lock'],
39280+
getCacheFolderCommand: 'npm config get cache'
39281+
},
39282+
yarn1: {
39283+
lockFilePatterns: ['yarn.lock'],
39284+
getCacheFolderCommand: 'yarn cache dir'
39285+
},
39286+
yarn2: {
39287+
lockFilePatterns: ['yarn.lock'],
39288+
getCacheFolderCommand: 'yarn config get cacheFolder'
39289+
}
3928139290
};
39282-
const getCommandOutput = (toolCommand, errMessage) => __awaiter(void 0, void 0, void 0, function* () {
39291+
const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
3928339292
let stdOut;
3928439293
let stdErr;
3928539294
yield exec.exec(toolCommand, undefined, {
@@ -39291,13 +39300,13 @@ const getCommandOutput = (toolCommand, errMessage) => __awaiter(void 0, void 0,
3929139300
if (stdErr) {
3929239301
throw new Error(stdErr);
3929339302
}
39294-
if (!stdOut) {
39295-
throw new Error(errMessage);
39296-
}
3929739303
return stdOut;
3929839304
});
39299-
const getpackageManagerVersion = (packageManager, command, regex) => __awaiter(void 0, void 0, void 0, function* () {
39300-
const stdOut = yield getCommandOutput(`${packageManager} ${command}`, `Could not get version for ${packageManager}`);
39305+
const getpackageManagerVersion = (packageManager, command) => __awaiter(void 0, void 0, void 0, function* () {
39306+
const stdOut = yield getCommandOutput(`${packageManager} ${command}`);
39307+
if (!stdOut) {
39308+
throw new Error(`Could not get version for ${packageManager}`);
39309+
}
3930139310
if (stdOut.startsWith('1.')) {
3930239311
return '1';
3930339312
}
@@ -39316,9 +39325,23 @@ exports.isPackageManagerCacheSupported = packageManager => {
3931639325
return arr.includes(packageManager);
3931739326
};
3931839327
exports.getCacheDirectoryPath = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
39319-
const fullToolName = yield getCmdCommand(packageManager);
39320-
const toolCommand = toolCacheCommands[fullToolName];
39321-
const stdOut = yield getCommandOutput(toolCommand, `Could not get version for ${packageManager}`);
39328+
let packageManagerInfo;
39329+
if (packageManager === 'npm') {
39330+
packageManagerInfo = exports.supportedPackageManagers.npm;
39331+
}
39332+
else if (packageManager === 'yarn') {
39333+
const yarnVersion = yield getpackageManagerVersion('yarn', '--version');
39334+
if (yarnVersion.startsWith('1.')) {
39335+
packageManagerInfo = exports.supportedPackageManagers.yarn1;
39336+
}
39337+
else {
39338+
packageManagerInfo = exports.supportedPackageManagers.yarn2;
39339+
}
39340+
}
39341+
const stdOut = yield getCommandOutput(packageManagerInfo.getCacheFolderCommand);
39342+
if (!stdOut) {
39343+
throw new Error(`Could not get version for ${packageManager}`);
39344+
}
3932239345
return stdOut;
3932339346
});
3932439347
// https://github.com/actions/runner/blob/master/src/Misc/expressionFunc/hashFiles/src/hashFiles.ts
@@ -50329,7 +50352,7 @@ function run() {
5032950352
});
5033050353
}
5033150354
const cachePackages = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
50332-
const state = getCacheState();
50355+
const state = core.getState(constants_1.State.CacheMatchedKey);
5033350356
const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
5033450357
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManager);
5033550358
if (primaryKey === state) {
@@ -50352,14 +50375,6 @@ const cachePackages = (packageManager) => __awaiter(void 0, void 0, void 0, func
5035250375
}
5035350376
}
5035450377
});
50355-
function getCacheState() {
50356-
const cacheKey = core.getState(constants_1.State.CacheMatchedKey);
50357-
if (cacheKey) {
50358-
core.debug(`Cache state/key: ${cacheKey}`);
50359-
return cacheKey;
50360-
}
50361-
return undefined;
50362-
}
5036350378
run();
5036450379

5036550380

dist/setup/index.js

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7522,7 +7522,6 @@ function run() {
75227522
}
75237523
let arch = core.getInput('architecture');
75247524
const cache = core.getInput('cache');
7525-
core.info(`cache is ${cache}`);
75267525
// if architecture supplied but node-version is not
75277526
// if we don't throw a warning, the already installed x64 node will be used which is not probably what user meant.
75287527
if (arch && !version) {
@@ -7545,7 +7544,7 @@ function run() {
75457544
}
75467545
if (cache) {
75477546
if (!isGhes()) {
7548-
yield cache_restore_1.restoreCache(cache, version);
7547+
yield cache_restore_1.restoreCache(cache);
75497548
}
75507549
else {
75517550
throw new Error('Caching is not supported on GHES');
@@ -43402,19 +43401,19 @@ const path_1 = __importDefault(__webpack_require__(622));
4340243401
const fs_1 = __importDefault(__webpack_require__(747));
4340343402
const constants_1 = __webpack_require__(196);
4340443403
const cache_utils_1 = __webpack_require__(452);
43405-
exports.restoreCache = (packageManager, version) => __awaiter(void 0, void 0, void 0, function* () {
43404+
exports.restoreCache = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
4340643405
if (!cache_utils_1.isPackageManagerCacheSupported(packageManager)) {
4340743406
throw new Error(`Caching for '${packageManager}'is not supported`);
4340843407
}
43409-
const lockKey = findLockFile(packageManager);
4341043408
const platform = process.env.RUNNER_OS;
43411-
const fileHash = yield cache_utils_1.hashFile(lockKey);
43412-
const primaryKey = `${platform}-${packageManager}-${version}-${fileHash}`;
43409+
const lockFilePath = findLockFile(packageManager);
43410+
const fileHash = yield cache_utils_1.hashFile(lockFilePath);
43411+
const primaryKey = `${platform}-${packageManager}-${fileHash}`;
4341343412
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
4341443413
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManager);
4341543414
const cacheKey = yield cache.restoreCache([cachePath], primaryKey);
4341643415
if (!cacheKey) {
43417-
core.warning(`${packageManager} cache is not found`);
43416+
core.info(`${packageManager} cache is not found`);
4341843417
return;
4341943418
}
4342043419
core.saveState(constants_1.State.CacheMatchedKey, cacheKey);
@@ -43423,12 +43422,15 @@ exports.restoreCache = (packageManager, version) => __awaiter(void 0, void 0, vo
4342343422
core.info(`Cache restored from key: ${cacheKey}`);
4342443423
});
4342543424
const findLockFile = (packageManager) => {
43426-
let lockFiles = ['package-lock.json', 'yarn.lock'];
43425+
let lockFiles;
43426+
if (packageManager === 'npm') {
43427+
lockFiles = cache_utils_1.supportedPackageManagers.npm.lockFilePatterns;
43428+
}
43429+
else {
43430+
lockFiles = cache_utils_1.supportedPackageManagers.yarn1.lockFilePatterns;
43431+
}
4342743432
const workspace = process.env.GITHUB_WORKSPACE;
4342843433
const rootContent = fs_1.default.readdirSync(workspace);
43429-
if (packageManager === 'yarn') {
43430-
lockFiles.splice(0);
43431-
}
4343243434
const fullLockFile = rootContent.find(item => lockFiles.includes(item));
4343343435
if (!fullLockFile) {
4343443436
throw new Error(`No package-lock.json or yarn.lock were found in ${workspace}`);
@@ -44684,12 +44686,21 @@ const fs = __importStar(__webpack_require__(747));
4468444686
const stream = __importStar(__webpack_require__(794));
4468544687
const util = __importStar(__webpack_require__(669));
4468644688
const path = __importStar(__webpack_require__(622));
44687-
const toolCacheCommands = {
44688-
npm: 'npm config get cache',
44689-
yarn1: 'yarn cache dir',
44690-
yarn2: 'yarn config get cacheFolder'
44689+
exports.supportedPackageManagers = {
44690+
npm: {
44691+
lockFilePatterns: ['package-lock.json', 'yarn.lock'],
44692+
getCacheFolderCommand: 'npm config get cache'
44693+
},
44694+
yarn1: {
44695+
lockFilePatterns: ['yarn.lock'],
44696+
getCacheFolderCommand: 'yarn cache dir'
44697+
},
44698+
yarn2: {
44699+
lockFilePatterns: ['yarn.lock'],
44700+
getCacheFolderCommand: 'yarn config get cacheFolder'
44701+
}
4469144702
};
44692-
const getCommandOutput = (toolCommand, errMessage) => __awaiter(void 0, void 0, void 0, function* () {
44703+
const getCommandOutput = (toolCommand) => __awaiter(void 0, void 0, void 0, function* () {
4469344704
let stdOut;
4469444705
let stdErr;
4469544706
yield exec.exec(toolCommand, undefined, {
@@ -44701,13 +44712,13 @@ const getCommandOutput = (toolCommand, errMessage) => __awaiter(void 0, void 0,
4470144712
if (stdErr) {
4470244713
throw new Error(stdErr);
4470344714
}
44704-
if (!stdOut) {
44705-
throw new Error(errMessage);
44706-
}
4470744715
return stdOut;
4470844716
});
44709-
const getpackageManagerVersion = (packageManager, command, regex) => __awaiter(void 0, void 0, void 0, function* () {
44710-
const stdOut = yield getCommandOutput(`${packageManager} ${command}`, `Could not get version for ${packageManager}`);
44717+
const getpackageManagerVersion = (packageManager, command) => __awaiter(void 0, void 0, void 0, function* () {
44718+
const stdOut = yield getCommandOutput(`${packageManager} ${command}`);
44719+
if (!stdOut) {
44720+
throw new Error(`Could not get version for ${packageManager}`);
44721+
}
4471144722
if (stdOut.startsWith('1.')) {
4471244723
return '1';
4471344724
}
@@ -44726,9 +44737,23 @@ exports.isPackageManagerCacheSupported = packageManager => {
4472644737
return arr.includes(packageManager);
4472744738
};
4472844739
exports.getCacheDirectoryPath = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
44729-
const fullToolName = yield getCmdCommand(packageManager);
44730-
const toolCommand = toolCacheCommands[fullToolName];
44731-
const stdOut = yield getCommandOutput(toolCommand, `Could not get version for ${packageManager}`);
44740+
let packageManagerInfo;
44741+
if (packageManager === 'npm') {
44742+
packageManagerInfo = exports.supportedPackageManagers.npm;
44743+
}
44744+
else if (packageManager === 'yarn') {
44745+
const yarnVersion = yield getpackageManagerVersion('yarn', '--version');
44746+
if (yarnVersion.startsWith('1.')) {
44747+
packageManagerInfo = exports.supportedPackageManagers.yarn1;
44748+
}
44749+
else {
44750+
packageManagerInfo = exports.supportedPackageManagers.yarn2;
44751+
}
44752+
}
44753+
const stdOut = yield getCommandOutput(packageManagerInfo.getCacheFolderCommand);
44754+
if (!stdOut) {
44755+
throw new Error(`Could not get version for ${packageManager}`);
44756+
}
4473244757
return stdOut;
4473344758
});
4473444759
// https://github.com/actions/runner/blob/master/src/Misc/expressionFunc/hashFiles/src/hashFiles.ts

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"description": "setup node action",
66
"main": "lib/setup-node.js",
77
"scripts": {
8-
"build": "tsc && ncc build -o dist/setup src/setup-node.ts && ncc build -o dist/saveCache src/cache-save.ts",
8+
"build": "tsc && ncc build -o dist/setup src/setup-node.ts && ncc build -o dist/save-cache src/cache-save.ts",
99
"format": "prettier --write **/*.ts",
1010
"format-check": "prettier --check **/*.ts",
1111
"test": "jest",

src/cache-restore.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,26 @@ import {State, Outputs} from './constants';
77
import {
88
getCacheDirectoryPath,
99
hashFile,
10-
isPackageManagerCacheSupported
10+
isPackageManagerCacheSupported,
11+
supportedPackageManagers
1112
} from './cache-utils';
1213

13-
export const restoreCache = async (packageManager: string, version: string) => {
14+
export const restoreCache = async (packageManager: string) => {
1415
if (!isPackageManagerCacheSupported(packageManager)) {
1516
throw new Error(`Caching for '${packageManager}'is not supported`);
1617
}
17-
const lockKey = findLockFile(packageManager);
18-
1918
const platform = process.env.RUNNER_OS;
20-
const fileHash = await hashFile(lockKey);
19+
const lockFilePath = findLockFile(packageManager);
2120

22-
const primaryKey = `${platform}-${packageManager}-${version}-${fileHash}`;
21+
const fileHash = await hashFile(lockFilePath);
22+
const primaryKey = `${platform}-${packageManager}-${fileHash}`;
2323
core.saveState(State.CachePrimaryKey, primaryKey);
2424

2525
const cachePath = await getCacheDirectoryPath(packageManager);
2626
const cacheKey = await cache.restoreCache([cachePath], primaryKey);
2727

2828
if (!cacheKey) {
29-
core.warning(`${packageManager} cache is not found`);
29+
core.info(`${packageManager} cache is not found`);
3030
return;
3131
}
3232

@@ -37,12 +37,14 @@ export const restoreCache = async (packageManager: string, version: string) => {
3737
};
3838

3939
const findLockFile = (packageManager: string) => {
40-
let lockFiles = ['package-lock.json', 'yarn.lock'];
40+
let lockFiles;
41+
if (packageManager === 'npm') {
42+
lockFiles = supportedPackageManagers.npm.lockFilePatterns;
43+
} else {
44+
lockFiles = supportedPackageManagers.yarn1.lockFilePatterns;
45+
}
4146
const workspace = process.env.GITHUB_WORKSPACE!;
4247
const rootContent = fs.readdirSync(workspace);
43-
if (packageManager === 'yarn') {
44-
lockFiles.splice(0);
45-
}
4648

4749
const fullLockFile = rootContent.find(item => lockFiles.includes(item));
4850
if (!fullLockFile) {

src/cache-save.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ async function run() {
1313
}
1414

1515
const cachePackages = async (packageManager: string) => {
16-
const state = getCacheState();
16+
const state = core.getState(State.CacheMatchedKey);
1717
const primaryKey = core.getState(State.CachePrimaryKey);
1818

1919
const cachePath = await getCacheDirectoryPath(packageManager);
@@ -38,14 +38,4 @@ const cachePackages = async (packageManager: string) => {
3838
}
3939
};
4040

41-
function getCacheState(): string | undefined {
42-
const cacheKey = core.getState(State.CacheMatchedKey);
43-
if (cacheKey) {
44-
core.debug(`Cache state/key: ${cacheKey}`);
45-
return cacheKey;
46-
}
47-
48-
return undefined;
49-
}
50-
5141
run();

0 commit comments

Comments
 (0)