feat(nvm)!: make lazy and autoload options compatible

BREAKING CHANGE: Prior to this commit, if `lazy` and `autoload` options
were enabled at the same time, `lazy` was getting overriden and only
`autoload` was taken into account.
Now they work together and `autoload` will be enabled after `nvm` has
been lazy-loaded.

Closes #11690
This commit is contained in:
Carlo Sala 2024-03-04 11:02:37 +01:00
parent 0ea0d14288
commit 94aa49c0b9
No known key found for this signature in database
GPG key ID: DA6FB450C1A4FE9A
2 changed files with 54 additions and 44 deletions

View file

@ -43,8 +43,7 @@ zstyle ':omz:plugins:nvm' lazy-cmd eslint prettier typescript ...
#### `.nvmrc` autoload #### `.nvmrc` autoload
Note: _this option cannot be used at the same time as `lazy`. `autoload` will override it and load `nvm` at Note: _if used at the same time as `lazy`, `autoload` will start working only after nvm has been lazy-loaded_
startup._
If set, the plugin will automatically load a node version when if finds a If set, the plugin will automatically load a node version when if finds a
[`.nvmrc` file](https://github.com/nvm-sh/nvm#nvmrc) in the current working directory indicating which node [`.nvmrc` file](https://github.com/nvm-sh/nvm#nvmrc) in the current working directory indicating which node

View file

@ -1,3 +1,7 @@
# Don't try to load nvm if command already available
# Note: nvm is a function so we need to use `which`
which nvm &>/dev/null && return
# See https://github.com/nvm-sh/nvm#installation-and-update # See https://github.com/nvm-sh/nvm#installation-and-update
if [[ -z "$NVM_DIR" ]]; then if [[ -z "$NVM_DIR" ]]; then
if [[ -d "$HOME/.nvm" ]]; then if [[ -d "$HOME/.nvm" ]]; then
@ -12,39 +16,34 @@ if [[ -z "$NVM_DIR" ]]; then
fi fi
fi fi
# Don't try to load nvm if command already available
# Note: nvm is a function so we need to use `which`
which nvm &>/dev/null && return
if [[ -z "$NVM_DIR" ]] || [[ ! -f "$NVM_DIR/nvm.sh" ]]; then if [[ -z "$NVM_DIR" ]] || [[ ! -f "$NVM_DIR/nvm.sh" ]]; then
return return
fi fi
if zstyle -t ':omz:plugins:nvm' lazy && \ function _omz_load_nvm_completion {
! zstyle -t ':omz:plugins:nvm' autoload; then local _nvm_completion
# Call nvm when first using nvm, node, npm, pnpm, yarn or other commands in lazy-cmd # Load nvm bash completion
zstyle -a ':omz:plugins:nvm' lazy-cmd nvm_lazy_cmd for _nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do
nvm_lazy_cmd=(nvm node npm npx pnpm yarn $nvm_lazy_cmd) # default values if [[ -f "$_nvm_completion" ]]; then
eval " # Load bashcompinit
function $nvm_lazy_cmd { autoload -U +X bashcompinit && bashcompinit
for func in $nvm_lazy_cmd; do # Bypass compinit call in nvm bash completion script. See:
if (( \$+functions[\$func] )); then # https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93
unfunction \$func ZSH_VERSION= source "$_nvm_completion"
break
fi fi
done done
# Load nvm if it exists in \$NVM_DIR unfunction _omz_load_nvm_completion
[[ -f \"\$NVM_DIR/nvm.sh\" ]] && source \"\$NVM_DIR/nvm.sh\"
\"\$0\" \"\$@\"
} }
"
unset nvm_lazy_cmd function _omz_setup_autoload {
else if ! zstyle -t ':omz:plugins:nvm' autoload; then
source "$NVM_DIR/nvm.sh" unfunction _omz_setup_autoload
return
fi fi
# Autoload nvm when finding a .nvmrc file in the current directory # Autoload nvm when finding a .nvmrc file in the current directory
# Adapted from: https://github.com/nvm-sh/nvm#zsh # Adapted from: https://github.com/nvm-sh/nvm#zsh
if zstyle -t ':omz:plugins:nvm' autoload; then
function load-nvmrc { function load-nvmrc {
local node_version="$(nvm version)" local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)" local nvmrc_path="$(nvm_find_nvmrc)"
@ -72,18 +71,30 @@ if zstyle -t ':omz:plugins:nvm' autoload; then
add-zsh-hook chpwd load-nvmrc add-zsh-hook chpwd load-nvmrc
load-nvmrc load-nvmrc
fi unfunction _omz_setup_autoload
}
# Load nvm bash completion if zstyle -t ':omz:plugins:nvm' lazy; then
for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do # Call nvm when first using nvm, node, npm, pnpm, yarn or other commands in lazy-cmd
if [[ -f "$nvm_completion" ]]; then zstyle -a ':omz:plugins:nvm' lazy-cmd nvm_lazy_cmd
# Load bashcompinit nvm_lazy_cmd=(nvm node npm npx pnpm yarn $nvm_lazy_cmd) # default values
autoload -U +X bashcompinit && bashcompinit eval "
# Bypass compinit call in nvm bash completion script. See: function $nvm_lazy_cmd {
# https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93 for func in $nvm_lazy_cmd; do
ZSH_VERSION= source "$nvm_completion" if (( \$+functions[\$func] )); then
break unfunction \$func
fi fi
done done
# Load nvm if it exists in \$NVM_DIR
unset NVM_HOMEBREW nvm_completion [[ -f \"\$NVM_DIR/nvm.sh\" ]] && source \"\$NVM_DIR/nvm.sh\"
_omz_load_nvm_completion
_omz_setup_autoload
\"\$0\" \"\$@\"
}
"
unset nvm_lazy_cmd
else
source "$NVM_DIR/nvm.sh"
_omz_load_nvm_completion
_omz_setup_autoload
fi