Skip to content

Instantly share code, notes, and snippets.

@martin-cotta
Last active August 22, 2017 01:12
Show Gist options
  • Save martin-cotta/010e0492fee893417fca42e54413d66e to your computer and use it in GitHub Desktop.
Save martin-cotta/010e0492fee893417fca42e54413d66e to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
# all the available versions at: https://nodejs.org/dist
NODE_VERSION="v8.1.0"
CURRENT_VERSION=""
install_node() {
echo "downloading and installing node $NODE_VERSION, sudo password might be required"
curl "https://nodejs.org/dist/${NODE_VERSION}/node-${NODE_VERSION}.pkg" > "$HOME/Downloads/node-installer.pkg"
sudo installer -store -pkg "$HOME/Downloads/node-installer.pkg" -target "/"
}
uninstall_node() {
echo "found Node $CURRENT_VERSION but expecting $NODE_VERSION"
echo "removing incorrect version ..."
if brew ls --versions node > /dev/null; then
brew uninstall --ignore-dependencies node
else
echo "sudo password might be required"
sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}
fi
}
# check if node is already installed
if type -P node > /dev/null; then
CURRENT_VERSION="$(node -v)"
if [ "$NODE_VERSION" == "$CURRENT_VERSION" ]; then
echo "Node $NODE_VERSION already installed"
exit 0 # exit script
else
uninstall_node
fi
fi
install_node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment