Skip to content

Instantly share code, notes, and snippets.

@aymanalzarrad
Last active June 11, 2020 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aymanalzarrad/3e0332103a2fe6b8db42dbaa2cedaf7c to your computer and use it in GitHub Desktop.
Save aymanalzarrad/3e0332103a2fe6b8db42dbaa2cedaf7c to your computer and use it in GitHub Desktop.
Manage installing/switching between all available node versions (in brew) using fish shell and brew.
# nodevm
# A fish command function to manage installing/switching between all available node versions using brew.
# Usage: nodevm {version}
# Note: {version} is optional.
# if not provided we check to see if node is installed with brew:
# if yes, we switch to whatever that version is.
# if no, we attempt to install node by running "brew install node"
function nodevm
# Default run means we want default version (The one installed using brew install node)
if test -z $argv[1]
set_color green
echo "Switching to default node..."
set_color normal
# Attempt to install if we don't have it already
if not brew ls --versions node >> /dev/null
set_color green
echo "node is not installed. Installing..."
set_color normal
if not brew install node >> /dev/null
set_color red
echo "Error: faild to install node"
set_color normal
return 1
end
end
brew unlink node >> /dev/null
brew link --overwrite --force node >> /dev/null
brew postinstall node >> /dev/null
else
# Switch
set_color green
echo "Switching to node@$argv[1]..."
set_color normal
# Attempt to install if we don't have it already
if not brew ls --versions node@$argv[1] >> /dev/null
set_color green
echo "node@$argv[1] is not installed. Installing..."
set_color normal
if not brew install node@$argv[1] >> /dev/null
set_color red
echo "Error: faild to install node@$argv[1]"
set_color normal
return 1
end
end
brew unlink node >> /dev/null
brew unlink node@$argv[1] >> /dev/null
brew link --overwrite --force node@$argv[1] >> /dev/null
brew postinstall node >> /dev/null
end
# All seems to be good. Now go and have a beer.
set_color green
echo "Success: node "(node -v) "/ npm "(npm -v)
set_color normal
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment