From 2af6b0eeca38ae75f5d2a8484af72af6e940cd14 Mon Sep 17 00:00:00 2001 From: Alex Sanders Date: Tue, 7 Nov 2023 12:43:07 +0000 Subject: [PATCH] convert `check-yarn` to bash and move to repo root --- dotcom-rendering/makefile | 4 +-- dotcom-rendering/scripts/env/check-yarn.js | 32 ------------------ dotcom-rendering/scripts/env/ensure.js | 37 --------------------- scripts/env/check-yarn | 38 ++++++++++++++++++++++ 4 files changed, 40 insertions(+), 71 deletions(-) delete mode 100644 dotcom-rendering/scripts/env/check-yarn.js delete mode 100644 dotcom-rendering/scripts/env/ensure.js create mode 100755 scripts/env/check-yarn diff --git a/dotcom-rendering/makefile b/dotcom-rendering/makefile index 781fdd9f106..fc9dbaeb6f0 100644 --- a/dotcom-rendering/makefile +++ b/dotcom-rendering/makefile @@ -183,8 +183,8 @@ validate-build: # private check-env: # private $(call log, "checking environment") - @ cd .. && scripts/env/check-node - @node scripts/env/check-yarn.js + @cd .. && scripts/env/check-node + @cd .. && scripts/env/check-yarn clear: # private @clear diff --git a/dotcom-rendering/scripts/env/check-yarn.js b/dotcom-rendering/scripts/env/check-yarn.js deleted file mode 100644 index e1191e97ae7..00000000000 --- a/dotcom-rendering/scripts/env/check-yarn.js +++ /dev/null @@ -1,32 +0,0 @@ -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('../../../scripts/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('../../../scripts/log').log(`Installing yarn`); - await exec('npm', ['i', '-g', `yarn@${YARN_MIN_VERSION}`]); - } -})(); diff --git a/dotcom-rendering/scripts/env/ensure.js b/dotcom-rendering/scripts/env/ensure.js deleted file mode 100644 index 008f1e6a8e8..00000000000 --- a/dotcom-rendering/scripts/env/ensure.js +++ /dev/null @@ -1,37 +0,0 @@ -// 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('../../../scripts/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()), - ); - } - }); diff --git a/scripts/env/check-yarn b/scripts/env/check-yarn new file mode 100755 index 00000000000..91e5891c69b --- /dev/null +++ b/scripts/env/check-yarn @@ -0,0 +1,38 @@ +#!/usr/bin/env bash + +# Check that yarn is available. If not, ask the user to run `corepack enable` +# which will provide it. Note that any yarn@>1 will do, since it will defer to +# the copy in that lives in .yarn/releases (which is the version we want to +# use). + +in_terminal() { test -t 1; } + +strip_colors() { + local text="$1" + printf "$text" | sed -e $'s/\x1b\[[0-9;]*m//g' +} + +log_with_color() { + local text="$1" + + # Check if output is a terminal and supports color + if in_terminal && [[ $(tput colors) -ge 8 ]]; then + # Terminal supports color, print the text as it is + echo -e "$text" + else + # Terminal does not support color, strip color codes and print + echo "$(strip_colors "$text")" + fi +} + +blue='\033[0;34m' +red='\033[0;31m' +dim='\033[2m' +reset='\033[0m' + +if ! command -v yarn &> /dev/null; then + log_with_color "${red}Could not find yarn. Please run 'corepack enable'.${reset}" + exit 1 +fi + +log_with_color "${dim}Using yarn ${blue}$(yarn -v)${reset}"