1
0
Fork 0
mirror of https://github.com/actions/setup-node synced 2024-12-23 13:42:42 +00:00
setup-node/src/cache-save.ts

65 lines
2 KiB
TypeScript
Raw Normal View History

import * as core from '@actions/core';
import * as cache from '@actions/cache';
2023-07-13 12:03:41 +00:00
import fs from 'fs';
import {State} from './constants';
import {getPackageManagerInfo} from './cache-utils';
// 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}`);
});
export async function run() {
try {
const cacheLock = core.getState(State.CachePackageManager);
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-07-13 12:03:41 +00:00
let cachePaths = JSON.parse(
core.getState(State.CachePaths) || '[]'
) as string[];
cachePaths = cachePaths.filter(fs.existsSync);
const packageManagerInfo = await getPackageManagerInfo(packageManager);
if (!packageManagerInfo) {
core.debug(`Caching for '${packageManager}' is not supported`);
return;
}
2023-07-13 12:03:41 +00:00
if (!cachePaths.length) {
// 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 14:25:45 +00:00
throw new Error(
`Cache folder paths are not retrieved for ${packageManager} with cache-dependency-path = ${cacheDependencyPath}`
2021-07-14 14:25:45 +00:00
);
}
if (primaryKey === state) {
core.info(
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
);
return;
}
const cacheId = await cache.saveCache(cachePaths, primaryKey);
if (cacheId == -1) {
return;
}
core.info(`Cache saved with the key: ${primaryKey}`);
};
run();