Skip to content

Commit

Permalink
feature: update node modules cache key (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
levivilet authored Oct 22, 2024
1 parent 2f8cc5c commit 834c68a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
node-version-file: '.nvmrc'
- name: Compute node modules cache key
id: nodeModulesCacheKey
run: echo "value=$(node scripts/computeNodeModulesCacheKey.js)" >> $GITHUB_OUTPUT
run: echo "value=$(node packages/build/src/computeNodeModulesCacheKey.js)" >> $GITHUB_OUTPUT
shell: bash
- uses: actions/cache@v4
id: npm-cache
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:
node-version-file: '.nvmrc'
- name: Compute node modules cache key
id: nodeModulesCacheKey
run: echo "value=$(node scripts/computeNodeModulesCacheKey.js)" >> $GITHUB_OUTPUT
run: echo "value=$(node packages/build/src/computeNodeModulesCacheKey.js)" >> $GITHUB_OUTPUT
shell: bash
- uses: actions/cache@v4
id: npm-cache
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
node-version-file: '.nvmrc'
- name: Compute node modules cache key
id: nodeModulesCacheKey
run: echo "value=$(node scripts/computeNodeModulesCacheKey.js)" >> $GITHUB_OUTPUT
run: echo "value=$(node packages/build/src/computeNodeModulesCacheKey.js)" >> $GITHUB_OUTPUT
shell: bash
- uses: actions/cache@v4
id: npm-cache
Expand Down
70 changes: 70 additions & 0 deletions packages/build/src/computeNodeModulesCacheKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { createHash } from 'node:crypto'
import { readdirSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'

const __dirname = dirname(fileURLToPath(import.meta.url))

const root = join(__dirname, '..', '..', '..')

const getPackageLocations = () => {
const packageLocations = []
const packagesFolder = join(root, 'packages')
const dirents = readdirSync(packagesFolder)
for (const dirent of dirents) {
packageLocations.push(`packages/${dirent}/package-lock.json`)
}
packageLocations.push('package-lock.json')
return packageLocations
}

const locations = [
'lerna.json',
...getPackageLocations(),
'.github/workflows/pr.yml',
'.github/workflows/ci.yml',
'.github/workflows/release.yml',
'packages/build/src/computeNodeModulesCacheKey.js',
]

const packagesFolder = join(root, 'packages')

const dirents = readdirSync(packagesFolder)
for (const dirent of dirents) {
locations.push(`packages/${dirent}/package-lock.json`)
}

const getAbsolutePath = (relativePath) => {
return join(root, relativePath)
}

const getContent = (absolutePath) => {
return readFile(absolutePath, 'utf8')
}

export const computeHash = (contents) => {
const hash = createHash('sha1')
if (Array.isArray(contents)) {
for (const content of contents) {
hash.update(content)
}
} else if (typeof contents === 'string') {
hash.update(contents)
}
return hash.digest('hex')
}

const computeCacheKey = async (locations) => {
const absolutePaths = locations.map(getAbsolutePath)
const contents = await Promise.all(absolutePaths.map(getContent))
const hash = computeHash(contents)
return hash
}

const main = async () => {
const hash = await computeCacheKey(locations)
process.stdout.write(hash)
}

main()

0 comments on commit 834c68a

Please sign in to comment.