Skip to content

Commit 292e405

Browse files
committed
fix: add deps hash to cache key
1 parent d568f20 commit 292e405

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/dependencies.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import fs from 'fs/promises'
22
import os from 'os'
3-
import { join } from 'path'
3+
import { join, dirname } from 'path'
4+
import { createHash } from 'crypto'
5+
import { fileURLToPath } from 'url'
46
import {
57
saveCache,
68
restoreCache
@@ -9,14 +11,27 @@ import { exec } from '@actions/exec'
911
import { getQuery } from '@simple-release/config'
1012
import requireResolve from './resolve.cjs'
1113

12-
const NPM_CLI = requireResolve('npm').replace('index.js', join('bin', 'npm-cli.js'))
14+
const DIR = dirname(fileURLToPath(import.meta.url))
1315
const DEPENDENCIES_DIR = join(os.homedir(), '.simple-release-dependencies')
16+
const NPM_CLI = requireResolve('npm').replace('index.js', join('bin', 'npm-cli.js'))
17+
18+
async function getDependenciesHash() {
19+
try {
20+
const packageLockPath = join(DIR, '..', 'package-lock.json')
21+
const packageLockContent = await fs.readFile(packageLockPath, 'utf8')
22+
23+
return createHash('sha256').update(packageLockContent).digest('hex').substring(0, 8)
24+
} catch {
25+
return ''
26+
}
27+
}
1428

15-
function getCacheKeyFromConfig(config) {
29+
async function getCacheKeyFromConfig(config) {
1630
const projectQuery = getQuery(config.project)
1731
const hostingQuery = getQuery(config.hosting)
32+
const packageLockHash = await getDependenciesHash()
1833

19-
return [projectQuery, hostingQuery].filter(Boolean).join('+')
34+
return [projectQuery, hostingQuery, packageLockHash].filter(Boolean).join('+')
2035
}
2136

2237
async function install(pkg, version) {
@@ -55,7 +70,7 @@ let cacheKey = null
5570

5671
export async function lazyDependencyImport(pkg, version, config) {
5772
if (cacheStatus === CACHE_NOT_CHECKED) {
58-
cacheKey = getCacheKeyFromConfig(config)
73+
cacheKey = await getCacheKeyFromConfig(config)
5974

6075
const hit = await restoreCache(
6176
[DEPENDENCIES_DIR],

0 commit comments

Comments
 (0)