mirror of
https://github.com/pnpm/action-setup.git
synced 2026-01-17 06:21:16 +08:00
feat: store caching (#188)
* add pnpm store caching * style: format * no semicolons * no star imports * import order * style: no star imports --------- Co-authored-by: khai96_ <hvksmr1996@gmail.com>
This commit is contained in:
39
src/cache-restore/run.ts
Normal file
39
src/cache-restore/run.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { restoreCache } from '@actions/cache'
|
||||
import { debug, info, saveState, setOutput } from '@actions/core'
|
||||
import { getExecOutput } from '@actions/exec'
|
||||
import { hashFiles } from '@actions/glob'
|
||||
import os from 'os'
|
||||
import { Inputs } from '../inputs'
|
||||
|
||||
export async function runRestoreCache(inputs: Inputs) {
|
||||
const cachePath = await getCacheDirectory()
|
||||
saveState('cache_path', cachePath)
|
||||
|
||||
const fileHash = await hashFiles(inputs.cacheDependencyPath)
|
||||
if (!fileHash) {
|
||||
throw new Error('Some specified paths were not resolved, unable to cache dependencies.')
|
||||
}
|
||||
|
||||
const primaryKey = `pnpm-cache-${process.env.RUNNER_OS}-${os.arch()}-${fileHash}`
|
||||
debug(`Primary key is ${primaryKey}`)
|
||||
saveState('cache_primary_key', primaryKey)
|
||||
|
||||
let cacheKey = await restoreCache([cachePath], primaryKey)
|
||||
|
||||
setOutput('cache-hit', Boolean(cacheKey))
|
||||
|
||||
if (!cacheKey) {
|
||||
info(`Cache is not found`)
|
||||
return
|
||||
}
|
||||
|
||||
saveState('cache_restored_key', cacheKey)
|
||||
info(`Cache restored from key: ${cacheKey}`)
|
||||
}
|
||||
|
||||
async function getCacheDirectory() {
|
||||
const { stdout } = await getExecOutput('pnpm store path --silent')
|
||||
const cacheFolderPath = stdout.trim()
|
||||
debug(`Cache folder is set to "${cacheFolderPath}"`)
|
||||
return cacheFolderPath
|
||||
}
|
||||
Reference in New Issue
Block a user