( packageManager: string, cacheDependencyPath: string )
| 14 | } from './cache-utils'; |
| 15 | |
| 16 | export const restoreCache = async ( |
| 17 | packageManager: string, |
| 18 | cacheDependencyPath: string |
| 19 | ) => { |
| 20 | const packageManagerInfo = await getPackageManagerInfo(packageManager); |
| 21 | if (!packageManagerInfo) { |
| 22 | throw new Error(`Caching for '${packageManager}' is not supported`); |
| 23 | } |
| 24 | const platform = process.env.RUNNER_OS; |
| 25 | const arch = os.arch(); |
| 26 | |
| 27 | const cachePaths = await getCacheDirectories( |
| 28 | packageManagerInfo, |
| 29 | cacheDependencyPath |
| 30 | ); |
| 31 | core.saveState(State.CachePaths, cachePaths); |
| 32 | const lockFilePath = cacheDependencyPath |
| 33 | ? cacheDependencyPath |
| 34 | : findLockFile(packageManagerInfo); |
| 35 | const fileHash = await glob.hashFiles(lockFilePath); |
| 36 | |
| 37 | if (!fileHash) { |
| 38 | throw new Error( |
| 39 | 'Some specified paths were not resolved, unable to cache dependencies.' |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | const keyPrefix = `node-cache-${platform}-${arch}-${packageManager}`; |
| 44 | const primaryKey = `${keyPrefix}-${fileHash}`; |
| 45 | core.debug(`primary key is ${primaryKey}`); |
| 46 | |
| 47 | core.saveState(State.CachePrimaryKey, primaryKey); |
| 48 | |
| 49 | const isManagedByYarnBerry = await repoHasYarnBerryManagedDependencies( |
| 50 | packageManagerInfo, |
| 51 | cacheDependencyPath |
| 52 | ); |
| 53 | let cacheKey: string | undefined; |
| 54 | if (isManagedByYarnBerry) { |
| 55 | core.info( |
| 56 | 'All dependencies are managed locally by yarn3, the previous cache can be used' |
| 57 | ); |
| 58 | cacheKey = await cache.restoreCache(cachePaths, primaryKey, [keyPrefix]); |
| 59 | } else { |
| 60 | cacheKey = await cache.restoreCache(cachePaths, primaryKey); |
| 61 | } |
| 62 | |
| 63 | core.setOutput('cache-hit', Boolean(cacheKey)); |
| 64 | |
| 65 | if (!cacheKey) { |
| 66 | core.info(`${packageManager} cache is not found`); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | core.saveState(State.CacheMatchedKey, cacheKey); |
| 71 | core.info(`Cache restored from key: ${cacheKey}`); |
| 72 | }; |
| 73 |
no test coverage detected