Add automatic node.js switching

This commit is contained in:
Josh Sherman 2022-12-28 15:09:43 -06:00
parent a0a290a98a
commit 2ede8c65a6
No known key found for this signature in database
GPG key ID: 55B058A80530EF22

View file

@ -106,3 +106,27 @@ elif command -v pacman &> /dev/null; then
&& printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
fi fi
# Automatically use Node.js version specified in .nvmrc
autoload -U add-zsh-hook
load-nvmrc() {
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then
nvm use
fi
elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc