6d5ba2d6c489d56fb34788ec128f98609e60b7be

Author
Carlo <carlosalag@protonmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

feat(nvm)!: settings now are zstyle-based (#11335)

Co-authored-by: Marc Cornellà <hello@mcornella.com>

Diff

  1diff --git a/plugins/nvm/README.md b/plugins/nvm/README.md
  2index 1acf120503f014afb059654f38f530cbc64d5f00..4c6312e6ea07c9b9819549ecb94159a08c21d0f5 100644
  3--- a/plugins/nvm/README.md
  4+++ b/plugins/nvm/README.md
  5@@ -1,7 +1,7 @@
  6 # nvm plugin
  7 
  8-This plugin adds autocompletions for [nvm](https://github.com/nvm-sh/nvm) — a Node.js version manager.
  9-It also automatically sources nvm, so you don't need to do it manually in your `.zshrc`.
 10+This plugin adds autocompletions for [nvm](https://github.com/nvm-sh/nvm) — a Node.js version manager. It also
 11+automatically sources nvm, so you don't need to do it manually in your `.zshrc`.
 12 
 13 To use it, add `nvm` to the plugins array of your zshrc file:
 14 
 15@@ -21,14 +21,32 @@ These settings should go in your zshrc file, before Oh My Zsh is sourced:
 16   [Homebrew is installed in `/opt/homebrew`](https://docs.brew.sh/Installation). To get the directory where
 17   nvm has been installed, regardless of chip architecture, use `NVM_HOMEBREW=$(brew --prefix nvm)`.
 18 
 19-- **`NVM_LAZY`**: if you want the plugin to defer the load of nvm to speed-up the start of your zsh session,
 20-  set `NVM_LAZY` to `1`. This will source nvm script only when using it, and will create a function for `node`,
 21-  `npm`, `pnpm`, `yarn`, and the command(s) specified by `NVM_LAZY_CMD`, so when you call either of them,
 22-  nvm will be loaded and run with default version.
 23+## Customization
 24 
 25-- **`NVM_LAZY_CMD`**: if you want additional command(s) to trigger lazy loading of nvm, set `NVM_LAZY_CMD` to
 26-  the command or an array of the commands.
 27+#### Lazy startup
 28 
 29-- **`NVM_AUTOLOAD`**: if `NVM_AUTOLOAD` is set to `1`, the plugin will automatically load a node version when
 30-  if finds a [`.nvmrc` file](https://github.com/nvm-sh/nvm#nvmrc) in the current working directory indicating
 31-  which node version to load.
 32+This option will help you to defer nvm's load until you use it to speed-up your zsh startup. This will source
 33+nvm script only when using it, and will create a function for `node`, `npm`, `pnpm`, `yarn`, and the
 34+command(s) specified by `lazy-cmd` option, so when you call either of them, nvm will be loaded and run with
 35+default version. To enable it, you can add this snippet to your zshrc, before Oh My Zsh is sourced:
 36+
 37+```zsh
 38+zstyle ':omz:plugins:nvm' lazy yes
 39+```
 40+
 41+Then, to define extra commands that will also trigger nvm load, you can use a similar syntax, adding as many
 42+as you want:
 43+
 44+```zsh
 45+zstyle ':omz:plugins:nvm' lazy-cmd eslint prettier typescript ...
 46+```
 47+
 48+#### `.nvmrc` autoload
 49+
 50+If set, the plugin will automatically load a node version when if finds a
 51+[`.nvmrc` file](https://github.com/nvm-sh/nvm#nvmrc) in the current working directory indicating which node
 52+version to load. This can be done, similar as previous options, adding:
 53+
 54+```zsh
 55+zstyle ':omz:plugins:nvm' autoload true
 56+```
 57diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh
 58index 1fb4d238b41036f11c0b8d638a4db57295de78a7..ec583cc2db67a2f751b0d72be9d6e63621a463ed 100644
 59--- a/plugins/nvm/nvm.plugin.zsh
 60+++ b/plugins/nvm/nvm.plugin.zsh
 61@@ -16,14 +16,41 @@ fi
 62 # Note: nvm is a function so we need to use `which`
 63 which nvm &>/dev/null && return
 64 
 65-if (( $+NVM_LAZY )); then
 66-  # Call nvm when first using nvm, node, npm, pnpm, yarn or $NVM_LAZY_CMD
 67-  function nvm node npm pnpm yarn $NVM_LAZY_CMD {
 68-    unfunction nvm node npm pnpm yarn $NVM_LAZY_CMD
 69-    # Load nvm if it exists in $NVM_DIR
 70-    [[ -f "$NVM_DIR/nvm.sh" ]] && source "$NVM_DIR/nvm.sh"
 71-    "$0" "$@"
 72-  }
 73+# TODO: 2022-11-11: Remove soft-deprecate options
 74+if (( ${+NVM_LAZY} + ${+NVM_LAZY_CMD} + ${+NVM_AUTOLOAD} )); then
 75+  # Get list of NVM_* variable settings defined
 76+  local -a used_vars
 77+  used_vars=(${(o)parameters[(I)NVM_(AUTOLOAD|LAZY|LAZY_CMD)]})
 78+  # Nicely print the list in the style `var1, var2 and var3`
 79+  echo "${fg[yellow]}[nvm plugin] Variable-style settings are deprecated. Instead of ${(j:, :)used_vars[1,-2]}${used_vars[-2]+ and }${used_vars[-1]}, use:\n"
 80+  if (( $+NVM_AUTOLOAD )); then
 81+    echo "  zstyle ':omz:plugins:nvm' autoload true"
 82+    zstyle ':omz:plugins:nvm' autoload yes
 83+  fi
 84+  if (( $+NVM_LAZY )); then
 85+    echo "  zstyle ':omz:plugins:nvm' lazy true"
 86+    zstyle ':omz:plugins:nvm' lazy yes
 87+  fi
 88+  if (( $+NVM_LAZY_CMD )); then
 89+    echo "  zstyle ':omz:plugins:nvm' lazy-cmd $NVM_LAZY_CMD"
 90+    zstyle ':omz:plugins:nvm' lazy-cmd $NVM_LAZY_CMD
 91+  fi
 92+  echo "$reset_color"
 93+  unset used_vars NVM_AUTOLOAD NVM_LAZY NVM_LAZY_CMD
 94+fi
 95+
 96+if zstyle -t ':omz:plugins:nvm' lazy; then
 97+  # Call nvm when first using nvm, node, npm, pnpm, yarn or other commands in lazy-cmd
 98+  zstyle -a ':omz:plugins:nvm' lazy-cmd nvm_lazy_cmd
 99+  eval "
100+    function nvm node npm pnpm yarn $nvm_lazy_cmd {
101+      unfunction nvm node npm pnpm yarn $nvm_lazy_cmd
102+      # Load nvm if it exists in \$NVM_DIR
103+      [[ -f \"\$NVM_DIR/nvm.sh\" ]] && source \"\$NVM_DIR/nvm.sh\"
104+      \"\$0\" \"\$@\"
105+    }
106+  "
107+  unset nvm_lazy_cmd
108 elif [[ -f "$NVM_DIR/nvm.sh" ]]; then
109   # Load nvm if it exists in $NVM_DIR
110   source "$NVM_DIR/nvm.sh"
111@@ -33,7 +60,7 @@ fi
112 
113 # Autoload nvm when finding a .nvmrc file in the current directory
114 # Adapted from: https://github.com/nvm-sh/nvm#zsh
115-if (( $+NVM_AUTOLOAD )); then
116+if zstyle -t ':omz:plugins:nvm' autoload; then
117   load-nvmrc() {
118     local node_version="$(nvm version)"
119     local nvmrc_path="$(nvm_find_nvmrc)"
120@@ -70,4 +97,4 @@ for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_complet
121   fi
122 done
123 
124-unset NVM_HOMEBREW NVM_LAZY NVM_AUTOLOAD nvm_completion
125+unset NVM_HOMEBREW nvm_completion