|
| 1 | +// @ts-check |
| 2 | + |
| 3 | +import { readFile } from 'node:fs/promises'; |
| 4 | +import { dirname, resolve } from 'node:path'; |
| 5 | +import { fileURLToPath } from 'node:url'; |
| 6 | +import { log, warn } from '../dotcom-rendering/scripts/env/log.js'; |
| 7 | + |
| 8 | +const __dirname = dirname(fileURLToPath(import.meta.url)); |
| 9 | + |
| 10 | +process.chdir(resolve(__dirname, '..')); |
| 11 | + |
| 12 | +const nvmrc = (await readFile('.nvmrc', 'utf-8')) |
| 13 | + // We don’t care about leading or trailing whitespace |
| 14 | + .trim(); |
| 15 | + |
| 16 | +/** Matches `x.y.z` pattern */ |
| 17 | +const nodeVersionPattern = /^\d+\.\d+\.\d+$/; |
| 18 | +const nodeVersion = nvmrc.match(nodeVersionPattern)?.[0] ?? undefined; |
| 19 | + |
| 20 | +if (!nodeVersion) { |
| 21 | + warn( |
| 22 | + 'Node version in .nvmrc has incorrect pattern:', |
| 23 | + `\`${nvmrc}\` does not match \`x.y.z\``, |
| 24 | + ); |
| 25 | + process.exit(1); |
| 26 | +} else { |
| 27 | + log(`Found node version ${nodeVersion} in \`.nvmrc\``); |
| 28 | +} |
| 29 | + |
| 30 | +const requiredNodeVersionMatches = |
| 31 | + /** @type {const} @satisfies {ReadonlyArray<{filepath: string, pattern: RegExp}>}*/ ([ |
| 32 | + { |
| 33 | + filepath: 'dotcom-rendering/Containerfile', |
| 34 | + pattern: /^FROM node:(.+)-alpine$/m, |
| 35 | + }, |
| 36 | + { |
| 37 | + filepath: 'dotcom-rendering/scripts/deploy/riff-raff.yaml', |
| 38 | + pattern: /^ +Recipe: dotcom-rendering.*-node-(\d+\.\d+\.\d+)$/m, |
| 39 | + }, |
| 40 | + { |
| 41 | + filepath: 'apps-rendering/riff-raff.yaml', |
| 42 | + pattern: /^ +Recipe: .+-mobile-node(\d+\.\d+\.\d+).*$/m, |
| 43 | + }, |
| 44 | + ]); |
| 45 | + |
| 46 | +const problems = ( |
| 47 | + await Promise.all( |
| 48 | + requiredNodeVersionMatches.map(async ({ filepath, pattern }) => { |
| 49 | + const fileContents = await readFile( |
| 50 | + resolve(...filepath.split('/')), |
| 51 | + 'utf-8', |
| 52 | + ); |
| 53 | + const foundNodeVersion = |
| 54 | + fileContents.match(pattern)?.[1] ?? undefined; |
| 55 | + |
| 56 | + return foundNodeVersion === nodeVersion |
| 57 | + ? undefined |
| 58 | + : `Node version in ${filepath} (${foundNodeVersion}) does not match \`.nvmrc\` (${nodeVersion})`; |
| 59 | + }), |
| 60 | + ) |
| 61 | +).filter( |
| 62 | + /** @type {(problem?: string) => problem is string} */ |
| 63 | + (problem) => !!problem, |
| 64 | +); |
| 65 | + |
| 66 | +if (problems.length === 0) { |
| 67 | + log( |
| 68 | + `All ${requiredNodeVersionMatches.length} checked files use the correct Node version`, |
| 69 | + ); |
| 70 | + process.exitCode = 0; |
| 71 | +} else { |
| 72 | + for (const problem of problems) { |
| 73 | + warn(problem); |
| 74 | + } |
| 75 | + process.exitCode = 1; |
| 76 | +} |
0 commit comments