2021-06-16 14:52:44 +08:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as cache from '@actions/cache';
|
2023-07-13 20:03:41 +08:00
|
|
|
|
2021-06-16 14:52:44 +08:00
|
|
|
import {State} from './constants';
|
2023-06-21 23:52:17 +08:00
|
|
|
import {getPackageManagerInfo} from './cache-utils';
|
2021-06-16 14:52:44 +08:00
|
|
|
|
2021-12-28 17:25:43 +08: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 14:52:44 +08:00
|
|
|
export async function run() {
|
|
|
|
try {
|
2023-08-10 22:32:24 +08:00
|
|
|
const cacheLock = core.getState(State.CachePackageManager);
|
2021-06-16 14:52:44 +08: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);
|
2023-08-15 21:53:46 +08:00
|
|
|
const cachePaths = JSON.parse(
|
2023-07-13 20:03:41 +08:00
|
|
|
core.getState(State.CachePaths) || '[]'
|
|
|
|
) as string[];
|
2021-06-16 14:52:44 +08:00
|
|
|
|
|
|
|
const packageManagerInfo = await getPackageManagerInfo(packageManager);
|
|
|
|
if (!packageManagerInfo) {
|
|
|
|
core.debug(`Caching for '${packageManager}' is not supported`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-13 20:03:41 +08:00
|
|
|
if (!cachePaths.length) {
|
2023-06-21 23:52:17 +08:00
|
|
|
// TODO: core.getInput has a bug - it can return undefined despite its definition (tests only?)
|
|
|
|
// export declare function getInput(name: string, options?: InputOptions): string;
|
|
|
|
const cacheDependencyPath = core.getInput('cache-dependency-path') || '';
|
2021-07-14 22:25:45 +08:00
|
|
|
throw new Error(
|
2023-06-21 23:52:17 +08:00
|
|
|
`Cache folder paths are not retrieved for ${packageManager} with cache-dependency-path = ${cacheDependencyPath}`
|
2021-07-14 22:25:45 +08:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:52:44 +08:00
|
|
|
if (primaryKey === state) {
|
|
|
|
core.info(
|
|
|
|
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-21 23:52:17 +08:00
|
|
|
const cacheId = await cache.saveCache(cachePaths, primaryKey);
|
2022-06-27 16:11:42 +08:00
|
|
|
if (cacheId == -1) {
|
|
|
|
return;
|
2021-06-16 14:52:44 +08:00
|
|
|
}
|
2022-06-27 16:11:42 +08:00
|
|
|
|
|
|
|
core.info(`Cache saved with the key: ${primaryKey}`);
|
2021-06-16 14:52:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
run();
|