Skip to content

Instantly share code, notes, and snippets.

@sndrs
Forked from jusopi/check.sh
Last active March 18, 2024 11:29
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save sndrs/5940e9e8a3f506b287233ed65365befb to your computer and use it in GitHub Desktop.
Save sndrs/5940e9e8a3f506b287233ed65365befb 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
}
# bash:
export PROMPT_COMMAND="$PROMPT_COMMAND enter_directory;"
# zsh:
# chpwd_functions=(${chpwd_functions[@]} "enter_directory")
@rjmk
Copy link

rjmk commented Sep 13, 2016

Should line 17 read

export PROMPT_COMMAND="$PROMPT_COMMAND enter_directory;"

($PROMPT_COMMAND rather than $PROMP_COMMAND)?

@sndrs
Copy link
Author

sndrs commented Dec 8, 2016

yep! edited to reflect that - thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment