Skip to content

Instantly share code, notes, and snippets.

@yaronuliel
Created August 23, 2022 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaronuliel/244bef91454816f4c5f2f9213bd6d448 to your computer and use it in GitHub Desktop.
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
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
@yaronuliel
Copy link
Author

To activate - execute the snipper in your ~/.zshrc or ~/.bashrc file

Please the file in your home dir, and add this line to ~/.bashrc:

source auto-nvm-use.sh

Note that this script acts by overriding the default behavior of the cd command

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