mirror of
https://github.com/pnpm/action-setup.git
synced 2026-01-14 12:31:17 +08:00
* add pnpm store caching * style: format * no semicolons * no star imports * import order * style: no star imports --------- Co-authored-by: khai96_ <hvksmr1996@gmail.com>
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
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
|
|
}
|