-
-
Save foobear/d77d246d99484f56b61da077b5554216 to your computer and use it in GitHub Desktop.
Bash: Automatically "nvm use" when .nvmrc is detected in current or ancestor directories. Put this at the end of your ~/.bashrc
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
find-up () { | |
path=$(pwd) | |
while [[ "$path" != "" && ! -e "$path/$1" ]]; do | |
path=${path%/*} | |
done | |
echo "$path" | |
} | |
automatic-nvm-use() { | |
NVM_PATH=$(find-up .nvmrc | tr -d '[:space:]') | |
if [[ $NVM_PATH == $NVM_PATH_WAS ]]; then | |
return | |
fi | |
NVM_PATH_WAS=$NVM_PATH | |
if [[ -f "$NVM_PATH/.nvmrc" ]]; then | |
nvm use $(<"$NVM_PATH/.nvmrc") | |
else | |
nvm use default | |
fi | |
} | |
if [[ "$PROMPT_COMMAND" ]]; then | |
export PROMPT_COMMAND=$PROMPT_COMMAND;automatic-nvm-use | |
else | |
export PROMPT_COMMAND=automatic-nvm-use | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment