d8cb67023540c1e2e7e4e211e2f7c9fc2d4e0c3c

Author
Marc Cornellà <marc.cornella@live.com>
Committer
Marc Cornellà <marc.cornella@live.com>
Date

Message

nvm: simplify nvm.sh and bash completion loading

Diff

 1diff --git a/lib/nvm.zsh b/lib/nvm.zsh
 2index c4f70c849f52ec58cb649906948e5a7c63a35da9..2fe57a8f42b7d011690530a9dad7626d943dbd3f 100644
 3--- a/lib/nvm.zsh
 4+++ b/lib/nvm.zsh
 5@@ -1,8 +1,6 @@
 6 # get the nvm-controlled node.js version
 7 function nvm_prompt_info() {
 8-  local nvm_prompt
 9   which nvm &>/dev/null || return
10-  nvm_prompt=$(nvm current)
11-  nvm_prompt=${nvm_prompt#v}
12+  local nvm_prompt=${$(nvm current)#v}
13   echo "${ZSH_THEME_NVM_PROMPT_PREFIX}${nvm_prompt}${ZSH_THEME_NVM_PROMPT_SUFFIX}"
14 }
15diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh
16index ee8d2324b3f45d32d6dfa44bc7327efd1ab37061..2c137894b6769fbfcb739d10c2a88db2f30bafc7 100644
17--- a/plugins/nvm/nvm.plugin.zsh
18+++ b/plugins/nvm/nvm.plugin.zsh
19@@ -1,45 +1,30 @@
20-# nvm
21-#
22-# This plugin locates and loads nvm, looking for it in well-known locations.
23+# Set NVM_DIR if it isn't already defined
24+[[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm"
25 
26-() {
27-  emulate -L zsh
28-  local nvm_install_dir="" dir install_locations
29-  if [[ -n $NVM_INSTALL_DIR ]]; then
30-    # User-specified path
31-    nvm_install_dir=$NVM_INSTALL_DIR
32-  else
33-    # Well-known common installation locations for NVM
34-    install_locations=( ~/.nvm )
35-    [[ -n $NVM_DIR ]] && install_locations=($NVM_DIR $install_locations)
36-    # Mac Homebrew sticks 
37-    which brew &>/dev/null && install_locations+=$(brew --prefix nvm)
38-    for dir ($install_locations); do
39-      if [[ -s $dir/nvm.sh ]]; then
40-        nvm_install_dir=$dir
41-        break
42-      fi
43-    done
44-  fi
45+# Don't try to load nvm if command already available
46+which nvm &> /dev/null && return
47 
48-  if [[ -n $nvm_install_dir ]]; then
49-    source $nvm_install_dir/nvm.sh
50-  else
51-    # No NVM installation found
52-    return 0
53-  fi
54+if [[ -f "$NVM_DIR/nvm.sh" ]]; then
55+  # Load nvm if it exists in $NVM_DIR
56+  source "$NVM_DIR/nvm.sh"
57+else
58+  # Otherwise try to load nvm installed via Homebrew
59+  # User can set this if they have an unusual Homebrew setup
60+  NVM_HOMEBREW="${NVM_HOMEBREW:-/usr/local/opt/nvm}"
61+  # Load nvm from Homebrew location if it exists
62+  [[ -f "$NVM_HOMEBREW/nvm.sh" ]] && source "$NVM_HOMEBREW/nvm.sh"
63+fi
64 
65-  # Locate and use the completion file shipped with NVM, instead of this
66-  # plugin's completion
67-  # (Their bash completion file has zsh portability support)
68-  if [[ $ZSH_NVM_BUNDLED_COMPLETION == true ]]; then
69-    local bash_comp_file
70-    # Homebrew relocates the bash completion file, so look multiple places
71-    for bash_comp_file ( bash_completion etc/bash_completion.d/nvm ); do
72-      if [[ -s $nvm_install_dir/$bash_comp_file ]]; then
73-        source $nvm_install_dir/$bash_comp_file
74-        break;
75-      fi
76-    done
77+# Load nvm bash completion
78+for nvm_completion in "$NVM_DIR/bash_completion" "$NVM_HOMEBREW/etc/bash_completion.d/nvm"; do
79+  if [[ -f "$nvm_completion" ]]; then
80+    # Load bashcompinit
81+    autoload -U +X bashcompinit && bashcompinit
82+    # Bypass compinit call in nvm bash completion script. See:
83+    # https://github.com/nvm-sh/nvm/blob/4436638/bash_completion#L86-L93
84+    ZSH_VERSION= source "$nvm_completion"
85+    break
86   fi
87-}
88+done
89+
90+unset NVM_HOMEBREW nvm_completion