2021-06-16 06:52:44 +00:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as cache from '@actions/cache';
|
2021-07-14 14:25:45 +00:00
|
|
|
import fs from 'fs';
|
2021-06-16 06:52:44 +00:00
|
|
|
import {State} from './constants';
|
|
|
|
import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils';
|
|
|
|
|
2021-12-28 09:25:43 +00:00
|
|
|
// Catch and log any unhandled exceptions. These exceptions can leak out of the uploadChunk method in
|
|
|
|
// @actions/toolkit when a failed upload closes the file descriptor causing any in-process reads to
|
|
|
|
// throw an uncaught exception. Instead of failing this action, just warn.
|
|
|
|
process.on('uncaughtException', e => {
|
|
|
|
const warningPrefix = '[warning]';
|
|
|
|
core.info(`${warningPrefix}${e.message}`);
|
|
|
|
});
|
|
|
|
|
2021-06-16 06:52:44 +00:00
|
|
|
export async function run() {
|
|
|
|
try {
|
2021-06-25 09:06:49 +00:00
|
|
|
const cacheLock = core.getInput('cache');
|
2021-06-16 06:52:44 +00:00
|
|
|
await cachePackages(cacheLock);
|
|
|
|
} catch (error) {
|
|
|
|
core.setFailed(error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cachePackages = async (packageManager: string) => {
|
|
|
|
const state = core.getState(State.CacheMatchedKey);
|
|
|
|
const primaryKey = core.getState(State.CachePrimaryKey);
|
|
|
|
|
|
|
|
const packageManagerInfo = await getPackageManagerInfo(packageManager);
|
|
|
|
if (!packageManagerInfo) {
|
|
|
|
core.debug(`Caching for '${packageManager}' is not supported`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const cachePath = await getCacheDirectoryPath(
|
|
|
|
packageManagerInfo,
|
|
|
|
packageManager
|
|
|
|
);
|
2021-07-14 14:25:45 +00:00
|
|
|
|
|
|
|
if (!fs.existsSync(cachePath)) {
|
|
|
|
throw new Error(
|
|
|
|
`Cache folder path is retrieved for ${packageManager} but doesn't exist on disk: ${cachePath}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-16 06:52:44 +00:00
|
|
|
if (primaryKey === state) {
|
|
|
|
core.info(
|
|
|
|
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await cache.saveCache([cachePath], primaryKey);
|
|
|
|
core.info(`Cache saved with the key: ${primaryKey}`);
|
|
|
|
} catch (error) {
|
|
|
|
if (error.name === cache.ValidationError.name) {
|
|
|
|
throw error;
|
|
|
|
} else if (error.name === cache.ReserveCacheError.name) {
|
|
|
|
core.info(error.message);
|
|
|
|
} else {
|
|
|
|
core.warning(`${error.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
run();
|