Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
setup-node/src/cache-save.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (44 sloc)
1.33 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as core from '@actions/core'; | |
import * as cache from '@actions/cache'; | |
import {State} from './constants'; | |
import {getCacheDirectoryPath, getPackageManagerInfo} from './cache-utils'; | |
export async function run() { | |
try { | |
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 | |
); | |
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(); |