Add logic for modules caching

Such files as cache-restore.ts, cache-utils.ts, constants.ts,
cache-save.ts we added. Main.ts file now incorporates logic for using
files mentioned above.
This commit is contained in:
Ivan Zosimov (Akvelon INC) 2022-02-21 16:21:14 +03:00
parent 5ba3482d38
commit 25a133c257
7 changed files with 198 additions and 2 deletions

49
src/cache-utils.ts Normal file
View file

@ -0,0 +1,49 @@
import * as core from '@actions/core';
import * as exec from '@actions/exec';
export interface PackageManagerInfo {
goSumFilePattern: string;
getCacheFolderCommand: string;
}
export const defaultPackageManager: PackageManagerInfo = {
goSumFilePattern: 'go.sum',
getCacheFolderCommand: 'go env GOMODCACHE',
};
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();
};
export const getPackageManagerInfo = async () => {
return defaultPackageManager;
};
export const getCacheDirectoryPath = async (
packageManagerInfo: PackageManagerInfo,
) => {
const stdout = await getCommandOutput(
packageManagerInfo.getCacheFolderCommand
);
if (!stdout) {
throw new Error(`Could not get cache folder path.`);
}
return stdout;
};