Skip to content

Commit

Permalink
fix: hash generated files (#15253)
Browse files Browse the repository at this point in the history
* fix: hash generated files

* chore: nx format:write update dirty files

---------

Co-authored-by: andes-it <[email protected]>
  • Loading branch information
lodmfjord and andes-it committed Jun 18, 2024
1 parent c6dffb8 commit fc305bb
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 29 deletions.
4 changes: 2 additions & 2 deletions scripts/ci/cache/__config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getYarnLockHash,
getPackageHash,
getNodeVersionString,
getGeneratedFileHash,
folderSizeIsEqualOrGreaterThan,
runCommand,
fileSizeIsEqualOrGreaterThan,
Expand All @@ -20,6 +19,7 @@ import {
ENV_CACHE_SUCCESS,
} from './_const.mjs'
import { keyStorage } from './_key_storage.mjs'
import { getGeneratedFilesHash } from './_generated_files.mjs'

// When testing this is good to manipulate
const HASH_VERSION = `newcache-${6}`
Expand Down Expand Up @@ -86,7 +86,7 @@ export const caches = [
enabled: ENABLED_MODULES['generated-files'],
hash: async () =>
keyStorage.getKey('generated-files') ??
`generated-files-${GENERATED_FILES_VERSION}-${HASH_VERSION}-${getPlatformString()}-${await getYarnLockHash()}-${await getPackageHash()}-${await getGeneratedFileHash()}`,
`generated-files-${HASH_VERSION}-${getPlatformString()}-${await getYarnLockHash()}-${await getPackageHash()}-${await getGeneratedFilesHash()}`,
name: 'Cache Generated Files',
id: 'generated-files',
path: 'generated_files.tar.gz',
Expand Down
1 change: 0 additions & 1 deletion scripts/ci/cache/_const.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@ export const ENV_YAML_FILE = 'YAML_FILE'
export const ENV_CACHE_SUCCESS = '_CACHE_SUCCESS'
export const ENV_ENABLED_CACHE = 'ENABLE_CACHE'
export const ENV_JOB_STATUS = 'JOB_STATUS'
export const HASH_GENERATE_FILES_SCRIPT = 'scripts/_hash-generated-files.sh'
51 changes: 51 additions & 0 deletions scripts/ci/cache/_generated_files.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// @ts-check
import { createHash } from 'node:crypto'
import { readFile } from 'node:fs/promises'
import { glob } from 'glob'
import { ROOT } from './_common.mjs'
import { resolve } from 'node:path'

const patterns = [
'scripts/codegen.js',
'libs/cms/src/lib/generated/contentfulTypes.d.ts',
'apps/air-discount-scheme/web/i18n/withLocale.tsx',
'apps/air-discount-scheme/web/components/AppLayout/AppLayout.tsx',
'apps/air-discount-scheme/web/components/Header/Header.tsx',
'apps/air-discount-scheme/web/screens/**/*.tsx',
'libs/application/types/src/lib/ApplicationTypes.ts',
'**/codegen.yml',
'**/*.model.ts',
'**/*.enum.ts',
'**/queries/**/*',
'**/mutations/**/*',
'**/fragments/**/*',
'**/*.resolver.ts',
'**/*.service.ts',
'**/*.dto.ts',
'**/*.input.ts',
'**/*.module.ts',
'**/*.controller.ts',
'**/*.union.ts',
'**/*.graphql.tsx?',
'**/*.graphql',
'**/clientConfig.*',
'libs/judicial-system/**',
]

export async function getGeneratedFilesHash() {
const hash = createHash('sha1')
const files = await glob(patterns, {
cwd: ROOT,
nodir: true,
})
console.log(`Files to hash:`)
for (const _file of files) {
console.log(_file)
const file = resolve(ROOT, _file)
const content = await readFile(file, 'utf8')
hash.update(content)
}

const finalHash = hash.digest('hex')
return finalHash
}
27 changes: 2 additions & 25 deletions scripts/ci/cache/_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,10 @@
import { arch, platform } from 'node:os'
import crypto from 'node:crypto'
import { ROOT } from './_common.mjs'
import { spawn, exec } from 'node:child_process'
import { spawn } from 'node:child_process'
import { resolve, join } from 'node:path'
import { readFile, readdir, stat } from 'node:fs/promises'
import { HASH_GENERATE_FILES_SCRIPT } from './_const.mjs'

const GENERATE_HASH_GENERATED_FILES_SCRIPT = resolve(
ROOT,
...HASH_GENERATE_FILES_SCRIPT.split('/'),
)
export async function getGeneratedFileHash(
scriptPath = GENERATE_HASH_GENERATED_FILES_SCRIPT,
) {
return new Promise((resolve, reject) => {
exec(`bash "${scriptPath}"`, (error, stdout, stderr) => {
if (error) {
reject(`Error: ${error.message}`)
return
}
if (stderr) {
reject(`Stderr: ${stderr}`)
return
}
console.log(stdout)
resolve(stdout)
})
})
}

export async function getNodeVersionString() {
const content = await getPackageJSON()
const nodeVersion = content?.engines?.node
Expand Down
3 changes: 2 additions & 1 deletion scripts/ci/cache/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"dependencies": {
"@actions/cache": "3.2.4"
"@actions/cache": "3.2.4",
"glob": "10.4.1"
}
}
Loading

0 comments on commit fc305bb

Please sign in to comment.