2022-05-25 18:07:29 +08:00
|
|
|
import * as core from '@actions/core';
|
|
|
|
import * as cache from '@actions/cache';
|
|
|
|
import fs from 'fs';
|
|
|
|
import {State} from './constants';
|
|
|
|
import {getCacheDirectoryPath, 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 {
|
|
|
|
await cachePackages();
|
|
|
|
} catch (error) {
|
2022-09-08 19:18:41 +08:00
|
|
|
let message = 'Unknown error!';
|
|
|
|
if (error instanceof Error) {
|
|
|
|
message = error.message;
|
|
|
|
}
|
|
|
|
if (typeof error === 'string') {
|
|
|
|
message = error;
|
|
|
|
}
|
|
|
|
core.setFailed(message);
|
2022-05-25 18:07:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const cachePackages = async () => {
|
|
|
|
const cacheInput = core.getBooleanInput('cache');
|
|
|
|
if (!cacheInput) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const packageManager = 'default';
|
|
|
|
|
|
|
|
const state = core.getState(State.CacheMatchedKey);
|
|
|
|
const primaryKey = core.getState(State.CachePrimaryKey);
|
|
|
|
|
|
|
|
const packageManagerInfo = await getPackageManagerInfo(packageManager);
|
|
|
|
|
|
|
|
const cachePaths = await getCacheDirectoryPath(packageManagerInfo);
|
|
|
|
|
|
|
|
const nonExistingPaths = cachePaths.filter(
|
|
|
|
cachePath => !fs.existsSync(cachePath)
|
|
|
|
);
|
|
|
|
|
|
|
|
if (nonExistingPaths.length === cachePaths.length) {
|
2022-09-08 18:29:13 +08:00
|
|
|
core.warning(`There are no cache folders on the disk`);
|
|
|
|
return;
|
2022-05-25 18:07:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (nonExistingPaths.length) {
|
|
|
|
logWarning(
|
|
|
|
`Cache folder path is retrieved but doesn't exist on disk: ${nonExistingPaths.join(
|
|
|
|
', '
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (primaryKey === state) {
|
|
|
|
core.info(
|
|
|
|
`Cache hit occurred on the primary key ${primaryKey}, not saving cache.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-28 20:17:12 +08:00
|
|
|
const cacheId = await cache.saveCache(cachePaths, primaryKey);
|
2022-07-01 15:25:30 +08:00
|
|
|
if (cacheId === -1) {
|
2022-06-28 20:17:12 +08:00
|
|
|
return;
|
2022-05-25 18:07:29 +08:00
|
|
|
}
|
2022-06-28 20:17:12 +08:00
|
|
|
core.info(`Cache saved with the key: ${primaryKey}`);
|
2022-05-25 18:07:29 +08:00
|
|
|
};
|
|
|
|
|
2022-09-08 19:18:41 +08:00
|
|
|
function logWarning(message: string): void {
|
2022-05-25 18:07:29 +08:00
|
|
|
const warningPrefix = '[warning]';
|
|
|
|
core.info(`${warningPrefix}${message}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|