-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rationalise DCR's env checking scripts (#9400)
* move `log` script to the repo root * move `check-node-versions` to DCR project * move `check-deps` and `check-files` to `lint-project` they don’t need to run every time you use the makefile * run `check-env` when running `lint-project` * convert `check-node` to bash and move to repo root * move `check-yarn` to repo root * make sure `log` emits legible messages outside terminals e.g. in a gui notification * use `colourise` in `check-yarn` * add links to ansi colour codes and standardise the escape char
- Loading branch information
Showing
13 changed files
with
140 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Check whether the current node version matches the .nvmrc version, and offer some help if not. | ||
|
||
in_terminal() { test -t 1; } | ||
|
||
strip_colors() { | ||
local text="$1" | ||
echo "$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' | ||
bold='\033[1m' | ||
reset='\033[0m' | ||
|
||
# get the node version from .nvmrc | ||
nvmrc_contents=$(cat .nvmrc) | ||
|
||
# check that it's a valid version (matches x.y.z) | ||
nvmrc_version_pattern="^[0-9]+\.[0-9]+\.[0-9]+$" | ||
if [[ $nvmrc_contents =~ $nvmrc_version_pattern ]]; then | ||
nvmrc_version=${BASH_REMATCH[0]} | ||
else | ||
log_with_color "${red}The Node version in .nvmrc is invalid${reset}" | ||
log_with_color "${dim}It should match the pattern 'x.y.z'.${reset}" | ||
# exit with failure | ||
exit 1 | ||
fi | ||
|
||
# Now we can check if the current version of Node matches the .nvmrc version | ||
|
||
# is _any_ node available? | ||
if [[ -x "$(command -v node)" ]]; then | ||
node_version=$(node -v) | ||
node_version=${node_version:1} # remove the "v" in "v1.2.3" | ||
fi | ||
|
||
# check the version we're using | ||
# note that `node_version` will be empty if node wan't available above | ||
if [ "$node_version" == "$nvmrc_version" ]; then | ||
# we're using the correct version of node | ||
log_with_color "${dim}Using Node ${blue}$node_version${reset}" | ||
|
||
# we're done, exit with success | ||
exit 0 | ||
fi | ||
|
||
# If we got here, we're using the wrong version of node, or There Is No Node. | ||
# Try to help people load the correct version: | ||
if in_terminal; then | ||
log_with_color "${red}This project requires Node v$nvmrc_version${reset}" | ||
if [[ -x "$(command -v fnm)" ]]; then | ||
log_with_color "${dim}Run ${blue}fnm use${reset}${dim} to switch to the correct version.${reset}" | ||
log_with_color "${dim}See ${blue}${dim}https://github.com/Schniz/fnm#shell-setup${reset}${dim} to automate this.${reset}" | ||
elif [[ -x "$(command -v asdf)" ]]; then | ||
log_with_color "${dim}Run ${blue}asdf install${reset}${dim} to switch to the correct version.${reset}" | ||
elif [[ -x "$(which nvm)" ]]; then | ||
log_with_color "${dim}Run ${blue}nvm install${reset}${dim} to switch to the correct version.${reset}" | ||
else | ||
log_with_color "${dim}Consider using ${bold}fnm${reset}${dim} to manage Node versions:${reset} ${blue}https://github.com/Schniz/fnm#installation${reset}${dim}.${reset}" | ||
fi | ||
else | ||
# not in a terminal, so v possible we're running a husky script in a git gui | ||
echo "Could not find Node v$nvmrc_version." | ||
echo "You may need to load your Node version manager in a ~/.huskyrc file." | ||
echo "See https://typicode.github.io/husky/troubleshooting.html#command-not-found." | ||
fi | ||
|
||
# exit with failure | ||
exit 1 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#!/usr/bin/env node | ||
|
||
/** | ||
* 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). | ||
*/ | ||
|
||
// no external deps can be used here, because this runs before deps are installed | ||
const { execSync } = require('child_process'); | ||
const { stdout } = require('process'); | ||
const { colourise, log, warn } = require('../log'); | ||
|
||
// we can't use chalk, because this runs before deps are installed | ||
// https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 | ||
const reset = '\x1b[0m'; | ||
const blue = '\x1b[34m'; | ||
|
||
try { | ||
const yarnVersion = execSync('yarn -v', { encoding: 'utf-8' }); | ||
log(`Using yarn ${colourise(reset, colourise(blue, yarnVersion.trim()))}`); | ||
} catch (e) { | ||
warn(`Could not find yarn. Please run 'corepack enable'.`); | ||
process.exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters