| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  | 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 { | 
					
						
							| 
									
										
										
										
											2021-06-25 12:06:49 +03:00
										 |  |  |     const cacheLock = core.getInput('cache'); | 
					
						
							| 
									
										
										
										
											2021-06-16 09:52:44 +03:00
										 |  |  |     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(); |