Skip to content

Commit 515f6db

Browse files
work on resolving comments
1 parent a8106dc commit 515f6db

File tree

6 files changed

+93
-98
lines changed

6 files changed

+93
-98
lines changed

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ inputs:
2020
description: Used to pull node distributions from node-versions. Since there's a default, this is typically not supplied by the user.
2121
default: ${{ github.token }}
2222
cache:
23-
description: 'Used to specify supported package manager to cache in its default directory'
23+
description: 'Used to specify supported package manager to cache in its default directory. Possible values are npm, yarn'
2424
# TODO: add input to control forcing to pull from cloud or dist.
2525
# escape valve for someone having issues or needing the absolute latest which isn't cached yet
2626
# Deprecated option, do not use. Will not be supported after October 1, 2019

dist/saveCache/index.js

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39279,7 +39279,7 @@ const toolCacheCommands = {
3927939279
yarn1: 'yarn cache dir',
3928039280
yarn2: 'yarn config get cacheFolder'
3928139281
};
39282-
const execHandler = (toolCommand, errMessage) => __awaiter(void 0, void 0, void 0, function* () {
39282+
const getCommandOutput = (toolCommand, errMessage) => __awaiter(void 0, void 0, void 0, function* () {
3928339283
let stdOut;
3928439284
let stdErr;
3928539285
yield exec.exec(toolCommand, undefined, {
@@ -39296,29 +39296,29 @@ const execHandler = (toolCommand, errMessage) => __awaiter(void 0, void 0, void
3929639296
}
3929739297
return stdOut;
3929839298
});
39299-
const getToolVersion = (toolName, command, regex) => __awaiter(void 0, void 0, void 0, function* () {
39300-
const stdOut = yield execHandler(`${toolName} ${command}`, `Could not get version for ${toolName}`);
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}`);
3930139301
if (stdOut.startsWith('1.')) {
3930239302
return '1';
3930339303
}
3930439304
return '2';
3930539305
});
39306-
const getCmdCommand = (toolName) => __awaiter(void 0, void 0, void 0, function* () {
39307-
let cmdCommand = toolName;
39308-
if (toolName === 'yarn') {
39309-
const toolVersion = yield getToolVersion(toolName, '--version');
39310-
cmdCommand = `${toolName}${toolVersion}`;
39306+
const getCmdCommand = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
39307+
let cmdCommand = packageManager;
39308+
if (packageManager === 'yarn') {
39309+
const toolVersion = yield getpackageManagerVersion(packageManager, '--version');
39310+
cmdCommand = `${packageManager}${toolVersion}`;
3931139311
}
3931239312
return cmdCommand;
3931339313
});
39314-
exports.isPackageManagerCacheSupported = toolName => {
39314+
exports.isPackageManagerCacheSupported = packageManager => {
3931539315
const arr = Array.of(...Object.values(constants_1.LockType));
39316-
return arr.includes(toolName);
39316+
return arr.includes(packageManager);
3931739317
};
39318-
exports.getCacheDirectoryPath = (toolName) => __awaiter(void 0, void 0, void 0, function* () {
39319-
const fullToolName = yield getCmdCommand(toolName);
39318+
exports.getCacheDirectoryPath = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
39319+
const fullToolName = yield getCmdCommand(packageManager);
3932039320
const toolCommand = toolCacheCommands[fullToolName];
39321-
const stdOut = yield execHandler(toolCommand, `Could not get version for ${toolName}`);
39321+
const stdOut = yield getCommandOutput(toolCommand, `Could not get version for ${packageManager}`);
3932239322
return stdOut;
3932339323
});
3932439324
// https://github.com/actions/runner/blob/master/src/Misc/expressionFunc/hashFiles/src/hashFiles.ts
@@ -39337,7 +39337,8 @@ function hashFile(matchPatterns) {
3933739337
try {
3933839338
for (var _b = __asyncValues(globber.globGenerator()), _c; _c = yield _b.next(), !_c.done;) {
3933939339
const file = _c.value;
39340-
console.log(file);
39340+
console.log(globber);
39341+
console.log(`fileis ${file}`);
3934139342
if (!file.startsWith(`${githubWorkspace}${path.sep}`)) {
3934239343
continue;
3934339344
}
@@ -39361,6 +39362,7 @@ function hashFile(matchPatterns) {
3936139362
finally { if (e_1) throw e_1.error; }
3936239363
}
3936339364
result.end();
39365+
// node -e "const prmosify = require('util').promisify; const crypto = require('crypto'); const stream = require('stream'); const file = '/Users/dmitryshibanov/Documents/myProjects/setup-node/__tests__/data/package-lock.json'; const resul = crypto.createHash('sha256'); const hash = crypto.createHash('sha256'); const pipeline = prmosify(stream.pipeline); pipeline(fs.createReadStream(file), hash).then((result1) =>{ result.write(hash.digest()); result.end(); console.log(result.digest('hex'))}) "
3936439366
return result.digest('hex');
3936539367
});
3936639368
}
@@ -50329,11 +50331,11 @@ function run() {
5032950331
}
5033050332
});
5033150333
}
50332-
const cachePackages = (toolName) => __awaiter(void 0, void 0, void 0, function* () {
50334+
const cachePackages = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
5033350335
const state = getCacheState();
5033450336
const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
50335-
const cachePath = yield cache_utils_1.getCacheDirectoryPath(toolName);
50336-
if (isExactKeyMatch(primaryKey, state)) {
50337+
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManager);
50338+
if (primaryKey === state) {
5033750339
core.info(`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`);
5033850340
return;
5033950341
}
@@ -50349,8 +50351,7 @@ const cachePackages = (toolName) => __awaiter(void 0, void 0, void 0, function*
5034950351
core.info(error.message);
5035050352
}
5035150353
else {
50352-
const warningPrefix = '[warning]';
50353-
core.info(`${warningPrefix}${error.message}`);
50354+
core.warning(`${error.message}`);
5035450355
}
5035550356
}
5035650357
});
@@ -50362,12 +50363,6 @@ function getCacheState() {
5036250363
}
5036350364
return undefined;
5036450365
}
50365-
function isExactKeyMatch(key, cacheKey) {
50366-
return !!(cacheKey &&
50367-
cacheKey.localeCompare(key, undefined, {
50368-
sensitivity: 'accent'
50369-
}) === 0);
50370-
}
5037150366
run();
5037250367

5037350368

dist/setup/index.js

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -43402,36 +43402,36 @@ const path_1 = __importDefault(__webpack_require__(622));
4340243402
const fs_1 = __importDefault(__webpack_require__(747));
4340343403
const constants_1 = __webpack_require__(196);
4340443404
const cache_utils_1 = __webpack_require__(452);
43405-
exports.restoreCache = (toolName, version) => __awaiter(void 0, void 0, void 0, function* () {
43406-
if (!cache_utils_1.isPackageManagerCacheSupported(toolName)) {
43407-
throw new Error(`${toolName} is not supported`);
43405+
exports.restoreCache = (packageManager, version) => __awaiter(void 0, void 0, void 0, function* () {
43406+
if (!cache_utils_1.isPackageManagerCacheSupported(packageManager)) {
43407+
throw new Error(`Caching for '${packageManager}'is not supported`);
4340843408
}
43409-
const lockKey = getLockFile(toolName);
43409+
const lockKey = findLockFile(packageManager);
4341043410
const platform = process.env.RUNNER_OS;
4341143411
const fileHash = yield cache_utils_1.hashFile(lockKey);
43412-
const primaryKey = `${platform}-${toolName}-${version}-${fileHash}`;
43412+
const primaryKey = `${platform}-${packageManager}-${version}-${fileHash}`;
4341343413
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
43414-
const cachePath = yield cache_utils_1.getCacheDirectoryPath(toolName);
43414+
const cachePath = yield cache_utils_1.getCacheDirectoryPath(packageManager);
4341543415
const cacheKey = yield cache.restoreCache([cachePath], primaryKey);
4341643416
if (!cacheKey) {
43417-
core.warning(`Cache not found for input keys: ${primaryKey}`);
43417+
core.warning(`${packageManager} cache is not found`);
4341843418
return;
4341943419
}
4342043420
core.saveState(constants_1.State.CacheMatchedKey, cacheKey);
4342143421
const isExactMatch = (primaryKey === cacheKey).toString();
4342243422
core.setOutput(constants_1.Outputs.CacheHit, isExactMatch);
4342343423
core.info(`Cache restored from key: ${cacheKey}`);
4342443424
});
43425-
const getLockFile = (cacheType) => {
43426-
let lockFile = 'package-lock.json';
43425+
const findLockFile = (packageManager) => {
43426+
let lockFiles = ['package-lock.json', 'yarn.lock'];
4342743427
const workspace = process.env.GITHUB_WORKSPACE;
4342843428
const rootContent = fs_1.default.readdirSync(workspace);
43429-
if (cacheType === 'yarn') {
43430-
lockFile = 'yarn.lock';
43429+
if (packageManager === 'yarn') {
43430+
lockFiles.splice(0);
4343143431
}
43432-
const fullLockFile = rootContent.find(item => lockFile === item);
43432+
const fullLockFile = rootContent.find(item => lockFiles.includes(item));
4343343433
if (!fullLockFile) {
43434-
throw new Error('No package-lock.json or yarn.lock were found');
43434+
throw new Error(`No package-lock.json or yarn.lock were found in ${workspace}`);
4343543435
}
4343643436
return path_1.default.resolve(fullLockFile);
4343743437
};
@@ -44689,7 +44689,7 @@ const toolCacheCommands = {
4468944689
yarn1: 'yarn cache dir',
4469044690
yarn2: 'yarn config get cacheFolder'
4469144691
};
44692-
const execHandler = (toolCommand, errMessage) => __awaiter(void 0, void 0, void 0, function* () {
44692+
const getCommandOutput = (toolCommand, errMessage) => __awaiter(void 0, void 0, void 0, function* () {
4469344693
let stdOut;
4469444694
let stdErr;
4469544695
yield exec.exec(toolCommand, undefined, {
@@ -44706,29 +44706,29 @@ const execHandler = (toolCommand, errMessage) => __awaiter(void 0, void 0, void
4470644706
}
4470744707
return stdOut;
4470844708
});
44709-
const getToolVersion = (toolName, command, regex) => __awaiter(void 0, void 0, void 0, function* () {
44710-
const stdOut = yield execHandler(`${toolName} ${command}`, `Could not get version for ${toolName}`);
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}`);
4471144711
if (stdOut.startsWith('1.')) {
4471244712
return '1';
4471344713
}
4471444714
return '2';
4471544715
});
44716-
const getCmdCommand = (toolName) => __awaiter(void 0, void 0, void 0, function* () {
44717-
let cmdCommand = toolName;
44718-
if (toolName === 'yarn') {
44719-
const toolVersion = yield getToolVersion(toolName, '--version');
44720-
cmdCommand = `${toolName}${toolVersion}`;
44716+
const getCmdCommand = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
44717+
let cmdCommand = packageManager;
44718+
if (packageManager === 'yarn') {
44719+
const toolVersion = yield getpackageManagerVersion(packageManager, '--version');
44720+
cmdCommand = `${packageManager}${toolVersion}`;
4472144721
}
4472244722
return cmdCommand;
4472344723
});
44724-
exports.isPackageManagerCacheSupported = toolName => {
44724+
exports.isPackageManagerCacheSupported = packageManager => {
4472544725
const arr = Array.of(...Object.values(constants_1.LockType));
44726-
return arr.includes(toolName);
44726+
return arr.includes(packageManager);
4472744727
};
44728-
exports.getCacheDirectoryPath = (toolName) => __awaiter(void 0, void 0, void 0, function* () {
44729-
const fullToolName = yield getCmdCommand(toolName);
44728+
exports.getCacheDirectoryPath = (packageManager) => __awaiter(void 0, void 0, void 0, function* () {
44729+
const fullToolName = yield getCmdCommand(packageManager);
4473044730
const toolCommand = toolCacheCommands[fullToolName];
44731-
const stdOut = yield execHandler(toolCommand, `Could not get version for ${toolName}`);
44731+
const stdOut = yield getCommandOutput(toolCommand, `Could not get version for ${packageManager}`);
4473244732
return stdOut;
4473344733
});
4473444734
// https://github.com/actions/runner/blob/master/src/Misc/expressionFunc/hashFiles/src/hashFiles.ts
@@ -44747,7 +44747,8 @@ function hashFile(matchPatterns) {
4474744747
try {
4474844748
for (var _b = __asyncValues(globber.globGenerator()), _c; _c = yield _b.next(), !_c.done;) {
4474944749
const file = _c.value;
44750-
console.log(file);
44750+
console.log(globber);
44751+
console.log(`fileis ${file}`);
4475144752
if (!file.startsWith(`${githubWorkspace}${path.sep}`)) {
4475244753
continue;
4475344754
}
@@ -44771,6 +44772,7 @@ function hashFile(matchPatterns) {
4477144772
finally { if (e_1) throw e_1.error; }
4477244773
}
4477344774
result.end();
44775+
// node -e "const prmosify = require('util').promisify; const crypto = require('crypto'); const stream = require('stream'); const file = '/Users/dmitryshibanov/Documents/myProjects/setup-node/__tests__/data/package-lock.json'; const resul = crypto.createHash('sha256'); const hash = crypto.createHash('sha256'); const pipeline = prmosify(stream.pipeline); pipeline(fs.createReadStream(file), hash).then((result1) =>{ result.write(hash.digest()); result.end(); console.log(result.digest('hex'))}) "
4477444776
return result.digest('hex');
4477544777
});
4477644778
}

src/cache-restore.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ import {
1010
isPackageManagerCacheSupported
1111
} from './cache-utils';
1212

13-
export const restoreCache = async (toolName: string, version: string) => {
14-
if (!isPackageManagerCacheSupported(toolName)) {
15-
throw new Error(`${toolName} is not supported`);
13+
export const restoreCache = async (packageManager: string, version: string) => {
14+
if (!isPackageManagerCacheSupported(packageManager)) {
15+
throw new Error(`Caching for '${packageManager}'is not supported`);
1616
}
17-
const lockKey = getLockFile(toolName);
17+
const lockKey = findLockFile(packageManager);
1818

1919
const platform = process.env.RUNNER_OS;
2020
const fileHash = await hashFile(lockKey);
2121

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

25-
const cachePath = await getCacheDirectoryPath(toolName);
25+
const cachePath = await getCacheDirectoryPath(packageManager);
2626
const cacheKey = await cache.restoreCache([cachePath], primaryKey);
2727

2828
if (!cacheKey) {
29-
core.warning(`Cache not found for input keys: ${primaryKey}`);
29+
core.warning(`${packageManager} cache is not found`);
3030
return;
3131
}
3232

@@ -36,17 +36,19 @@ export const restoreCache = async (toolName: string, version: string) => {
3636
core.info(`Cache restored from key: ${cacheKey}`);
3737
};
3838

39-
const getLockFile = (cacheType: string) => {
40-
let lockFile = 'package-lock.json';
39+
const findLockFile = (packageManager: string) => {
40+
let lockFiles = ['package-lock.json', 'yarn.lock'];
4141
const workspace = process.env.GITHUB_WORKSPACE!;
4242
const rootContent = fs.readdirSync(workspace);
43-
if (cacheType === 'yarn') {
44-
lockFile = 'yarn.lock';
43+
if (packageManager === 'yarn') {
44+
lockFiles.splice(0);
4545
}
4646

47-
const fullLockFile = rootContent.find(item => lockFile === item);
47+
const fullLockFile = rootContent.find(item => lockFiles.includes(item));
4848
if (!fullLockFile) {
49-
throw new Error('No package-lock.json or yarn.lock were found');
49+
throw new Error(
50+
`No package-lock.json or yarn.lock were found in ${workspace}`
51+
);
5052
}
5153

5254
return path.resolve(fullLockFile);

src/cache-save.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ async function run() {
1212
}
1313
}
1414

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

19-
const cachePath = await getCacheDirectoryPath(toolName);
20-
if (isExactKeyMatch(primaryKey, state)) {
19+
const cachePath = await getCacheDirectoryPath(packageManager);
20+
if (primaryKey === state) {
2121
core.info(
2222
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
2323
);
@@ -33,8 +33,7 @@ const cachePackages = async (toolName: string) => {
3333
} else if (error.name === cache.ReserveCacheError.name) {
3434
core.info(error.message);
3535
} else {
36-
const warningPrefix = '[warning]';
37-
core.info(`${warningPrefix}${error.message}`);
36+
core.warning(`${error.message}`);
3837
}
3938
}
4039
};
@@ -49,13 +48,4 @@ function getCacheState(): string | undefined {
4948
return undefined;
5049
}
5150

52-
function isExactKeyMatch(key: string, cacheKey?: string): boolean {
53-
return !!(
54-
cacheKey &&
55-
cacheKey.localeCompare(key, undefined, {
56-
sensitivity: 'accent'
57-
}) === 0
58-
);
59-
}
60-
6151
run();

0 commit comments

Comments
 (0)