Created
December 4, 2021 13:11
-
-
Save othyn/f5aa2346b5f16d74b57b331710f72dbf to your computer and use it in GitHub Desktop.
Easy ZSH/Bash alias for swapping Node versions on the fly
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
# Switch Node version in brew | |
# Place this in your ~/.bashrc or similar for autoload into your shell | |
function nodesw() { | |
if [ -z "$1" ]; then | |
logInfo "You need to provide a Node formulae version to switch to in format:" | |
logInfo "$ nodesw 16" | |
if command -v jq >/dev/null 2>&1; then | |
logInfo "Installed Node versions via Brew:" | |
brew info --json node | jq -r '.[].versioned_formulae[]' | |
else | |
logInfo "Please install jq to list installed Node versions via: $ brew install jq" | |
fi | |
return 1 | |
fi | |
local nodeFormulaeVersion="node@$1" | |
if brew ls --versions $nodeFormulaeVersion > /dev/null; then | |
logInfo "${nodeFormulaeVersion} is already installed, continuing..." | |
else | |
logInfo "${nodeFormulaeVersion} is not installed, installing..." | |
brew install $nodeFormulaeVersion | |
logInfo "${nodeFormulaeVersion} installed!" | |
fi | |
logInfo "Now linking ${nodeFormulaeVersion} and making it active!" | |
brew unlink node && brew link --force --overwrite $nodeFormulaeVersion | |
logInfo "Done! ${nodeFormulaeVersion} is now active:" | |
node -v | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment