Created
August 23, 2022 16:50
-
-
Save yaronuliel/244bef91454816f4c5f2f9213bd6d448 to your computer and use it in GitHub Desktop.
Automatically switch node version using `nvm` use `.nvmrc` file exists in the context
This file contains hidden or 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
function find-up { | |
x=`pwd` | |
result="" | |
while [ -z "$result" ] && [ "$x" != "/" ] ; do | |
result=$(find "$x" -maxdepth 1 -name $1) | |
x=`dirname "$x"` | |
done | |
echo $result | |
} | |
last="" | |
function nvmrc-check { | |
file=$(find-up ".nvmrc") | |
if [ "$last" != "$file" ] && [ ! -z "$file" ] && [ -f "$file" ]; then | |
last=$file | |
nvm use | |
fi | |
} | |
function cd { | |
builtin cd "$@" | |
[[ "$?" == "0" ]] || return | |
nvmrc-check | |
} | |
nvmrc-check > /dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To activate - execute the snipper in your
~/.zshrc
or~/.bashrc
filePlease the file in your home dir, and add this line to
~/.bashrc
:Note that this script acts by overriding the default behavior of the
cd
command