summaryrefslogtreecommitdiff
path: root/plugins/nvm/nvm.plugin.zsh
diff options
context:
space:
mode:
authorAndrew Janke <andrew@apjanke.net>2015-09-10 04:10:18 -0400
committerMarc Cornellà <marc.cornella@live.com>2020-10-09 17:21:03 +0200
commitef44416df2e2ae819b13764dbf6ca87ce099ec36 (patch)
treeddd379838bd4df32048f7b7ff2bf6a073078309c /plugins/nvm/nvm.plugin.zsh
parentfc6c9ca4b40ee1c9ba7fb4b1c8862fb54a8cb1f6 (diff)
downloadzsh-ef44416df2e2ae819b13764dbf6ca87ce099ec36.tar.gz
zsh-ef44416df2e2ae819b13764dbf6ca87ce099ec36.tar.bz2
zsh-ef44416df2e2ae819b13764dbf6ca87ce099ec36.zip
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
Diffstat (limited to 'plugins/nvm/nvm.plugin.zsh')
-rw-r--r--plugins/nvm/nvm.plugin.zsh62
1 files changed, 42 insertions, 20 deletions
diff --git a/plugins/nvm/nvm.plugin.zsh b/plugins/nvm/nvm.plugin.zsh
index 2264a2420..ee8d2324b 100644
--- a/plugins/nvm/nvm.plugin.zsh
+++ b/plugins/nvm/nvm.plugin.zsh
@@ -1,23 +1,45 @@
-# Set NVM_DIR if it isn't already defined
-[[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm"
+# nvm
+#
+# This plugin locates and loads nvm, looking for it in well-known locations.
-# Don't try to load nvm if command already available
-type "nvm" &> /dev/null && return
+() {
+ emulate -L zsh
+ local nvm_install_dir="" dir install_locations
+ if [[ -n $NVM_INSTALL_DIR ]]; then
+ # User-specified path
+ nvm_install_dir=$NVM_INSTALL_DIR
+ else
+ # Well-known common installation locations for NVM
+ install_locations=( ~/.nvm )
+ [[ -n $NVM_DIR ]] && install_locations=($NVM_DIR $install_locations)
+ # Mac Homebrew sticks
+ which brew &>/dev/null && install_locations+=$(brew --prefix nvm)
+ for dir ($install_locations); do
+ if [[ -s $dir/nvm.sh ]]; then
+ nvm_install_dir=$dir
+ break
+ fi
+ done
+ fi
-# Load nvm if it exists in $NVM_DIR
-if [[ -f "$NVM_DIR/nvm.sh" ]]; then
- source "$NVM_DIR/nvm.sh"
- return
-fi
+ if [[ -n $nvm_install_dir ]]; then
+ source $nvm_install_dir/nvm.sh
+ else
+ # No NVM installation found
+ return 0
+ fi
-# Otherwise try to load nvm installed via Homebrew
-
-# User can set this if they have an unusual Homebrew setup
-NVM_HOMEBREW="${NVM_HOMEBREW:-/usr/local/opt/nvm}"
-# Load nvm from Homebrew location if it exists
-[[ -f "$NVM_HOMEBREW/nvm.sh" ]] && source "$NVM_HOMEBREW/nvm.sh"
-# Load nvm bash completion from Homebrew if it exists
-if [[ -f "$NVM_HOMEBREW/etc/bash_completion.d/nvm" ]]; then
- autoload -U +X bashcompinit && bashcompinit
- source "$NVM_HOMEBREW/etc/bash_completion.d/nvm"
-fi
+ # Locate and use the completion file shipped with NVM, instead of this
+ # plugin's completion
+ # (Their bash completion file has zsh portability support)
+ if [[ $ZSH_NVM_BUNDLED_COMPLETION == true ]]; then
+ local bash_comp_file
+ # Homebrew relocates the bash completion file, so look multiple places
+ for bash_comp_file ( bash_completion etc/bash_completion.d/nvm ); do
+ if [[ -s $nvm_install_dir/$bash_comp_file ]]; then
+ source $nvm_install_dir/$bash_comp_file
+ break;
+ fi
+ done
+ fi
+}