mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-11-24 06:40:07 +00:00
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
This commit is contained in:
parent
fc6c9ca4b4
commit
ef44416df2
2 changed files with 46 additions and 25 deletions
|
@ -1,9 +1,8 @@
|
||||||
# get the node.js version
|
# get the nvm-controlled node.js version
|
||||||
function nvm_prompt_info() {
|
function nvm_prompt_info() {
|
||||||
[[ -f "$NVM_DIR/nvm.sh" ]] || return
|
|
||||||
local nvm_prompt
|
local nvm_prompt
|
||||||
nvm_prompt=$(node -v 2>/dev/null)
|
which nvm &>/dev/null || return
|
||||||
[[ "${nvm_prompt}x" == "x" ]] && return
|
nvm_prompt=$(nvm current)
|
||||||
nvm_prompt=${nvm_prompt:1}
|
nvm_prompt=${nvm_prompt#v}
|
||||||
echo "${ZSH_THEME_NVM_PROMPT_PREFIX}${nvm_prompt}${ZSH_THEME_NVM_PROMPT_SUFFIX}"
|
echo "${ZSH_THEME_NVM_PROMPT_PREFIX}${nvm_prompt}${ZSH_THEME_NVM_PROMPT_SUFFIX}"
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,45 @@
|
||||||
# Set NVM_DIR if it isn't already defined
|
# nvm
|
||||||
[[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.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
|
||||||
# Load nvm if it exists in $NVM_DIR
|
if [[ -n $NVM_INSTALL_DIR ]]; then
|
||||||
if [[ -f "$NVM_DIR/nvm.sh" ]]; then
|
# User-specified path
|
||||||
source "$NVM_DIR/nvm.sh"
|
nvm_install_dir=$NVM_INSTALL_DIR
|
||||||
return
|
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
|
fi
|
||||||
|
|
||||||
# Otherwise try to load nvm installed via Homebrew
|
if [[ -n $nvm_install_dir ]]; then
|
||||||
|
source $nvm_install_dir/nvm.sh
|
||||||
# User can set this if they have an unusual Homebrew setup
|
else
|
||||||
NVM_HOMEBREW="${NVM_HOMEBREW:-/usr/local/opt/nvm}"
|
# No NVM installation found
|
||||||
# Load nvm from Homebrew location if it exists
|
return 0
|
||||||
[[ -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
|
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
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue