Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the ability to exclude specific cases from NVM_AUTO_USE #62

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 38 additions & 3 deletions zsh-nvm.plugin.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,57 @@ _zsh_nvm_revert() {
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
_zsh_nvm_auto_use() {
_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)"

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"))"

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
elif [[ "$nvmrc_node_version" != "$node_version" ]]; then
nvm use && export NVM_AUTO_USE_ACTIVE=true
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"
nvm use default
fi
Expand Down