Skip to content

Commit bd3feac

Browse files
work on yarn support
1 parent 3c6ab3c commit bd3feac

File tree

6 files changed

+136
-73
lines changed

6 files changed

+136
-73
lines changed

dist/cleanup/index.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5895,8 +5895,8 @@ var Inputs;
58955895
})(Inputs = exports.Inputs || (exports.Inputs = {}));
58965896
var LockType;
58975897
(function (LockType) {
5898-
LockType[LockType["Npm"] = 0] = "Npm";
5899-
LockType[LockType["Yarn"] = 1] = "Yarn";
5898+
LockType["Npm"] = "npm";
5899+
LockType["Yarn"] = "yarn";
59005900
})(LockType = exports.LockType || (exports.LockType = {}));
59015901
var State;
59025902
(function (State) {
@@ -47722,34 +47722,61 @@ var __importStar = (this && this.__importStar) || function (mod) {
4772247722
};
4772347723
Object.defineProperty(exports, "__esModule", { value: true });
4772447724
const exec = __importStar(__webpack_require__(986));
47725+
const core = __importStar(__webpack_require__(470));
47726+
const constants_1 = __webpack_require__(196);
4772547727
const semver_1 = __webpack_require__(280);
47726-
const toolCommands = {
47728+
const toolCacheCommands = {
4772747729
npm: 'npm config get cache',
4772847730
yarn1: 'yarn cache dir',
4772947731
yarn2: 'yarn config get cacheFolder'
4773047732
};
47731-
exports.getYarnVersion = () => __awaiter(void 0, void 0, void 0, function* () {
47733+
const getToolVersion = (toolName, command, regex) => __awaiter(void 0, void 0, void 0, function* () {
4773247734
let stdErr;
4773347735
let stdOut;
47734-
let majorVersion;
47735-
yield exec.exec('yarn --version', undefined, {
47736+
let toolVersion;
47737+
yield exec.exec(`${toolName} ${command}`, undefined, {
4773647738
listeners: {
4773747739
stdout: (err) => (stdOut = err.toString()),
4773847740
stderr: (out) => (stdErr = out.toString())
4773947741
}
4774047742
});
47743+
core.info(`stdout is ${stdOut}`);
47744+
core.info(`stdErr is ${stdErr}`);
4774147745
if (stdErr) {
4774247746
throw new Error(stdErr);
4774347747
}
4774447748
if (!stdOut) {
47745-
throw new Error('Could not get version for yarn');
47749+
throw new Error(`Could not get version for ${toolName}`);
47750+
}
47751+
if (regex) {
47752+
core.info('add regex support');
47753+
toolVersion = stdOut;
47754+
}
47755+
else {
47756+
toolVersion = semver_1.major(stdOut).toString();
4774647757
}
47747-
return semver_1.major(stdOut);
47758+
return toolVersion;
4774847759
});
47749-
exports.getDefaultCacheDirectory = (tool) => __awaiter(void 0, void 0, void 0, function* () {
47760+
const getCmdCommand = (toolName, version) => {
47761+
let cmdCommand = toolName;
47762+
if (toolName === 'yarn') {
47763+
cmdCommand = `${toolName}${version}`;
47764+
}
47765+
return toolName;
47766+
};
47767+
exports.isToolSupported = toolName => {
47768+
const arr = Array.of(...Object.values(constants_1.LockType));
47769+
return arr.includes(toolName);
47770+
};
47771+
exports.getDefaultCacheDirectory = (toolName) => __awaiter(void 0, void 0, void 0, function* () {
4775047772
let stdOut;
4775147773
let stdErr;
47752-
const toolCommand = toolCommands[tool];
47774+
if (exports.isToolSupported(toolName)) {
47775+
core.info(`${toolName} is supported`);
47776+
}
47777+
const toolVersion = yield getToolVersion(toolName, '--version');
47778+
const fullToolName = getCmdCommand(toolName, toolVersion);
47779+
const toolCommand = toolCacheCommands[fullToolName];
4775347780
yield exec.exec(toolCommand, undefined, {
4775447781
listeners: {
4775547782
stderr: (err) => (stdErr = err.toString()),
@@ -50273,7 +50300,7 @@ const cache_1 = __webpack_require__(722);
5027350300
function run() {
5027450301
return __awaiter(this, void 0, void 0, function* () {
5027550302
const cacheLock = core.getInput(constants_1.Inputs.Cache);
50276-
if (cacheLock) {
50303+
if (cacheLock && cache_1.isToolSupported(cacheLock)) {
5027750304
try {
5027850305
exports.cachePackages(cacheLock);
5027950306
}
@@ -50283,15 +50310,10 @@ function run() {
5028350310
}
5028450311
});
5028550312
}
50286-
exports.cachePackages = (type) => __awaiter(void 0, void 0, void 0, function* () {
50287-
let tool = 'npm';
50313+
exports.cachePackages = (toolName) => __awaiter(void 0, void 0, void 0, function* () {
5028850314
const state = getCacheState();
5028950315
const primaryKey = core.getState(constants_1.State.CachePrimaryKey);
50290-
if (type === constants_1.LockType.Yarn) {
50291-
const yarnVersion = yield cache_1.getYarnVersion();
50292-
tool = `yarn${yarnVersion}`;
50293-
}
50294-
const cachePath = yield cache_1.getDefaultCacheDirectory(tool);
50316+
const cachePath = yield cache_1.getDefaultCacheDirectory(toolName);
5029550317
core.info(`cachePath is ${cachePath}`);
5029650318
core.info(`primaryKey is ${primaryKey}`);
5029750319
core.info(`state is ${state}`);

dist/setup/index.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43412,18 +43412,13 @@ const util = __importStar(__webpack_require__(669));
4341243412
const path = __importStar(__webpack_require__(622));
4341343413
const constants_1 = __webpack_require__(694);
4341443414
const cache_1 = __webpack_require__(913);
43415-
exports.restoreCache = (type, version) => __awaiter(void 0, void 0, void 0, function* () {
43416-
let tool = 'npm';
43415+
exports.restoreCache = (toolName, version) => __awaiter(void 0, void 0, void 0, function* () {
4341743416
const lockKey = core.getInput(constants_1.Inputs.Key, { required: true });
4341843417
const currentOs = process.env.RUNNER_OS;
4341943418
const fileHash = yield hashFile(lockKey);
43420-
if (type === constants_1.LockType.Yarn) {
43421-
const yarnVersion = yield cache_1.getYarnVersion();
43422-
tool = `yarn${yarnVersion}`;
43423-
}
43424-
const primaryKey = `${currentOs}-${tool}-${version}-${fileHash}`;
43419+
const primaryKey = `${currentOs}-${toolName}-${version}-${fileHash}`;
4342543420
core.saveState(constants_1.State.CachePrimaryKey, primaryKey);
43426-
const cachePath = yield cache_1.getDefaultCacheDirectory(tool);
43421+
const cachePath = yield cache_1.getDefaultCacheDirectory(toolName);
4342743422
core.info(`cachePath is ${cachePath}`);
4342843423
core.info(`primaryKey is ${primaryKey}`);
4342943424
const cacheKey = yield cache.restoreCache([cachePath], primaryKey);
@@ -55401,8 +55396,8 @@ var Inputs;
5540155396
})(Inputs = exports.Inputs || (exports.Inputs = {}));
5540255397
var LockType;
5540355398
(function (LockType) {
55404-
LockType[LockType["Npm"] = 0] = "Npm";
55405-
LockType[LockType["Yarn"] = 1] = "Yarn";
55399+
LockType["Npm"] = "npm";
55400+
LockType["Yarn"] = "yarn";
5540655401
})(LockType = exports.LockType || (exports.LockType = {}));
5540755402
var State;
5540855403
(function (State) {
@@ -64319,34 +64314,61 @@ var __importStar = (this && this.__importStar) || function (mod) {
6431964314
};
6432064315
Object.defineProperty(exports, "__esModule", { value: true });
6432164316
const exec = __importStar(__webpack_require__(986));
64317+
const core = __importStar(__webpack_require__(470));
64318+
const constants_1 = __webpack_require__(694);
6432264319
const semver_1 = __webpack_require__(280);
64323-
const toolCommands = {
64320+
const toolCacheCommands = {
6432464321
npm: 'npm config get cache',
6432564322
yarn1: 'yarn cache dir',
6432664323
yarn2: 'yarn config get cacheFolder'
6432764324
};
64328-
exports.getYarnVersion = () => __awaiter(void 0, void 0, void 0, function* () {
64325+
const getToolVersion = (toolName, command, regex) => __awaiter(void 0, void 0, void 0, function* () {
6432964326
let stdErr;
6433064327
let stdOut;
64331-
let majorVersion;
64332-
yield exec.exec('yarn --version', undefined, {
64328+
let toolVersion;
64329+
yield exec.exec(`${toolName} ${command}`, undefined, {
6433364330
listeners: {
6433464331
stdout: (err) => (stdOut = err.toString()),
6433564332
stderr: (out) => (stdErr = out.toString())
6433664333
}
6433764334
});
64335+
core.info(`stdout is ${stdOut}`);
64336+
core.info(`stdErr is ${stdErr}`);
6433864337
if (stdErr) {
6433964338
throw new Error(stdErr);
6434064339
}
6434164340
if (!stdOut) {
64342-
throw new Error('Could not get version for yarn');
64341+
throw new Error(`Could not get version for ${toolName}`);
64342+
}
64343+
if (regex) {
64344+
core.info('add regex support');
64345+
toolVersion = stdOut;
6434364346
}
64344-
return semver_1.major(stdOut);
64347+
else {
64348+
toolVersion = semver_1.major(stdOut).toString();
64349+
}
64350+
return toolVersion;
6434564351
});
64346-
exports.getDefaultCacheDirectory = (tool) => __awaiter(void 0, void 0, void 0, function* () {
64352+
const getCmdCommand = (toolName, version) => {
64353+
let cmdCommand = toolName;
64354+
if (toolName === 'yarn') {
64355+
cmdCommand = `${toolName}${version}`;
64356+
}
64357+
return toolName;
64358+
};
64359+
exports.isToolSupported = toolName => {
64360+
const arr = Array.of(...Object.values(constants_1.LockType));
64361+
return arr.includes(toolName);
64362+
};
64363+
exports.getDefaultCacheDirectory = (toolName) => __awaiter(void 0, void 0, void 0, function* () {
6434764364
let stdOut;
6434864365
let stdErr;
64349-
const toolCommand = toolCommands[tool];
64366+
if (exports.isToolSupported(toolName)) {
64367+
core.info(`${toolName} is supported`);
64368+
}
64369+
const toolVersion = yield getToolVersion(toolName, '--version');
64370+
const fullToolName = getCmdCommand(toolName, toolVersion);
64371+
const toolCommand = toolCacheCommands[fullToolName];
6435064372
yield exec.exec(toolCommand, undefined, {
6435164373
listeners: {
6435264374
stderr: (err) => (stdErr = err.toString()),

src/cache-restore.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,18 @@ import * as stream from 'stream';
77
import * as util from 'util';
88
import * as path from 'path';
99

10-
import {State, LockType, Inputs, Outputs} from './constants';
11-
import {getYarnVersion, getDefaultCacheDirectory} from './cache';
12-
13-
export const restoreCache = async (
14-
type: LockType | string,
15-
version: string
16-
) => {
17-
let tool = 'npm';
10+
import {State, Inputs, Outputs} from './constants';
11+
import {getDefaultCacheDirectory} from './cache';
1812

13+
export const restoreCache = async (toolName: string, version: string) => {
1914
const lockKey = core.getInput(Inputs.Key, {required: true});
2015
const currentOs = process.env.RUNNER_OS;
2116
const fileHash = await hashFile(lockKey);
2217

23-
if (type === LockType.Yarn) {
24-
const yarnVersion = await getYarnVersion();
25-
tool = `yarn${yarnVersion}`;
26-
}
27-
28-
const primaryKey = `${currentOs}-${tool}-${version}-${fileHash}`;
18+
const primaryKey = `${currentOs}-${toolName}-${version}-${fileHash}`;
2919
core.saveState(State.CachePrimaryKey, primaryKey);
3020

31-
const cachePath = await getDefaultCacheDirectory(tool);
21+
const cachePath = await getDefaultCacheDirectory(toolName);
3222
core.info(`cachePath is ${cachePath}`);
3323
core.info(`primaryKey is ${primaryKey}`);
3424
const cacheKey = await cache.restoreCache([cachePath], primaryKey);

src/cache-save.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as core from '@actions/core';
22
import * as cache from '@actions/cache';
3-
import {Inputs, LockType, State} from './constants';
4-
import {getDefaultCacheDirectory, getYarnVersion} from './cache';
3+
import {Inputs, State} from './constants';
4+
import {getDefaultCacheDirectory, isToolSupported} from './cache';
55

66
async function run() {
77
const cacheLock = core.getInput(Inputs.Cache);
8-
if (cacheLock) {
8+
if (cacheLock && isToolSupported(cacheLock)) {
99
try {
1010
cachePackages(cacheLock);
1111
} catch (error) {
@@ -14,18 +14,11 @@ async function run() {
1414
}
1515
}
1616

17-
export const cachePackages = async (type: LockType | string) => {
18-
let tool = 'npm';
19-
17+
export const cachePackages = async (toolName: string) => {
2018
const state = getCacheState();
2119
const primaryKey = core.getState(State.CachePrimaryKey);
2220

23-
if (type === LockType.Yarn) {
24-
const yarnVersion = await getYarnVersion();
25-
tool = `yarn${yarnVersion}`;
26-
}
27-
28-
const cachePath = await getDefaultCacheDirectory(tool);
21+
const cachePath = await getDefaultCacheDirectory(toolName);
2922
core.info(`cachePath is ${cachePath}`);
3023
core.info(`primaryKey is ${primaryKey}`);
3124
core.info(`state is ${state}`);

src/cache.ts

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,76 @@
11
import * as exec from '@actions/exec';
2+
import * as core from '@actions/core';
3+
import {LockType} from './constants';
24
import {major} from 'semver';
35

4-
const toolCommands = {
6+
const toolCacheCommands = {
57
npm: 'npm config get cache',
68
yarn1: 'yarn cache dir',
79
yarn2: 'yarn config get cacheFolder'
810
};
911

10-
export const getYarnVersion = async () => {
12+
const getToolVersion = async (
13+
toolName: string,
14+
command: string,
15+
regex?: RegExp | string
16+
) => {
1117
let stdErr: string | undefined;
1218
let stdOut: string | undefined;
13-
let majorVersion: string;
19+
let toolVersion: string;
1420

15-
await exec.exec('yarn --version', undefined, {
21+
await exec.exec(`${toolName} ${command}`, undefined, {
1622
listeners: {
1723
stdout: (err: Buffer) => (stdOut = err.toString()),
1824
stderr: (out: Buffer) => (stdErr = out.toString())
1925
}
2026
});
2127

28+
core.info(`stdout is ${stdOut}`);
29+
core.info(`stdErr is ${stdErr}`);
30+
2231
if (stdErr) {
2332
throw new Error(stdErr);
2433
}
2534

2635
if (!stdOut) {
27-
throw new Error('Could not get version for yarn');
36+
throw new Error(`Could not get version for ${toolName}`);
37+
}
38+
39+
if (regex) {
40+
core.info('add regex support');
41+
toolVersion = stdOut;
42+
} else {
43+
toolVersion = major(stdOut).toString();
44+
}
45+
46+
return toolVersion;
47+
};
48+
49+
const getCmdCommand = (toolName: string, version: string) => {
50+
let cmdCommand = toolName;
51+
if (toolName === 'yarn') {
52+
cmdCommand = `${toolName}${version}`;
2853
}
2954

30-
return major(stdOut);
55+
return toolName;
56+
};
57+
58+
export const isToolSupported = toolName => {
59+
const arr = Array.of<string>(...Object.values(LockType));
60+
return arr.includes(toolName);
3161
};
3262

33-
export const getDefaultCacheDirectory = async (tool: string) => {
63+
export const getDefaultCacheDirectory = async (toolName: string) => {
3464
let stdOut: string | undefined;
3565
let stdErr: string | undefined;
3666

37-
const toolCommand = toolCommands[tool];
67+
if (isToolSupported(toolName)) {
68+
core.info(`${toolName} is supported`);
69+
}
70+
71+
const toolVersion = await getToolVersion(toolName, '--version');
72+
const fullToolName = getCmdCommand(toolName, toolVersion);
73+
const toolCommand = toolCacheCommands[fullToolName];
3874

3975
await exec.exec(toolCommand, undefined, {
4076
listeners: {

src/constants.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ export enum Inputs {
1414
}
1515

1616
export enum LockType {
17-
Npm,
18-
Yarn
17+
Npm = 'npm',
18+
Yarn = 'yarn'
1919
}
2020

2121
export enum State {

0 commit comments

Comments
 (0)