Skip to content

Commit

Permalink
Revert "Rationalise DCR's env checking scripts (#9400)"
Browse files Browse the repository at this point in the history
This reverts commit 7d623b9.
  • Loading branch information
cemms1 authored Nov 8, 2023
1 parent 24b08b3 commit aa2bb5a
Show file tree
Hide file tree
Showing 13 changed files with 136 additions and 140 deletions.
16 changes: 8 additions & 8 deletions dotcom-rendering/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ export SHELL := /usr/bin/env bash
# messaging #########################################

define log
@node ../scripts/log $(1)
@node scripts/env/log $(1)
endef

define warn
@node ../scripts/log $(1) warn
@node scripts/env/log $(1) warn
endef

# deployment #########################################
Expand Down Expand Up @@ -134,11 +134,9 @@ lint: clean-dist install
$(call log, "checking for lint errors")
@yarn lint

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

stylelint: clean-dist install
$(call log, "checking for style lint errors")
Expand Down Expand Up @@ -183,8 +181,10 @@ validate-build: # private

check-env: # private
$(call log, "checking environment")
@cd .. && scripts/env/check-node
@cd .. && scripts/env/check-yarn
@node scripts/env/check-node.js
@node scripts/env/check-yarn.js
@node scripts/env/check-deps.js
@node scripts/env/check-files.js

clear: # private
@clear
Expand Down
2 changes: 1 addition & 1 deletion dotcom-rendering/scripts/deploy/build-riffraff-bundle.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path';
import * as url from 'node:url';
import cpy from 'cpy';
import execa from 'execa';
import { log, warn } from '../../../scripts/log.js';
import { log, warn } from '../env/log.js';

const dirname = url.fileURLToPath(new URL('.', import.meta.url));
const target = path.resolve(dirname, '../..', 'target');
Expand Down
2 changes: 1 addition & 1 deletion dotcom-rendering/scripts/env/check-deps.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const fs = require('node:fs');
const lockfile = require('@yarnpkg/lockfile');
const { warn, log } = require('../../../scripts/log');
const pkg = require('../../package.json');
const { warn, log } = require('./log');

