27857e66d0dab0bd05790307dfe5388f3cb2c51a

Author
davidfmatheson <david.f.matheson@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

nvm: add Homebrew's nvm setup (#8316)

Co-authored-by: Marc Cornellà <marc.cornella@live.com>

Diff

 1diff --git a/plugins/nvm/README.md b/plugins/nvm/README.md
 2index 079cf00099f757401216bd35a7387cee066bc3c3..2515da9e8a560e810db77deda87272acaebd1d6c 100644
 3--- a/plugins/nvm/README.md
 4+++ b/plugins/nvm/README.md
 5@@ -1,9 +1,18 @@
 6 # nvm plugin
 7 
 8-This plugin adds autocompletions for [nvm](https://github.com/creationix/nvm) — a Node.js version manager.
 9+This plugin adds autocompletions for [nvm](https://github.com/nvm-sh/nvm) — a Node.js version manager.
10 It also automatically sources nvm, so you don't need to do it manually in your `.zshrc`.
11 
12 To use it, add `nvm` to the plugins array of your zshrc file:
13+
14 ```zsh
15 plugins=(... nvm)
16 ```
17+
18+## Settings
19+
20+- **`NVM_DIR`**: if you have installed nvm in a directory other than `$HOME/.nvm`, set and export `NVM_DIR`
21+  to be the directory where you installed nvm.
22+  
23+- **`NVM_HOMEBREW`**: if you installed nvm via Homebrew, in a directory other than `/usr/local/opt/nvm`, you
24+  can set `NVM_HOMEBREW` to be the directory where you installed it.
25diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh
26index 4bab8e9d701f91bf6998148327e1fcf40b521eda..2264a242060f924ef928dfb97a34cd3f7afab9d5 100644
27--- a/plugins/nvm/nvm.plugin.zsh
28+++ b/plugins/nvm/nvm.plugin.zsh
29@@ -1,8 +1,23 @@
30 # Set NVM_DIR if it isn't already defined
31 [[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm"
32 
33-# Try to load nvm only if command not already available
34-if ! type "nvm" &> /dev/null; then
35-    # Load nvm if it exists
36-    [[ -f "$NVM_DIR/nvm.sh" ]] && source "$NVM_DIR/nvm.sh"
37+# Don't try to load nvm if command already available
38+type "nvm" &> /dev/null && return
39+
40+# Load nvm if it exists in $NVM_DIR
41+if [[ -f "$NVM_DIR/nvm.sh" ]]; then
42+    source "$NVM_DIR/nvm.sh"
43+    return
44+fi
45+
46+# Otherwise try to load nvm installed via Homebrew
47+
48+# User can set this if they have an unusual Homebrew setup
49+NVM_HOMEBREW="${NVM_HOMEBREW:-/usr/local/opt/nvm}"
50+# Load nvm from Homebrew location if it exists
51+[[ -f "$NVM_HOMEBREW/nvm.sh" ]] && source "$NVM_HOMEBREW/nvm.sh"
52+# Load nvm bash completion from Homebrew if it exists
53+if [[ -f "$NVM_HOMEBREW/etc/bash_completion.d/nvm" ]]; then
54+    autoload -U +X bashcompinit && bashcompinit
55+    source "$NVM_HOMEBREW/etc/bash_completion.d/nvm"
56 fi