This commit is contained in:
Eric Henderson 2024-03-04 17:09:39 +01:00 committed by GitHub
commit e8f02d2fac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -163,22 +163,57 @@ _zsh_nvm_revert() {
fi fi
} }
_current_node_version() {
if _node_is_nvm; then
node --version 2>/dev/null
else
printf 'system'
fi
}
_node_is_nvm() {
local node_path="$(command which node 2>/dev/null)"
if [[ -n "$node_path" && -z "${node_path##$NVM_DIR*}" ]]; then
return 0
else
return 1
fi
}
_nvm_default_version() {
local alias="$(nvm_resolve_alias default)"
if [[ "$alias" = 'node' ]]; then
nvm_ls node | nvm_grep -e '^v' | nvm_grep -v -e "^v0" | tail -n 1
else
nvm version default
fi
}
autoload -U add-zsh-hook autoload -U add-zsh-hook
_zsh_nvm_auto_use() { _zsh_nvm_auto_use() {
_zsh_nvm_has nvm_find_nvmrc || return _zsh_nvm_has nvm_find_nvmrc || return
if [[ $NVM_AUTO_USE != 'true' ]]; then
return
fi
local node_version="$(nvm version)" local node_version="$(_current_node_version)"
local nvmrc_path="$(nvm_find_nvmrc)" local nvmrc_path="$(nvm_find_nvmrc)"
if [[ -n "$nvmrc_path" ]]; then if [[ -n "$nvmrc_path" ]]; then
local nvmrc_dir="$(dirname "$nvmrc_path")"
local nvmrc_version="$(cat "$nvmrc_path")"
local nvmrc_node_version="$(nvm version $(cat "$nvmrc_path"))" local nvmrc_node_version="$(nvm version $(cat "$nvmrc_path"))"
if [[ "$nvmrc_node_version" = "N/A" ]]; then if [[ -v NVM_AUTO_USE_IGNORE_VERSION ]] && [[ ${NVM_AUTO_USE_IGNORE_VERSION[(Ie)$nvmrc_version]} -gt 0 ]]; then
echo "Detected nvm version <$nvmrc_version>, but this version is set to be ignored"
elif [[ -v NVM_AUTO_USE_IGNORE_PATH ]] && [[ ${NVM_AUTO_USE_IGNORE_PATH[(Ie)$nvmrc_dir]} -gt 0 ]]; then
echo "Detected nvm version <$nvmrc_version>, but this path is set to be ignored"
elif [[ "$nvmrc_node_version" = "N/A" ]]; then
nvm install && export NVM_AUTO_USE_ACTIVE=true nvm install && export NVM_AUTO_USE_ACTIVE=true
elif [[ "$nvmrc_node_version" != "$node_version" ]]; then elif [[ "$nvmrc_node_version" != "$node_version" ]]; then
nvm use && export NVM_AUTO_USE_ACTIVE=true nvm use && export NVM_AUTO_USE_ACTIVE=true
fi fi
elif [[ "$node_version" != "$(nvm version default)" ]] && [[ "$NVM_AUTO_USE_ACTIVE" = true ]]; then elif [[ "$NVM_AUTO_USE_ACTIVE" = true ]] && [[ "$node_version" != "$(_nvm_default_version)" ]]; then
echo "Reverting to nvm default version" echo "Reverting to nvm default version"
nvm use default nvm use default
fi fi