From 834c68a6f5cd8c6efefee7d3a6683c962af76d2a Mon Sep 17 00:00:00 2001 From: Le Vivilet <72156503+levivilet@users.noreply.github.com> Date: Tue, 22 Oct 2024 21:46:27 +0200 Subject: [PATCH] feature: update node modules cache key (#68) --- .github/workflows/ci.yml | 2 +- .github/workflows/pr.yml | 2 +- .github/workflows/release.yml | 2 +- .../build/src/computeNodeModulesCacheKey.js | 70 +++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 packages/build/src/computeNodeModulesCacheKey.js diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8a4c91e..f5fba82 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index 3bf3e3d..5f39823 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 5c3ec83..ab8866e 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -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 diff --git a/packages/build/src/computeNodeModulesCacheKey.js b/packages/build/src/computeNodeModulesCacheKey.js new file mode 100644 index 0000000..bced2a1 --- /dev/null +++ b/packages/build/src/computeNodeModulesCacheKey.js @@ -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()