Last active
August 8, 2016 04:08
-
-
Save mjj2000/f232b196a7299fe61be94cb3b094a1f3 to your computer and use it in GitHub Desktop.
Switch to target node version of project by nvm automatically(only when .nvmrc exist in current folder)
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
# put this snippet in your shell config(ex: ~/.bashrc, ~/.zshrc, ...) | |
# make sure `.nvmrc` exsits in your project folder | |
function init-node { | |
# switch node version by nvm after changing folder without AVN | |
if [[ -f ".nvmrc" ]]; then | |
# initialize nvm if necessary | |
if [[ -z $(nvm 2>/dev/null) ]]; then | |
# nvm is not initailized | |
echo 'initialize nvm ...' | |
source ~/.nvm/nvm.sh | |
fi | |
# switch to target version | |
if [ $(cat "./.nvmrc") != $(node --version | sed "s/v//") ]; then | |
nvm use | |
fi | |
fi | |
} | |
function cd { | |
builtin cd "$@" | |
init-node | |
} | |
init-node # check after entering shell |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment