1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-11 00:10:08 +00:00

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
Note: _this option cannot be used at the same time as `lazy`. `autoload` will override it and load `nvm` at
startup._
Note: _if used at the same time as `lazy`, `autoload` will start working only after nvm has been lazy-loaded_
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

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