Skip to content

Commit 23067bd

Browse files
authored
Add NVM_LAZY_LOAD_EXTRA_COMMANDS option (#67)
1 parent fdf4d6c commit 23067bd

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,17 @@ Performance comparison:
108108
( _zsh_nvm_lazy_load; ) 0.01s user 0.01s system 168% cpu 0.012 total
109109
```
110110

111+
#### Extra commands to trigger lazy loading
112+
By default lazy loading nvm is triggered by running the `nvm`, `node`, `npm` commands or any installed npm global binaries.
113+
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.
114+
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.
115+
116+
```shell
117+
export NVM_LAZY_LOAD_EXTRA_COMMANDS=('vim')
118+
vim --version
119+
#node is now loaded
120+
```
121+
111122
### Don't autoload node
112123

113124
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.
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/sh
2+
source ../common.sh
3+
4+
# Node.js version to install
5+
node_version=v5.11.0
6+
7+
# Load zsh-nvm and install Node.js in subshell
8+
(load_zsh_nvm && nvm install "$node_version" && [[ "$(node --version)" == "$node_version" ]]) || die "node wasn't installed"
9+
10+
# Check node isn't available
11+
[[ "$(node --version)" != "$node_version" ]] || die "node shouldn't be available $(node --version)"
12+
13+
# Set NVM_LAZY_LOAD to true
14+
export NVM_LAZY_LOAD=true
15+
export NVM_LAZY_LOAD_EXTRA_COMMANDS=('hostname' 'whoami')
16+
17+
# Load zsh-nvm
18+
load_zsh_nvm
19+
20+
# Check nvm is a lazy load function
21+
[[ $(which nvm) == *"_zsh_nvm_load"* ]] || die "nvm should be a lazy load function"
22+
23+
# Check node is a lazy load function
24+
[[ $(command -v node) == "node" ]] || die "node should be a shell function"
25+
26+
# Check npm is a lazy load function
27+
[[ $(command -v npm) == "npm" ]] || die "npm should be a shell function"
28+
29+
# Init lazy loader
30+
whoami || die "couldn't run lazy loader"
31+
32+
# Check nvm is not a lazy load function
33+
[[ $(which nvm) != *"_zsh_nvm_load"* ]] || die "nvm should not be a lazy load function"
34+
35+
# Check node is a binary
36+
[[ "$(command -v node)" == "$(nvm_version_path $node_version)/bin/node" ]] || die "node should now be a binary"
37+
38+
# Check npm is a binary
39+
[[ "$(command -v npm)" == "$(nvm_version_path $node_version)/bin/npm" ]] || die "npm should now be a binary"

zsh-nvm.plugin.zsh

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ _zsh_nvm_lazy_load() {
9393

9494
# Add nvm
9595
global_binaries+=('nvm')
96+
global_binaries+=($NVM_LAZY_LOAD_EXTRA_COMMANDS)
9697

9798
# Remove any binaries that conflict with current aliases
9899
local cmds

0 commit comments

Comments
 (0)