From 63595c2579daa79d840499d63d41e4648fccfa1c Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Sun, 22 Jan 2023 19:20:31 +0700 Subject: [PATCH 1/4] 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 a0f0a2cfd07af0e77f339966dcd3f5ae2bebc4c5 Mon Sep 17 00:00:00 2001 From: Riatre Date: Sat, 25 Feb 2023 01:41:46 +0800 Subject: [PATCH 2/4] 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 d85e94c..226bd96 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 9295d10c88b3a469f88367c4623b7d467adc0b98 Mon Sep 17 00:00:00 2001 From: Luke Childs Date: Thu, 2 Mar 2023 00:02:35 +0700 Subject: [PATCH 3/4] 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 226bd96..d85e94c 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 745291dcf20686ec421935f1c3f8f3a2918dd106 Mon Sep 17 00:00:00 2001 From: Sumit Sahrawat Date: Thu, 13 Jul 2023 17:44:12 +0530 Subject: [PATCH 4/4] 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 d85e94c..f4f56f3 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