| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  | 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'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-17 14:35:58 +01:00
										 |  |  | import {State} from './constants'; | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  | import { | 
					
						
							| 
									
										
										
										
											2023-06-21 17:52:17 +02:00
										 |  |  |   getCacheDirectories, | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  |   getPackageManagerInfo, | 
					
						
							|  |  |  |   PackageManagerInfo | 
					
						
							|  |  |  | } from './cache-utils'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 20:44:59 +03:00
										 |  |  | export const restoreCache = async ( | 
					
						
							|  |  |  |   packageManager: string, | 
					
						
							| 
									
										
										
										
											2023-06-21 17:52:17 +02:00
										 |  |  |   cacheDependencyPath: string | 
					
						
							| 
									
										
										
										
											2021-08-02 20:44:59 +03:00
										 |  |  | ) => { | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  |   const packageManagerInfo = await getPackageManagerInfo(packageManager); | 
					
						
							|  |  |  |   if (!packageManagerInfo) { | 
					
						
							|  |  |  |     throw new Error(`Caching for '${packageManager}' is not supported`); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   const platform = process.env.RUNNER_OS; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 17:52:17 +02:00
										 |  |  |   const cachePaths = await getCacheDirectories( | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  |     packageManagerInfo, | 
					
						
							| 
									
										
										
										
											2023-06-21 17:52:17 +02:00
										 |  |  |     cacheDependencyPath | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2023-06-21 17:52:17 +02:00
										 |  |  |   core.saveState(State.CachePaths, cachePaths); | 
					
						
							| 
									
										
										
										
											2021-08-02 20:44:59 +03:00
										 |  |  |   const lockFilePath = cacheDependencyPath | 
					
						
							|  |  |  |     ? cacheDependencyPath | 
					
						
							|  |  |  |     : findLockFile(packageManagerInfo); | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  |   const fileHash = await glob.hashFiles(lockFilePath); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-02 20:44:59 +03:00
										 |  |  |   if (!fileHash) { | 
					
						
							|  |  |  |     throw new Error( | 
					
						
							|  |  |  |       'Some specified paths were not resolved, unable to cache dependencies.' | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-25 12:06:49 +03:00
										 |  |  |   const primaryKey = `node-cache-${platform}-${packageManager}-${fileHash}`; | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  |   core.debug(`primary key is ${primaryKey}`); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   core.saveState(State.CachePrimaryKey, primaryKey); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-21 17:52:17 +02:00
										 |  |  |   const cacheKey = await cache.restoreCache(cachePaths, primaryKey); | 
					
						
							| 
									
										
										
										
											2021-09-03 18:34:37 +00:00
										 |  |  |   core.setOutput('cache-hit', Boolean(cacheKey)); | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |   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) => { | 
					
						
							| 
									
										
										
										
											2023-03-08 10:47:38 +02:00
										 |  |  |   const lockFiles = packageManager.lockFilePatterns; | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  |   const workspace = process.env.GITHUB_WORKSPACE!; | 
					
						
							| 
									
										
										
										
											2023-06-21 17:52:17 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  |   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); | 
					
						
							|  |  |  | }; |