Skip to content

Commit 86b3b65

Browse files
bachandmartijnwalraven
authored andcommitted
Actually install the version of node we want via NVM/nodenv instead of just checking (#434)
1 parent 92c16aa commit 86b3b65

1 file changed

Lines changed: 19 additions & 26 deletions

File tree

scripts/check-and-run-apollo-cli.sh

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Only major and minor version should be specified here
22
REQUIRED_APOLLO_CLI_VERSION=1.9
3-
# Only major version should be specified here
4-
REQUIRED_NODE_VERSION=8
3+
# Specify fully qualified version here. Ideally this should be a LTS version.
4+
REQUIRED_NODE_VERSION=8.15.0
55

66
# Using npx to execute 'apollo' looks for a local install in node_modules before checking $PATH (for a global install)
77
APOLLO_CLI="npx --no-install apollo"
@@ -17,18 +17,33 @@ if [[ -z "$CONFIGURATION" ]]; then
1717
exit 1
1818
fi
1919

20-
# Define NVM_DIR and source the nvm.sh setup script
21-
[[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm"
20+
use_correct_node_via_nvm() {
21+
nvm version $REQUIRED_NODE_VERSION > /dev/null
22+
if [[ $? -eq 0 ]]; then
23+
# The version of node that we want is installed.
24+
nvm use $REQUIRED_NODE_VERSION
25+
else
26+
nvm install $REQUIRED_NODE_VERSION
27+
fi
28+
}
29+
30+
use_correct_node_via_nodenv() {
31+
nodenv install -s $REQUIRED_NODE_VERSION
32+
nodenv shell $REQUIRED_NODE_VERSION
33+
}
2234

2335
if [[ -s "$HOME/.nvm/nvm.sh" ]]; then
2436
. "$HOME/.nvm/nvm.sh"
37+
use_correct_node_via_nvm
2538
elif [[ -x "$(command -v brew)" && -s "$(brew --prefix nvm)/nvm.sh" ]]; then
2639
. "$(brew --prefix nvm)/nvm.sh"
40+
use_correct_node_via_nvm
2741
fi
2842

2943
# Set up the nodenv node version manager if present
3044
if [[ -x "$HOME/.nodenv/bin/nodenv" ]]; then
3145
eval "$("$HOME/.nodenv/bin/nodenv" init -)"
46+
use_correct_node_via_nodenv
3247
fi
3348

3449
parse_version() {
@@ -37,28 +52,6 @@ parse_version() {
3752
fi
3853
}
3954

40-
get_installed_node_version() {
41-
version=$(node -v)
42-
if [[ $? -eq 0 ]]; then
43-
echo "$version"
44-
fi
45-
}
46-
47-
is_mimimum_major_version() {
48-
[[ "$(parse_version $1 | cut -d. -f1)" -ge $2 ]]
49-
}
50-
51-
# Check the installed version of Node, if available
52-
INSTALLED_NODE_VERSION="$(get_installed_node_version)"
53-
if [[ -z "$INSTALLED_NODE_VERSION" ]]; then
54-
echo "error: Apollo CLI requires Node $REQUIRED_NODE_VERSION or higher to be installed."
55-
exit 1
56-
elif ! is_mimimum_major_version "$INSTALLED_NODE_VERSION" $REQUIRED_NODE_VERSION; then
57-
echo "error: Apollo CLI requires Node $REQUIRED_NODE_VERSION or higher, \
58-
but $INSTALLED_NODE_VERSION seems to be installed."
59-
exit 1
60-
fi
61-
6255
get_installed_cli_version() {
6356
version=$($APOLLO_CLI -v)
6457
if [[ $? -eq 0 ]]; then

0 commit comments

Comments
 (0)