Last active
May 25, 2024 20:35
-
-
Save mpurdon/87b9f8de39e961b622bd8fc771ac456c to your computer and use it in GitHub Desktop.
NVM oh-my-zsh script
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
export NVM_DIR="$HOME/.nvm" | |
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm | |
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion | |
export PATH=$PATH:/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin | |
export PATH=$PATH:$(npm get prefix)/bin | |
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 | |
} | |
autoload -U add-zsh-hook | |
load-nvmrc() { | |
local nvmrc_path=$(find-up .nvmrc) | |
if [ -n "$nvmrc_path" ]; then | |
nvm use | |
elif [ "$(nvm current)" != "none" ]; then | |
nvm deactivate | |
fi | |
} | |
add-zsh-hook chpwd load-nvmrc | |
load-nvmrc |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Used to make good choices related to .nvmrc files in directories