-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnvm-install
47 lines (42 loc) · 1.62 KB
/
nvm-install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# shellcheck shell=sh
# https://gist.github.com/karfau/dcf98c6eefc2f2132c160f5c14d2112f
# v2024.8.15
# needs to be sourced as part of your script
# 1. tries to configure nvm and run `nvm install`
# 2. checks if the node version is correct based on ./.nvmrc
# if both doesn't work, exits with code 1 and some helpful messages
# Sometimes we prefer `nvm use` over `nvm install`
# you can basically put anything you want here, but the default is `install`
NVM_SETUP_COMMAND=${NVM_SETUP_COMMAND:-install}
NVM_DIR=${NVM_DIR:-$HOME/.nvm}
# https://unix.stackexchange.com/a/184512/194420
# https://github.com/nvm-sh/nvm/issues/1290
if [ -f "${NVM_DIR}/nvm.sh" ]; then
echo "sourcing nvm from NVM_DIR:${NVM_DIR}"
. "${NVM_DIR}/nvm.sh"
elif command -v brew; then
# https://docs.brew.sh/Manpage#--prefix-formula
BREW_PREFIX=$(brew --prefix nvm)
if [ -f "$BREW_PREFIX/nvm.sh" ]; then
echo "sourcing nvm from brew ($BREW_PREFIX)"
. "${BREW_PREFIX}/nvm.sh"
fi
fi
if command -v nvm ; then
echo "NVM_SETUP_COMMAND is ${NVM_SETUP_COMMAND}"
nvm ${NVM_SETUP_COMMAND}
else
echo "WARN: not able to configure nvm"
fi
NODE_VERSION="$(cat .nvmrc | sed 's/^v//')"
which node
ACTIVE_VERSION="$(node --version | sed 's/^v//')"
GLOBAL_NPM=$(which npm || echo "not found on PATH")
# .nvmrc can contain only major or major.minor or full version
# so we replace active version with node version and anything afterwards
# if something is left, it's not a match
if [ "${ACTIVE_VERSION%%$NODE_VERSION*}" ] || [ ! -e "$GLOBAL_NPM" ]; then
echo "expected node '$NODE_VERSION' and npm on path"
echo "but was '$ACTIVE_VERSION' and npm:'$GLOBAL_NPM'"
return 1
fi