Skip to content

Commit

Permalink
feat(Makefile): lint project
Browse files Browse the repository at this point in the history
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 <alex@sndrs.dev>
  • Loading branch information
mxdvl and sndrs committed Jun 21, 2023
1 parent 3b756a7 commit e0cddd6
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:
cache: 'yarn'
- run: make install
working-directory: dotcom-rendering
- name: Lint Project
run: make lint-project
working-directory: dotcom-rendering
- name: Lint
run: make lint
working-directory: dotcom-rendering
Expand Down
3 changes: 2 additions & 1 deletion apps-rendering/riff-raff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ templates:
amiParameter: AMIMobileappsrendering
amiEncrypted: true
amiTags:
Recipe: jammy-mobile-node18-ARM
# Keep the Node version in sync with `.nvmrc`
Recipe: jammy-mobile-node18.16.0-ARM
AmigoStage: PROD
deployments:
mobile-apps-rendering-cfn:
Expand Down
1 change: 1 addition & 0 deletions dotcom-rendering/Containerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# This container is used in our CICD pipelines for running E2E and regression tests.
# Keep the Node version in sync with `.nvmrc`
FROM node:18.16.0-alpine

WORKDIR /opt/app/dotcom-rendering/dotcom-rendering
Expand Down
8 changes: 6 additions & 2 deletions dotcom-rendering/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ lint: clean-dist install
$(call log, "checking for lint errors")
@yarn lint

lint-project:
$(call log, "linting project")
@node ../scripts/check-node-versions.mjs

stylelint: clean-dist install
$(call log, "checking for style lint errors")
@stylelint "src/**/*.ts{,x}"
Expand All @@ -149,10 +153,10 @@ test-ci: clear clean-dist install
$(call log, "running tests")
@yarn test:ci --verbose --collectCoverage --coverageReporters=lcov

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

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

# helpers #########################################
Expand Down
3 changes: 2 additions & 1 deletion dotcom-rendering/scripts/deploy/riff-raff.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ deployments:
InstanceType: t4g.small
amiParametersToTags:
AMI:
Recipe: dotcom-rendering-ARM-jammy-node-18
# Keep the Node version in sync with `.nvmrc`
Recipe: dotcom-rendering-ARM-jammy-node-18.16.0
BuiltBy: amigo
AmigoStage: PROD
rendering:
Expand Down
76 changes: 76 additions & 0 deletions scripts/check-node-versions.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// @ts-check

import { readFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { log, warn } from '../dotcom-rendering/scripts/env/log.js';

const __dirname = dirname(fileURLToPath(import.meta.url));

process.chdir(resolve(__dirname, '..'));

const nvmrc = (await readFile('.nvmrc', 'utf-8'))
// We don’t care about leading or trailing whitespace
.trim();

/** Matches `x.y.z` pattern */
const nodeVersionPattern = /^\d+\.\d+\.\d+$/;
const nodeVersion = nvmrc.match(nodeVersionPattern)?.[0] ?? undefined;

if (!nodeVersion) {
warn(
'Node version in .nvmrc has incorrect pattern:',
`\`${nvmrc}\` does not match \`x.y.z\``,
);
process.exit(1);
} else {
log(`Found node version ${nodeVersion} in \`.nvmrc\``);
}

const requiredNodeVersionMatches =
/** @type {const} @satisfies {ReadonlyArray<{filepath: string, pattern: RegExp}>}*/ ([
{
filepath: 'dotcom-rendering/Containerfile',
pattern: /^FROM node:(.+)-alpine$/m,
},
{
filepath: 'dotcom-rendering/scripts/deploy/riff-raff.yaml',
pattern: /^ +Recipe: dotcom-rendering.*-node-(\d+\.\d+\.\d+)$/m,
},
{
filepath: 'apps-rendering/riff-raff.yaml',
pattern: /^ +Recipe: .+-mobile-node(\d+\.\d+\.\d+).*$/m,
},
]);

const problems = (
await Promise.all(
requiredNodeVersionMatches.map(async ({ filepath, pattern }) => {
const fileContents = await readFile(
resolve(...filepath.split('/')),
'utf-8',
);
const foundNodeVersion =
fileContents.match(pattern)?.[1] ?? undefined;

return foundNodeVersion === nodeVersion
? undefined
: `Node version in ${filepath} (${foundNodeVersion}) does not match \`.nvmrc\` (${nodeVersion})`;
}),
)
).filter(
/** @type {(problem?: string) => problem is string} */
(problem) => !!problem,
);

if (problems.length === 0) {
log(
`All ${requiredNodeVersionMatches.length} checked files use the correct Node version`,
);
process.exitCode = 0;
} else {
for (const problem of problems) {
warn(problem);
}
process.exitCode = 1;
}

0 comments on commit e0cddd6

Please sign in to comment.