2022-02-21 16:21:14 +03:00
|
|
|
import * as exec from '@actions/exec';
|
2022-02-22 15:42:32 +03:00
|
|
|
import {supportedPackageManagers, PackageManagerInfo} from './package-managers';
|
2022-02-21 16:21:14 +03:00
|
|
|
|
|
|
|
export const getCommandOutput = async (toolCommand: string) => {
|
|
|
|
let {stdout, stderr, exitCode} = await exec.getExecOutput(
|
|
|
|
toolCommand,
|
|
|
|
undefined,
|
|
|
|
{ignoreReturnCode: true}
|
|
|
|
);
|
|
|
|
|
|
|
|
if (exitCode) {
|
|
|
|
stderr = !stderr.trim()
|
|
|
|
? `The '${toolCommand}' command failed with exit code: ${exitCode}`
|
|
|
|
: stderr;
|
|
|
|
throw new Error(stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout.trim();
|
|
|
|
};
|
|
|
|
|
2022-02-22 14:55:30 +03:00
|
|
|
export const getPackageManagerInfo = async (packageManager: string) => {
|
2022-02-22 15:11:41 +03:00
|
|
|
if (!supportedPackageManagers[packageManager]) {
|
2022-02-22 14:55:30 +03:00
|
|
|
throw new Error(
|
|
|
|
`It's not possible to use ${packageManager}, please, check correctness of the package manager name spelling.`
|
|
|
|
);
|
|
|
|
}
|
2022-02-28 15:30:28 +03:00
|
|
|
const obtainedPackageManager = supportedPackageManagers[packageManager];
|
2022-02-22 14:55:30 +03:00
|
|
|
|
2022-02-28 14:02:27 +03:00
|
|
|
return obtainedPackageManager;
|
2022-02-21 16:21:14 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export const getCacheDirectoryPath = async (
|
2022-02-21 16:29:32 +03:00
|
|
|
packageManagerInfo: PackageManagerInfo
|
2022-02-21 16:21:14 +03:00
|
|
|
) => {
|
|
|
|
const stdout = await getCommandOutput(
|
|
|
|
packageManagerInfo.getCacheFolderCommand
|
2022-02-21 16:29:32 +03:00
|
|
|
);
|
2022-02-21 16:21:14 +03:00
|
|
|
|
|
|
|
if (!stdout) {
|
|
|
|
throw new Error(`Could not get cache folder path.`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return stdout;
|
|
|
|
};
|