94aa49c0b96fed98d2751107301c16f32de13383

Author
Carlo Sala <carlosalag@protonmail.com>
Committer
Carlo Sala <carlosalag@protonmail.com>
Date

Message

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

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/nvm/README.md b/plugins/nvm/README.md
  2index b5ef221d31ce94de56d76c9b62c8e49a383060c1..e88ce0158bbf748edaa681557b70296f274e7db5 100644
  3--- a/plugins/nvm/README.md
  4+++ b/plugins/nvm/README.md
  5@@ -43,8 +43,7 @@ zstyle ':omz:plugins:nvm' lazy-cmd eslint prettier typescript ...
  6 
  7 #### `.nvmrc` autoload
  8 
  9-Note: _this option cannot be used at the same time as `lazy`. `autoload` will override it and load `nvm` at
 10-startup._
 11+Note: _if used at the same time as `lazy`, `autoload` will start working only after nvm has been lazy-loaded_
 12 
 13 If set, the plugin will automatically load a node version when if finds a
 14 [`.nvmrc` file](https://github.com/nvm-sh/nvm#nvmrc) in the current working directory indicating which node
 15diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh
 16index 94b6661758f77e944105339b31fb8551e29acf50..084b4846f47065082830f1630b4dff1221e98ec1 100644
 17--- a/plugins/nvm/nvm.plugin.zsh
 18+++ b/plugins/nvm/nvm.plugin.zsh
 19@@ -1,3 +1,7 @@
 20+# Don't try to load nvm if command already available
 21+# Note: nvm is a function so we need to use `which`
 22+which nvm &>/dev/null && return
 23+
 24 # See https://github.com/nvm-sh/nvm#installation-and-update
 25 if [[ -z "$NVM_DIR" ]]; then
 26   if [[ -d "$HOME/.nvm" ]]; then
 27@@ -12,39 +16,34 @@ if [[ -z "$NVM_DIR" ]]; then
 28   fi
 29 fi
 30 
 31-# Don't try to load nvm if command already available
 32-# Note: nvm is a function so we need to use `which`
 33-which nvm &>/dev/null && return
 34-
 35 if [[ -z "$NVM_DIR" ]] || [[ ! -f "$NVM_DIR/nvm.sh" ]]; then 
 36   return
 37 fi
 38 
 39-if zstyle -t ':omz:plugins:nvm' lazy && \
 40-  ! zstyle -t ':omz:plugins:nvm' autoload; then
 41-  # Call nvm when first using nvm, node, npm, pnpm, yarn or other commands in lazy-cmd
 42-  zstyle -a ':omz:plugins:nvm' lazy-cmd nvm_lazy_cmd
 43-  nvm_lazy_cmd=(nvm node npm npx pnpm yarn $nvm_lazy_cmd) # default values
 44-  eval "
 45-    function $nvm_lazy_cmd {
 46-      for func in $nvm_lazy_cmd; do
 47-        if (( \$+functions[\$func] )); then
 48-          unfunction \$func
 49-        fi
 50-      done
 51-      # Load nvm if it exists in \$NVM_DIR
 52-      [[ -f \"\$NVM_DIR/nvm.sh\" ]] && source \"\$NVM_DIR/nvm.sh\"
 53-      \"\$0\" \"\$@\"
 54-    }
 55-  "
 56-  unset nvm_lazy_cmd
 57-else
 58-  source "$NVM_DIR/nvm.sh"
 59-fi
 60+function _omz_load_nvm_completion {
 61+  local _nvm_completion
 62+  # Load nvm bash completion
 63+  for _nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do
 64+    if [[ -f "$_nvm_completion" ]]; then
 65+      # Load bashcompinit
 66+      autoload -U +X bashcompinit && bashcompinit
 67+      # Bypass compinit call in nvm bash completion script. See:
 68+      # https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93
 69+      ZSH_VERSION= source "$_nvm_completion"
 70+      break
 71+    fi
 72+  done
 73+  unfunction _omz_load_nvm_completion
 74+}
 75+
 76+function _omz_setup_autoload {
 77+  if ! zstyle -t ':omz:plugins:nvm' autoload; then
 78+    unfunction _omz_setup_autoload
 79+    return
 80+  fi
 81 
 82-# Autoload nvm when finding a .nvmrc file in the current directory
 83-# Adapted from: https://github.com/nvm-sh/nvm#zsh
 84-if zstyle -t ':omz:plugins:nvm' autoload; then
 85+  # Autoload nvm when finding a .nvmrc file in the current directory
 86+  # Adapted from: https://github.com/nvm-sh/nvm#zsh
 87   function load-nvmrc {
 88     local node_version="$(nvm version)"
 89     local nvmrc_path="$(nvm_find_nvmrc)"
 90@@ -72,18 +71,30 @@ if zstyle -t ':omz:plugins:nvm' autoload; then
 91   add-zsh-hook chpwd load-nvmrc
 92 
 93   load-nvmrc
 94-fi
 95-
 96-# Load nvm bash completion
 97-for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do
 98-  if [[ -f "$nvm_completion" ]]; then
 99-    # Load bashcompinit
100-    autoload -U +X bashcompinit && bashcompinit
101-    # Bypass compinit call in nvm bash completion script. See:
102-    # https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93
103-    ZSH_VERSION= source "$nvm_completion"
104-    break
105-  fi
106-done
107+  unfunction _omz_setup_autoload
108+}
109 
110-unset NVM_HOMEBREW nvm_completion
111+if zstyle -t ':omz:plugins:nvm' lazy; then
112+  # Call nvm when first using nvm, node, npm, pnpm, yarn or other commands in lazy-cmd
113+  zstyle -a ':omz:plugins:nvm' lazy-cmd nvm_lazy_cmd
114+  nvm_lazy_cmd=(nvm node npm npx pnpm yarn $nvm_lazy_cmd) # default values
115+  eval "
116+    function $nvm_lazy_cmd {
117+      for func in $nvm_lazy_cmd; do
118+        if (( \$+functions[\$func] )); then