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-restore.ts
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
57 lines (46 sloc)
1.71 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 cache from '@actions/cache'; | |
import * as core from '@actions/core'; | |
import * as glob from '@actions/glob'; | |
import path from 'path'; | |
import fs from 'fs'; | |
import {State, Outputs} from './constants'; | |
import { | |
getCacheDirectoryPath, | |
getPackageManagerInfo, | |
PackageManagerInfo | |
} from './cache-utils'; | |
export const restoreCache = async (packageManager: string) => { | |
const packageManagerInfo = await getPackageManagerInfo(packageManager); | |
if (!packageManagerInfo) { | |
throw new Error(`Caching for '${packageManager}' is not supported`); | |
} | |
const platform = process.env.RUNNER_OS; | |
const cachePath = await getCacheDirectoryPath( | |
packageManagerInfo, | |
packageManager | |
); | |
const lockFilePath = findLockFile(packageManagerInfo); | |
const fileHash = await glob.hashFiles(lockFilePath); | |
const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`; | |
core.debug(`primary key is ${primaryKey}`); | |
core.saveState(State.CachePrimaryKey, primaryKey); | |
const cacheKey = await cache.restoreCache([cachePath], primaryKey); | |
if (!cacheKey) { | |
core.info(`${packageManager} cache is not found`); | |
return; | |
} | |
core.saveState(State.CacheMatchedKey, cacheKey); | |
core.info(`Cache restored from key: ${cacheKey}`); | |
}; | |
const findLockFile = (packageManager: PackageManagerInfo) => { | |
let lockFiles = packageManager.lockFilePatterns; | |
const workspace = process.env.GITHUB_WORKSPACE!; | |
const rootContent = fs.readdirSync(workspace); | |
const lockFile = lockFiles.find(item => rootContent.includes(item)); | |
if (!lockFile) { | |
throw new Error( | |
`Dependencies lock file is not found in ${workspace}. Supported file patterns: ${lockFiles.toString()}` | |
); | |
} | |
return path.join(workspace, lockFile); | |
}; |