Skip to content

Commit e0cddd6

Browse files
mxdvlsndrs
andcommitted
feat(Makefile): lint project
Add a check for node versions in all known config using it: - dotcom-rendering’s riff-raff.yaml - dotcom-rendering’s Containerfile - apps-rendering’s riff-raff.yaml Co-authored-by: Alex Sanders <[email protected]>
1 parent 3b756a7 commit e0cddd6

File tree

6 files changed

+90
-4
lines changed

6 files changed

+90
-4
lines changed

.github/workflows/lint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ jobs:
1313
cache: 'yarn'
1414
- run: make install
1515
working-directory: dotcom-rendering
16+
- name: Lint Project
17+
run: make lint-project
18+
working-directory: dotcom-rendering
1619
- name: Lint
1720
run: make lint
1821
working-directory: dotcom-rendering

apps-rendering/riff-raff.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ templates:
1111
amiParameter: AMIMobileappsrendering
1212
amiEncrypted: true
1313
amiTags:
14-
Recipe: jammy-mobile-node18-ARM
14+
# Keep the Node version in sync with `.nvmrc`
15+
Recipe: jammy-mobile-node18.16.0-ARM
1516
AmigoStage: PROD
1617
deployments:
1718
mobile-apps-rendering-cfn:

dotcom-rendering/Containerfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# This container is used in our CICD pipelines for running E2E and regression tests.
2+
# Keep the Node version in sync with `.nvmrc`
23
FROM node:18.16.0-alpine
34

45
WORKDIR /opt/app/dotcom-rendering/dotcom-rendering

dotcom-rendering/makefile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ lint: clean-dist install
136136
$(call log, "checking for lint errors")
137137
@yarn lint
138138

139+
lint-project:
140+
$(call log, "linting project")
141+
@node ../scripts/check-node-versions.mjs
142+
139143
stylelint: clean-dist install
140144
$(call log, "checking for style lint errors")
141145
@stylelint "src/**/*.ts{,x}"
@@ -149,10 +153,10 @@ test-ci: clear clean-dist install
149153
$(call log, "running tests")
150154
@yarn test:ci --verbose --collectCoverage --coverageReporters=lcov
151155

152-
validate: clean-dist install tsc lint stylelint test validate-build
156+
validate: clean-dist install lint-project tsc lint stylelint test validate-build
153157
$(call log, "everything seems 👌")
154158

155-
validate-ci: install tsc lint stylelint test-ci
159+
validate-ci: install lint-project tsc lint stylelint test-ci
156160
$(call log, "everything seems 👌")
157161

158162
# helpers #########################################

dotcom-rendering/scripts/deploy/riff-raff.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ deployments:
1919
InstanceType: t4g.small
2020
amiParametersToTags:
2121
AMI:
22-
Recipe: dotcom-rendering-ARM-jammy-node-18
22+
# Keep the Node version in sync with `.nvmrc`
23+
Recipe: dotcom-rendering-ARM-jammy-node-18.16.0
2324
BuiltBy: amigo
2425
AmigoStage: PROD
2526
rendering:

scripts/check-node-versions.mjs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)