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