-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: update node modules cache key (#68)
- Loading branch information
Showing
4 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |