2021-06-16 06:52:44 +00:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as exec from '@actions/exec';
|
2021-07-14 12:06:33 +00:00
|
|
|
import os from 'os';
|
|
|
|
import path from 'path';
|
2021-06-16 06:52:44 +00:00
|
|
|
|
|
|
|
type SupportedPackageManagers = {
|
|
|
|
[prop: string]: PackageManagerInfo;
|
|
|
|
};
|
|
|
|
|
|
|
|
export interface PackageManagerInfo {
|
|
|
|
lockFilePatterns: Array<string>;
|
|
|
|
getCacheFolderCommand: string;
|
2021-06-30 15:44:51 +00:00
|
|
|
defaultCacheFolder?: string;
|
2021-06-16 06:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const supportedPackageManagers: SupportedPackageManagers = {
|
|
|
|
npm: {
|
|
|
|
lockFilePatterns: ['package-lock.json', 'yarn.lock'],
|
|
|
|
getCacheFolderCommand: 'npm config get cache'
|
|
|
|
},
|
2021-06-30 15:44:51 +00:00
|
|
|
pnpm: {
|
|
|
|
lockFilePatterns: ['pnpm-lock.yaml'],
|
|
|
|
getCacheFolderCommand: 'pnpm get store',
|
2021-07-14 12:06:33 +00:00
|
|
|
defaultCacheFolder: path.join(os.homedir(), '.pnpm-store')
|
2021-06-30 15:44:51 +00:00
|
|
|
},
|
2021-06-16 06:52:44 +00:00
|
|
|
yarn1: {
|
|
|
|
lockFilePatterns: ['yarn.lock'],
|
|
|
|
getCacheFolderCommand: 'yarn cache dir'
|
|
|
|
},
|
|
|
|
yarn2: {
|
|
|
|
lockFilePatterns: ['yarn.lock'],
|
|
|
|
getCacheFolderCommand: 'yarn config get cacheFolder'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getCommandOutput = async (toolCommand: string) => {
|
|
|
|
const {stdout, stderr, exitCode} = await exec.getExecOutput(toolCommand);
|
|
|
|
|
|
|
|
if (stderr) {
|
|
|
|
throw new Error(stderr);
|
|
|
|
}
|
|
|
|
|
2021-06-30 15:44:51 +00:00
|
|
|
return stdout.trim();
|
2021-06-16 06:52:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const getPackageManagerVersion = async (
|
|
|
|
packageManager: string,
|
|
|
|
command: string
|
|
|
|
) => {
|
|
|
|
const stdOut = await getCommandOutput(`${packageManager} ${command}`);
|
|
|
|
|
|
|
|
if (!stdOut) {
|
|
|
|
throw new Error(`Could not retrieve version of ${packageManager}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdOut;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getPackageManagerInfo = async (packageManager: string) => {
|
|
|
|
if (packageManager === 'npm') {
|
|
|
|
return supportedPackageManagers.npm;
|
2021-06-30 15:44:51 +00:00
|
|
|
} else if (packageManager === 'pnpm') {
|
|
|
|
return supportedPackageManagers.pnpm;
|
2021-06-16 06:52:44 +00:00
|
|
|
} else if (packageManager === 'yarn') {
|
|
|
|
const yarnVersion = await getPackageManagerVersion('yarn', '--version');
|
|
|
|
|
|
|
|
core.debug(`Consumed yarn version is ${yarnVersion}`);
|
|
|
|
|
|
|
|
if (yarnVersion.startsWith('1.')) {
|
|
|
|
return supportedPackageManagers.yarn1;
|
|
|
|
} else {
|
|
|
|
return supportedPackageManagers.yarn2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getCacheDirectoryPath = async (
|
|
|
|
packageManagerInfo: PackageManagerInfo,
|
|
|
|
packageManager: string
|
|
|
|
) => {
|
2021-06-30 15:44:51 +00:00
|
|
|
let stdOut = await getCommandOutput(packageManagerInfo.getCacheFolderCommand);
|
|
|
|
|
|
|
|
// pnpm returns 'undefined' if no custom store path is set
|
|
|
|
if (stdOut === 'undefined') {
|
|
|
|
stdOut = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!stdOut && packageManagerInfo.defaultCacheFolder) {
|
|
|
|
stdOut = packageManagerInfo.defaultCacheFolder;
|
|
|
|
}
|
2021-06-16 06:52:44 +00:00
|
|
|
|
|
|
|
if (!stdOut) {
|
|
|
|
throw new Error(`Could not get cache folder path for ${packageManager}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
core.debug(`${packageManager} path is ${stdOut}`);
|
|
|
|
|
|
|
|
return stdOut;
|
|
|
|
};
|