0
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-09-19 04:01:21 +02: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 #### `.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"
fi break
done fi
# Load nvm if it exists in \$NVM_DIR done
[[ -f \"\$NVM_DIR/nvm.sh\" ]] && source \"\$NVM_DIR/nvm.sh\" unfunction _omz_load_nvm_completion
\"\$0\" \"\$@\" }
}
"
unset nvm_lazy_cmd
else
source "$NVM_DIR/nvm.sh"
fi
# Autoload nvm when finding a .nvmrc file in the current directory function _omz_setup_autoload {
# Adapted from: https://github.com/nvm-sh/nvm#zsh if ! zstyle -t ':omz:plugins:nvm' autoload; then
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 { 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
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 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