if (pkg.devDependencies) {
warn('Don’t use devDependencies');
Expand Down
2 changes: 1 addition & 1 deletion dotcom-rendering/scripts/env/check-files.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const execa = require('execa');
const { warn } = require('../../../scripts/log');
const { warn } = require('./log');

execa('find', ['src', '-type', 'f', '-name', '*index*.ts*'])
.then(({ stdout }) => {
Expand Down
46 changes: 46 additions & 0 deletions dotcom-rendering/scripts/env/check-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// eslint-disable-next-line @typescript-eslint/unbound-method
const { join } = require('node:path');
const { promisify } = require('node:util');
const readFile = promisify(require('node:fs').readFile);
const ensure = require('./ensure');

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
try {
const [semver] = await ensure('semver');

const nodeVersion = /^v(\d+\.\d+\.\d+)/.exec(process.version)[1];
const nvmrcVersion = (
await readFile(join(__dirname, '..', '..', '..', '.nvmrc'), 'utf8')
).trim();

if (!semver.satisfies(nodeVersion, nvmrcVersion)) {
const { warn, prompt, log } = require('./log');
warn(
`dotcom-rendering requires Node v${nvmrcVersion}`,
`You are using v${nodeVersion ?? '(unknown)'}`,
);
if (process.env.NVM_DIR) {
prompt('Run `nvm install` from the repo root and try again.');
log(
'See also: https://gist.github.com/sndrs/5940e9e8a3f506b287233ed65365befb',
);
} else if (process.env.FNM_DIR) {
prompt(
'It looks like you have fnm installed',
'Run `fnm use` from the repo root and try again.',
);
} else {
prompt(
`Using a Node version manager can make things easier.`,
`Our recommendation is fnm: https://github.com/Schniz/fnm`,
);
}
process.exit(1);
}
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
process.exit(1);
}
})();
32 changes: 32 additions & 0 deletions dotcom-rendering/scripts/env/check-yarn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { promisify } = require('node:util');
const exec = promisify(require('node:child_process').execFile);
const ensure = require('./ensure');

// Yarn v1.x support .yarnrc, so we can use a local (check-in) copy of yarn
const YARN_MIN_VERSION = '1.x';

// eslint-disable-next-line @typescript-eslint/no-floating-promises
(async () => {
try {
// This will fail if yarn isn't installed, and force into the catch,
// where we install yarn with NPM (mainly for CI)
const { stdout: version } = await exec('yarn', ['--version']);

const [semver] = await ensure('semver');

if (!semver.satisfies(version, YARN_MIN_VERSION)) {
const { warn, prompt, log } = require('./log');
warn(
`dotcom-rendering requires Yarn >=${YARN_MIN_VERSION}`,
`You are using v${version}`,
);
prompt('Please upgrade yarn');
log('https://classic.yarnpkg.com/en/docs/install');

process.exit(1);
}
} catch (e) {
require('./log').log(`Installing yarn`);
await exec('npm', ['i', '-g', `yarn@${YARN_MIN_VERSION}`]);
}
})();
37 changes: 37 additions & 0 deletions dotcom-rendering/scripts/env/ensure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// provides a way to use packages in scripts when we don't know
// if they've been installed yet (with yarn) by temporarily
// installing with npm if node cannot resolve the package

const { log } = require('./log');

module.exports = (...packages) =>
new Promise((resolve) => {
try {
resolve(packages.map(require));
} catch (e) {
log(`Pre-installing dependency (${packages.join(', ')})...`);
const npmInstallProcess = require('node:child_process')
.spawn('npm', [
'i',
...packages,
'--no-save',
'--legacy-peer-deps',
'--package-lock=false',
])
.on('close', (code) => {
if (code !== 0) {
process.exit(code);
}
try {
resolve(packages.map(require));
} catch (e2) {
// eslint-disable-next-line no-console
console.log(e2);
process.exit(1);
}
})
.stderr.on('data', (data) =>
console.error(Buffer.from(data).toString()),
);
}
});
9 changes: 2 additions & 7 deletions scripts/log.js → dotcom-rendering/scripts/env/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ const capitalize = (str) =>

// we could use chalk, but this saves needing to pre-install it
// if this is a first run
// https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
const red = '\x1b[31m';
const yellow = '\x1b[33m';
const green = '\x1b[32m';
const green = '\u001b[32m';
const dim = '\x1b[2m';
const reset = '\x1b[0m';

const colourise = (colour, str) =>
process.stdout.isTTY ? `${colour}${str}${reset}` : str;

const logIt = (messages = [], color = dim) => {
console.log(colourise(color, capitalize(messages.join('\n'))));
console.log(`${color}%s${reset}`, capitalize(messages.join('\n')));
};

const log = (...messages) => logIt(messages);
Expand Down Expand Up @@ -46,5 +42,4 @@ module.exports = {
warn,
prompt,
success,
colourise,
};
4 changes: 2 additions & 2 deletions dotcom-rendering/scripts/gen-stories/get-stories.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import { mkdirSync, readFileSync, writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { log, success, warn } from '../../../scripts/log.js';
import { log, success, warn } from '../env/log.js';

const STORIES_PATH = resolve(
dirname(fileURLToPath(new URL(import.meta.url))),
Expand Down Expand Up @@ -132,7 +132,7 @@ ${storyVariableName}.args = { config: ${JSON.stringify(config)} };
${storyVariableName}.decorators = [lightDecorator({
display: ArticleDisplay.${displayName},
design: ArticleDesign.${designName},
theme: {...ArticleSpecial, ...Pillar}.${theme.replace('Pillar', '')},
theme: {...ArticleSpecial, ...Pillar}.${theme.replace("Pillar", "")},
})];
`;
};
Expand Down
2 changes: 1 addition & 1 deletion dotcom-rendering/scripts/perf/perf-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const execa = require('execa');
const { warn, log } = require('../../../scripts/log');
const { warn, log } = require('../env/log');

const run = async () => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import { readFile } from 'node:fs/promises';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { log, warn } from '../../scripts/log.js';
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'))
const nvmrc = (await readFile('.nvmrc', 'utf-8'))
// We don’t care about leading or trailing whitespace
.trim();

Expand All @@ -30,15 +30,15 @@ if (!nodeVersion) {
const requiredNodeVersionMatches =
/** @type {const} @satisfies {ReadonlyArray<{filepath: string, pattern: RegExp}>}*/ ([
{
filepath: 'Containerfile',
filepath: 'dotcom-rendering/Containerfile',
pattern: /^FROM node:(.+)-alpine$/m,
},
{
filepath: 'scripts/deploy/riff-raff.yaml',
filepath: 'dotcom-rendering/scripts/deploy/riff-raff.yaml',
pattern: /^ +Recipe: dotcom-rendering.*-node-(\d+\.\d+\.\d+)$/m,
},
{
filepath: '../apps-rendering/riff-raff.yaml',
filepath: 'apps-rendering/riff-raff.yaml',
pattern: /^ +Recipe: .+-mobile-node(\d+\.\d+\.\d+).*$/m,
},
]);
Expand Down
86 changes: 0 additions & 86 deletions scripts/env/check-node

This file was deleted.

28 changes: 0 additions & 28 deletions scripts/env/check-yarn

This file was deleted.

0 comments on commit aa2bb5a

Please sign in to comment.