From e07475126c90d9484fad8dbc388e94f397bf4f52 Mon Sep 17 00:00:00 2001 From: Joeri de Gooijer Date: Wed, 27 May 2020 16:07:51 +0200 Subject: [PATCH 1/3] Add NVM_LAZY_LOAD_EXTRA_COMMANDS option --- README.md | 10 ++++++ tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS | 39 ++++++++++++++++++++++ zsh-nvm.plugin.zsh | 1 + 3 files changed, 50 insertions(+) create mode 100755 tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS diff --git a/README.md b/README.md index 6659795..eaac982 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,16 @@ Performance comparison: ( _zsh_nvm_lazy_load; ) 0.01s user 0.01s system 168% cpu 0.012 total ``` +#### Extra commands to trigger lazy loading +By default lazy loading nvm is triggered by running the `nvm`, `node`, `npm` commands or any installed npm global binaries. +If you want to trigger the lazy loading via extra arbitrary commands you can define `NVM_LAZY_LOAD_EXTRA_COMMANDS` and set it to an array of commands as strings. + +```shell +export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim', 'ls') +vim --version +#node is now loaded +``` + ### Don't autoload node By default when `nvm` is loaded it'll automatically run `nvm use default` and load your default `node` version along with `npm` and any global modules. You can disable this behaviour by exporting the `NVM_NO_USE` environment variable and setting it to `true`. It must be set before `zsh-nvm` is loaded. diff --git a/tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS b/tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS new file mode 100755 index 0000000..5cbe3fb --- /dev/null +++ b/tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS @@ -0,0 +1,39 @@ +#!/bin/sh +source ../common.sh + +# Node.js version to install +node_version=v5.11.0 + +# Load zsh-nvm and install Node.js in subshell +(load_zsh_nvm && nvm install "$node_version" && [[ "$(node --version)" == "$node_version" ]]) || die "node wasn't installed" + +# Check node isn't available +[[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)" + +# Set NVM_LAZY_LOAD to true +export NVM_LAZY_LOAD=true +export NVM_LAZY_LOAD_EXTRA_COMMANDS=('ls' 'whoami') + +# Load zsh-nvm +load_zsh_nvm + +# Check nvm is a lazy load function +[[ $(which nvm) == *"_zsh_nvm_load"* ]] || die "nvm should be a lazy load function" + +# Check node is a lazy load function +[[ $(command -v node) == "node" ]] || die "node should be a shell function" + +# Check npm is a lazy load function +[[ $(command -v npm) == "npm" ]] || die "npm should be a shell function" + +# Init lazy loader +whoami || die "couldn't run lazy loader" + +# Check nvm is not a lazy load function +[[ $(which nvm) != *"_zsh_nvm_load"* ]] || die "nvm should not be a lazy load function" + +# Check node is a binary +[[ "$(command -v node)" == "$(nvm_version_path $node_version)/bin/node" ]] || die "node should now be a binary" + +# Check npm is a binary +[[ "$(command -v npm)" == "$(nvm_version_path $node_version)/bin/npm" ]] || die "npm should now be a binary" diff --git a/zsh-nvm.plugin.zsh b/zsh-nvm.plugin.zsh index 9c64c44..d85e94c 100644 --- a/zsh-nvm.plugin.zsh +++ b/zsh-nvm.plugin.zsh @@ -93,6 +93,7 @@ _zsh_nvm_lazy_load() { # Add nvm global_binaries+=('nvm') + global_binaries+=($NVM_LAZY_LOAD_EXTRA_COMMANDS) # Remove any binaries that conflict with current aliases local cmds From 8a42d77b777ac63dc8c7ecb06c6898d2e1994a1a Mon Sep 17 00:00:00 2001 From: Joeri de Gooijer Date: Tue, 16 Jun 2020 08:33:02 +0200 Subject: [PATCH 2/3] Make test more resilient Use `hostname` instead of `ls` in test of NVM_LAZY_LOAD_EXTRA_COMMANDS option --- tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS b/tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS index 5cbe3fb..e0e8215 100755 --- a/tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS +++ b/tests/options/NVM_LAZY_LOAD_EXTRA_COMMANDS @@ -12,7 +12,7 @@ node_version=v5.11.0 # Set NVM_LAZY_LOAD to true export NVM_LAZY_LOAD=true -export NVM_LAZY_LOAD_EXTRA_COMMANDS=('ls' 'whoami') +export NVM_LAZY_LOAD_EXTRA_COMMANDS=('hostname' 'whoami') # Load zsh-nvm load_zsh_nvm From 4324516c9b14a16cf1b4bfce8651c394b704bb1d Mon Sep 17 00:00:00 2001 From: Joeri de Gooijer Date: Tue, 16 Jun 2020 08:33:25 +0200 Subject: [PATCH 3/3] Improve explanation of NVM_LAZY_LOAD_EXTRA_COMMANDS option --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index eaac982..42ef45e 100644 --- a/README.md +++ b/README.md @@ -111,9 +111,10 @@ Performance comparison: #### Extra commands to trigger lazy loading By default lazy loading nvm is triggered by running the `nvm`, `node`, `npm` commands or any installed npm global binaries. If you want to trigger the lazy loading via extra arbitrary commands you can define `NVM_LAZY_LOAD_EXTRA_COMMANDS` and set it to an array of commands as strings. +This can be usefull if programs are not in the above list of binaries but do depend on the availability of `node`, e.g. a vim plugin. ```shell -export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim', 'ls') +export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim') vim --version #node is now loaded ```