Browse Source

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
Carlo Sala 1 month ago
parent
commit
94aa49c0b9
2 changed files with 54 additions and 44 deletions
  1. 1 2
      plugins/nvm/README.md
  2. 53 42
      plugins/nvm/nvm.plugin.zsh

+ 1 - 2
plugins/nvm/README.md

@@ -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

+ 53 - 42
plugins/nvm/nvm.plugin.zsh

@@ -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
+}
+
+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
-if zstyle -t ':omz:plugins:nvm' autoload; then
+  # 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
-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
+  unfunction _omz_setup_autoload
+}
 
-unset NVM_HOMEBREW nvm_completion
+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