From 52d743363ca343fa1a8bb265a8284f9c9100e88a Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 21 Apr 2020 11:34:01 -0400 Subject: [PATCH 01/14] Add support for NVM_AUTO_USE_IGNORE_VERSION and NVM_AUTO_USE_IGNORE_PATH as methods for skiping the auto-use in specific cases --- zsh-nvm.plugin.zsh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index d85e94c..59e4bbb 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -169,9 +169,15 @@ _zsh_nvm_auto_use() { local nvmrc_path="$(nvm_find_nvmrc)" if [[ -n "$nvmrc_path" ]]; then + local nvmrc_dir="$(dirname "$nvmrc_path")" + local nvmrc_version="$(cat "$nvmrc_path")" local nvmrc_node_version="$(nvm version $(cat "$nvmrc_path"))" - if [[ "$nvmrc_node_version" = "N/A" ]]; then + if [[ -v NVM_AUTO_USE_IGNORE_VERSION ]] && [[ ${NVM_AUTO_USE_IGNORE_VERSION[(Ie)$nvmrc_version]} -gt 0 ]]; then + echo "Detected nvm version <$nvmrc_version>, but this version is set to be ignored" + elif [[ -v NVM_AUTO_USE_IGNORE_PATH ]] && [[ ${NVM_AUTO_USE_IGNORE_PATH[(Ie)$nvmrc_dir]} -gt 0 ]]; then + echo "Detected nvm version <$nvmrc_version>, but this path is set to be ignored" + elif [[ "$nvmrc_node_version" = "N/A" ]]; then nvm install && export NVM_AUTO_USE_ACTIVE=true elif [[ "$nvmrc_node_version" != "$node_version" ]]; then nvm use && export NVM_AUTO_USE_ACTIVE=true From 0cd8789d1e9fdde6dea7b79461e44c8289c7bb90 Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 15 Feb 2022 17:11:47 -0500 Subject: [PATCH 02/14] make it possible to disable auto-use after shell init --- zsh-nvm.plugin.zsh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 59e4bbb..17f7e20 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -164,6 +164,9 @@ _zsh_nvm_revert() { autoload -U add-zsh-hook _zsh_nvm_auto_use() { _zsh_nvm_has nvm_find_nvmrc || return + if [[ $NVM_AUTO_USE != 'true' ]]; then + return + fi local node_version="$(nvm version)" local nvmrc_path="$(nvm_find_nvmrc)" From 9af14935509527c6c9e482caad8f8388a5ce11bb Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 7 Jun 2022 12:08:06 -0400 Subject: [PATCH 03/14] potential performance optimization on NVM_AUTO_USE --- zsh-nvm.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 17f7e20..22b1c19 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -168,10 +168,10 @@ _zsh_nvm_auto_use() { return fi - local node_version="$(nvm version)" local nvmrc_path="$(nvm_find_nvmrc)" if [[ -n "$nvmrc_path" ]]; then + local node_version="$(nvm version)" local nvmrc_dir="$(dirname "$nvmrc_path")" local nvmrc_version="$(cat "$nvmrc_path")" local nvmrc_node_version="$(nvm version $(cat "$nvmrc_path"))" From e57f45a3d060e132ffa5698a3c16ae3340926e99 Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 7 Jun 2022 13:24:22 -0400 Subject: [PATCH 04/14] revert the previous change and replace it with an optimized version of the functionality --- zsh-nvm.plugin.zsh | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 22b1c19..3e52edc 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -161,6 +161,24 @@ _zsh_nvm_revert() { fi } +_current_node_version() { + local node_path="$(command which node 2>/dev/null)" + if [[ -z "${node_path##$NVM_DIR*}" ]]; then + node --version 2>/dev/null + else + printf 'system' + fi +} + +_nvm_default_version() { + local alias="$(nvm_resolve_alias default)" + if [[ "$alias" = 'node' ]]; then + nvm_ls node | nvm_grep -e '^v' | nvm_grep -v -e "^v0" | tail -n 1 + else + nvm version default + fi +} + autoload -U add-zsh-hook _zsh_nvm_auto_use() { _zsh_nvm_has nvm_find_nvmrc || return @@ -168,10 +186,10 @@ _zsh_nvm_auto_use() { return fi + local node_version="$(_current_node_version)" local nvmrc_path="$(nvm_find_nvmrc)" if [[ -n "$nvmrc_path" ]]; then - local node_version="$(nvm version)" local nvmrc_dir="$(dirname "$nvmrc_path")" local nvmrc_version="$(cat "$nvmrc_path")" local nvmrc_node_version="$(nvm version $(cat "$nvmrc_path"))" @@ -185,7 +203,7 @@ _zsh_nvm_auto_use() { elif [[ "$nvmrc_node_version" != "$node_version" ]]; then nvm use && export NVM_AUTO_USE_ACTIVE=true fi - elif [[ "$node_version" != "$(nvm version default)" ]] && [[ "$NVM_AUTO_USE_ACTIVE" = true ]]; then + elif [[ "$NVM_AUTO_USE_ACTIVE" = true ]] && [[ "$node_version" != "$(_nvm_default_version)" ]]; then echo "Reverting to nvm default version" nvm use default fi From 359907432655d28819ace7083b202d383ed89d15 Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 7 Jun 2022 13:34:07 -0400 Subject: [PATCH 05/14] fixing some issues --- zsh-nvm.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 3e52edc..ff50165 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -203,7 +203,7 @@ _zsh_nvm_auto_use() { elif [[ "$nvmrc_node_version" != "$node_version" ]]; then nvm use && export NVM_AUTO_USE_ACTIVE=true fi - elif [[ "$NVM_AUTO_USE_ACTIVE" = true ]] && [[ "$node_version" != "$(_nvm_default_version)" ]]; then + elif [[ "$NVM_AUTO_USE_ACTIVE" != false ]] && [[ "$node_version" != "$(_nvm_default_version)" ]]; then echo "Reverting to nvm default version" nvm use default fi From e32f46e20f0a655d74487b1ee56f0c7334fc48df Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 7 Jun 2022 13:35:29 -0400 Subject: [PATCH 06/14] fixing messaging --- zsh-nvm.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index ff50165..79a5e16 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -204,7 +204,7 @@ _zsh_nvm_auto_use() { nvm use && export NVM_AUTO_USE_ACTIVE=true fi elif [[ "$NVM_AUTO_USE_ACTIVE" != false ]] && [[ "$node_version" != "$(_nvm_default_version)" ]]; then - echo "Reverting to nvm default version" + [[ "$NVM_AUTO_USE_ACTIVE" = true ]] && echo "Reverting to nvm default version" nvm use default fi } From 8f682fafee3f3ea48f2c54ccaed5d2996c30affe Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 7 Jun 2022 13:36:27 -0400 Subject: [PATCH 07/14] further optimization --- zsh-nvm.plugin.zsh | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 79a5e16..cf03dfa 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -204,8 +204,12 @@ _zsh_nvm_auto_use() { nvm use && export NVM_AUTO_USE_ACTIVE=true fi elif [[ "$NVM_AUTO_USE_ACTIVE" != false ]] && [[ "$node_version" != "$(_nvm_default_version)" ]]; then - [[ "$NVM_AUTO_USE_ACTIVE" = true ]] && echo "Reverting to nvm default version" - nvm use default + if [[ "$NVM_AUTO_USE_ACTIVE" = true ]]; then + echo "Reverting to nvm default version" + nvm use default + else + nvm use default >/dev/null + fi fi } From 3d6db8f1eb6bbafd4483c53d57f277528cf0f979 Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 7 Jun 2022 13:37:36 -0400 Subject: [PATCH 08/14] didn't improve performance like i'd hoped, so put this back --- zsh-nvm.plugin.zsh | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index cf03dfa..3e52edc 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -203,13 +203,9 @@ _zsh_nvm_auto_use() { elif [[ "$nvmrc_node_version" != "$node_version" ]]; then nvm use && export NVM_AUTO_USE_ACTIVE=true fi - elif [[ "$NVM_AUTO_USE_ACTIVE" != false ]] && [[ "$node_version" != "$(_nvm_default_version)" ]]; then - if [[ "$NVM_AUTO_USE_ACTIVE" = true ]]; then - echo "Reverting to nvm default version" - nvm use default - else - nvm use default >/dev/null - fi + elif [[ "$NVM_AUTO_USE_ACTIVE" = true ]] && [[ "$node_version" != "$(_nvm_default_version)" ]]; then + echo "Reverting to nvm default version" + nvm use default fi } From a55dc057d93d42c23ce1f444ced11eb8fb7d18f6 Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Tue, 7 Jun 2022 13:49:16 -0400 Subject: [PATCH 09/14] more optimizations --- zsh-nvm.plugin.zsh | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 3e52edc..57c0623 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -162,14 +162,22 @@ _zsh_nvm_revert() { } _current_node_version() { - local node_path="$(command which node 2>/dev/null)" - if [[ -z "${node_path##$NVM_DIR*}" ]]; then + if _node_is_nvm; then node --version 2>/dev/null else printf 'system' fi } +_node_is_nvm() { + local node_path="$(command which node 2>/dev/null)" + if [[ -z "${node_path##$NVM_DIR*}" ]]; then + return 0 + else + return 1 + fi +} + _nvm_default_version() { local alias="$(nvm_resolve_alias default)" if [[ "$alias" = 'node' ]]; then From 75e0ac5c57d0daba858fb23e3d6127e407abf5d6 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Sun, 22 Jan 2023 19:20:31 +0700 Subject: [PATCH 10/14] Update README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 42ef45e..53a70c2 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ > Zsh plugin for installing, updating and loading `nvm` +[![GitHub Donate](https://badgen.net/badge/GitHub/Sponsor/D959A7?icon=github)](https://github.com/sponsors/lukechilds) +[![Bitcoin Donate](https://badgen.net/badge/Bitcoin/Donate/F19537?icon=bitcoin)](https://lu.ke/tip/bitcoin) +[![Lightning Donate](https://badgen.net/badge/Lightning/Donate/F6BC41?icon=bitcoin-lightning)](https://lu.ke/tip/lightning) + [`nvm`](https://github.com/nvm-sh/nvm) is an awesome tool but it can be kind of a pain to install and keep up to date. This zsh plugin allows you to quickly setup `nvm` once, save it in your dotfiles, then never worry about it again. The plugin will install the latest stable release of `nvm` if you don't already have it, and then automatically `source` it for you. You can upgrade `nvm` to the latest version whenever you want without losing your installed `node` versions by running `nvm upgrade`. From c123e04fa97d968de42f907949720708f3f41e95 Mon Sep 17 00:00:00 2001 From: Riatre Date: Sat, 25 Feb 2023 01:41:46 +0800 Subject: [PATCH 11/14] Optimize lazy loading performance (#76) --- zsh-nvm.plugin.zsh | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 57c0623..21cdd9e 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -22,20 +22,6 @@ _zsh_nvm_install() { $(builtin cd "$NVM_DIR" && git checkout --quiet "$(_zsh_nvm_latest_release_tag)") } -_zsh_nvm_global_binaries() { - - # Look for global binaries - local global_binary_paths="$(echo "$NVM_DIR"/v0*/bin/*(N) "$NVM_DIR"/versions/*/*/bin/*(N))" - - # If we have some, format them - if [[ -n "$global_binary_paths" ]]; then - echo "$NVM_DIR"/v0*/bin/*(N) "$NVM_DIR"/versions/*/*/bin/*(N) | - xargs -n 1 basename | - sort | - uniq - fi -} - _zsh_nvm_load() { # Source nvm (check if `nvm use` should be ran after load) @@ -85,7 +71,7 @@ _zsh_nvm_lazy_load() { if [[ "$NVM_NO_USE" == true ]]; then global_binaries=() else - global_binaries=($(_zsh_nvm_global_binaries)) + global_binaries=("$NVM_DIR"/v0*/bin/*(N:t) "$NVM_DIR"/versions/*/*/bin/*(N:t)) fi # Add yarn lazy loader if it's been installed by something other than npm @@ -95,19 +81,22 @@ _zsh_nvm_lazy_load() { global_binaries+=('nvm') global_binaries+=($NVM_LAZY_LOAD_EXTRA_COMMANDS) + # Deduplicate + typeset -U global_binaries + # Remove any binaries that conflict with current aliases local cmds - cmds=() - for bin in $global_binaries; do - [[ "$(which $bin 2> /dev/null)" = "$bin: aliased to "* ]] || cmds+=($bin) - done + IFS=$'\n' cmds=($(whence -w -- "${global_binaries[@]}" 2> /dev/null)) + unset IFS + cmds=(${cmds#*": alias"}) + cmds=(${(@q-)cmds%": "*}) # Create function for each command for cmd in $cmds; do # When called, unset all lazy loaders, load nvm then run current command eval "$cmd(){ - unset -f $cmds > /dev/null 2>&1 + unset -f ${cmds[@]} > /dev/null 2>&1 _zsh_nvm_load $cmd \"\$@\" }" From e293a0914fbf3760f408dc095b0af9131e7513dc Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Thu, 2 Mar 2023 00:02:35 +0700 Subject: [PATCH 12/14] Revert "Optimize lazy loading performance (#76)" (#96) This reverts commit a0f0a2cfd07af0e77f339966dcd3f5ae2bebc4c5. --- zsh-nvm.plugin.zsh | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 21cdd9e..57c0623 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -22,6 +22,20 @@ _zsh_nvm_install() { $(builtin cd "$NVM_DIR" && git checkout --quiet "$(_zsh_nvm_latest_release_tag)") } +_zsh_nvm_global_binaries() { + + # Look for global binaries + local global_binary_paths="$(echo "$NVM_DIR"/v0*/bin/*(N) "$NVM_DIR"/versions/*/*/bin/*(N))" + + # If we have some, format them + if [[ -n "$global_binary_paths" ]]; then + echo "$NVM_DIR"/v0*/bin/*(N) "$NVM_DIR"/versions/*/*/bin/*(N) | + xargs -n 1 basename | + sort | + uniq + fi +} + _zsh_nvm_load() { # Source nvm (check if `nvm use` should be ran after load) @@ -71,7 +85,7 @@ _zsh_nvm_lazy_load() { if [[ "$NVM_NO_USE" == true ]]; then global_binaries=() else - global_binaries=("$NVM_DIR"/v0*/bin/*(N:t) "$NVM_DIR"/versions/*/*/bin/*(N:t)) + global_binaries=($(_zsh_nvm_global_binaries)) fi # Add yarn lazy loader if it's been installed by something other than npm @@ -81,22 +95,19 @@ _zsh_nvm_lazy_load() { global_binaries+=('nvm') global_binaries+=($NVM_LAZY_LOAD_EXTRA_COMMANDS) - # Deduplicate - typeset -U global_binaries - # Remove any binaries that conflict with current aliases local cmds - IFS=$'\n' cmds=($(whence -w -- "${global_binaries[@]}" 2> /dev/null)) - unset IFS - cmds=(${cmds#*": alias"}) - cmds=(${(@q-)cmds%": "*}) + cmds=() + for bin in $global_binaries; do + [[ "$(which $bin 2> /dev/null)" = "$bin: aliased to "* ]] || cmds+=($bin) + done # Create function for each command for cmd in $cmds; do # When called, unset all lazy loaders, load nvm then run current command eval "$cmd(){ - unset -f ${cmds[@]} > /dev/null 2>&1 + unset -f $cmds > /dev/null 2>&1 _zsh_nvm_load $cmd \"\$@\" }" From d3db5265b5b1ed110a00cc9b27d9170bd6656ef4 Mon Sep 17 00:00:00 2001 From: Sumit Sahrawat Date: Thu, 13 Jul 2023 17:44:12 +0530 Subject: [PATCH 13/14] Fix leaking variables using `local` with for loops (#98) --- zsh-nvm.plugin.zsh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 57c0623..e2ef031 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -98,11 +98,13 @@ _zsh_nvm_lazy_load() { # Remove any binaries that conflict with current aliases local cmds cmds=() + local bin for bin in $global_binaries; do [[ "$(which $bin 2> /dev/null)" = "$bin: aliased to "* ]] || cmds+=($bin) done # Create function for each command + local cmd for cmd in $cmds; do # When called, unset all lazy loaders, load nvm then run current command From a430aab4f988a73bc8c402542acdbd66111dbd17 Mon Sep 17 00:00:00 2001 From: Eric Henderson Date: Mon, 4 Mar 2024 10:40:01 -0500 Subject: [PATCH 14/14] fix the case when there is no node at all --- zsh-nvm.plugin.zsh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index e2ef031..6675ba2 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -173,7 +173,7 @@ _current_node_version() { _node_is_nvm() { local node_path="$(command which node 2>/dev/null)" - if [[ -z "${node_path##$NVM_DIR*}" ]]; then + if [[ -n "$node_path" && -z "${node_path##$NVM_DIR*}" ]]; then return 0 else return 1