Skip to content

Instantly share code, notes, and snippets.

@hjzheng
Last active December 5, 2018 09:21
Show Gist options
  • Save hjzheng/8b8a37f6f98473ca64069823139436f0 to your computer and use it in GitHub Desktop.
Save hjzheng/8b8a37f6f98473ca64069823139436f0 to your computer and use it in GitHub Desktop.
auto-switch-node-version.sh
#!/bin/bash
# usage: auto-switch-node-version.sh v8.11.1 'npm install&&npm run build:qa'
# you can also use nvm run 8.11.1 app.js or nvm exec 8.11.1 node app.js
newNodeVersion=$1
oldNodeVersion=`node -v`
source ~/.nvm/nvm.sh
if nvm use --silent ${newNodeVersion}; then
echo "switch node ${newNodeVersion} success!!!"
else
nvm install ${newNodeVersion}
fi
if eval $2; then
nvm use --silent ${oldNodeVersion}
else
nvm use --silent ${oldNodeVersion}
exit 1
fi
echo "switch node ${oldNodeVersion} back!!!"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment