From 94aa49c0b96fed98d2751107301c16f32de13383 Mon Sep 17 00:00:00 2001 From: Carlo Sala Date: Mon, 4 Mar 2024 11:02:37 +0100 Subject: [PATCH] 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 --- plugins/nvm/README.md | 3 +- plugins/nvm/nvm.plugin.zsh | 95 +++++++++++++++++++++----------------- 2 files changed, 54 insertions(+), 44 deletions(-) diff --git a/plugins/nvm/README.md b/plugins/nvm/README.md index b5ef221d3..e88ce0158 100644 --- a/plugins/nvm/README.md +++ b/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 diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh index 94b666175..084b4846f 100644 --- a/plugins/nvm/nvm.plugin.zsh +++ b/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 +} -# 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