3
0
Fork 0
mirror of https://gitea.com/actions/setup-node.git synced 2024-11-22 18:19:33 +01:00
setup-node/src/cache-save.ts

59 lines
1.5 KiB
TypeScript
Raw Normal View History

import * as core from '@actions/core';
import * as cache from '@actions/cache';
2021-07-14 16:25:45 +02:00
import fs from 'fs';
import {State} from './constants';
import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils';
export async function run() {
try {
2021-06-25 11:06:49 +02:00
const cacheLock = core.getInput('cache');
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 16:25:45 +02:00
if (!fs.existsSync(cachePath)) {
throw new Error(
`Cache folder path is retrieved for ${packageManager} but doesn't exist on disk: ${cachePath}`
);
}
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();