Skip to content

Instantly share code, notes, and snippets.

@goliatone
Forked from sndrs/auto_nvm_use.sh
Created June 5, 2017 16:46
Show Gist options
  • Save goliatone/dc994120dba8db807a9a63823fda64d0 to your computer and use it in GitHub Desktop.
Save goliatone/dc994120dba8db807a9a63823fda64d0 to your computer and use it in GitHub Desktop.
Automatically set node version per project using .nvmrc file
# This will run `nvm use` everytime you change directory, if
# 1. an .nvmrc file is present
# 2. there is no .nvmrc but you're not using your default node
# Add it to your `.bash_profile` (or wherever else is suitable for your setup).
enter_directory(){
if [ "$PWD" != "$PREV_PWD" ]; then
PREV_PWD="$PWD";
if [ -e ".nvmrc" ]; then
nvm use;
elif [[ $(nvm version) != $(nvm version default) ]]; then
echo "Reverting to nvm default version."
nvm use default
fi
fi
}
export PROMPT_COMMAND="$PROMPT_COMMAND enter_directory;"
cd () { builtin cd "$@" && chNodeVersion; }
pushd () { builtin pushd "$@" && chNodeVersion; }
popd () { builtin popd "$@" && chNodeVersion; }
chNodeVersion() {
# if there's a file named ".nvmrc"...
if [ -f ".nvmrc" ] ; then
# use it
nvm use;
fi
}
chNodeVersion;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment