Created
March 26, 2020 20:13
-
-
Save giordanocardillo/68ccbe28714c3c4fee4925caed83bb54 to your computer and use it in GitHub Desktop.
Node switch
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
function nsw() { | |
if [[ -z $1 ]]; then | |
echo "Usage nsw [<node version>|ls] [-q]" | |
return; | |
fi | |
if [[ $1 == "ls" ]]; then | |
ls /d/Spindox/nodejs | sed s/nodejs-// | |
return | |
fi | |
versions=$(ls /d/Spindox/nodejs | sed s/nodejs-//) | |
if [[ ! $versions =~ $1 ]]; then | |
echo "Unsupported version provided" | |
return | |
fi | |
old_node=$(node -v | sed s/v//) | |
if [[ ! -z $old_node ]]; then | |
# There is an old version of node in PATH, replace that | |
PATH=$(echo $PATH | sed s/$old_node/$1/g) | |
else | |
export PATH=/d/Spindox/nodejs/nodejs-$1:$PATH | |
fi | |
export NODE_VERSION=$1 | |
export NODE_HOME=$(echo $NODE_HOME | sed s/$old_node/$1/g) | |
if [[ ! $2 == "-q" ]]; then | |
echo Now using node $(node -v) | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment