zsh-nvm/zsh-nvm.plugin.zsh

193 lines
4.7 KiB
Bash
Raw Normal View History

ZSH_NVM_DIR=${0:a:h}
2016-07-30 07:05:39 +08:00
[[ -z "$NVM_DIR" ]] && export NVM_DIR="$HOME/.nvm"
2016-07-09 02:47:16 +08:00
_zsh_nvm_rename_function() {
test -n "$(declare -f $1)" || return
eval "${_/$1/$2}"
unset -f $1
}
2016-07-08 20:39:46 +08:00
_zsh_nvm_has() {
type "$1" > /dev/null 2>&1
}
2016-07-09 02:43:09 +08:00
_zsh_nvm_latest_release_tag() {
2016-08-10 03:54:02 +08:00
echo $(cd "$NVM_DIR" && git fetch --quiet origin && git describe --abbrev=0 --tags --match "v[0-9]*" origin)
2016-07-09 02:43:09 +08:00
}
2016-07-09 02:47:48 +08:00
_zsh_nvm_install() {
echo "Installing nvm..."
2016-07-09 02:47:48 +08:00
git clone https://github.com/creationix/nvm.git "$NVM_DIR"
$(cd "$NVM_DIR" && git checkout --quiet "$(_zsh_nvm_latest_release_tag)")
2016-07-09 02:47:48 +08:00
}
2016-07-09 02:53:31 +08:00
2016-08-28 03:23:44 +08:00
_zsh_nvm_global_binaries() {
# Look for global binaries
2016-09-28 05:02:42 +08:00
local global_binary_paths="$(echo "$NVM_DIR"/v0*/bin/*(N) "$NVM_DIR"/versions/*/*/bin/*(N))"
# If we have some, format them
if [[ -n "$global_binary_paths" ]]; then
echo "$NVM_DIR"/v0*/bin/*(N) "$NVM_DIR"/versions/*/*/bin/*(N) |
xargs -n 1 basename |
sort |
uniq
fi
2016-08-28 03:23:44 +08:00
}
_zsh_nvm_load() {
2016-10-01 20:05:14 +08:00
# Source nvm (check if `nvm use` should be ran after load)
if [[ "$NVM_NO_USE" == true ]]; then
source "$NVM_DIR/nvm.sh" --no-use
else
source "$NVM_DIR/nvm.sh"
fi
# Rename main nvm function
_zsh_nvm_rename_function nvm _zsh_nvm_nvm
# Wrap nvm in our own function
nvm() {
case $1 in
'upgrade')
_zsh_nvm_upgrade
;;
2016-07-27 06:20:32 +08:00
'revert')
_zsh_nvm_revert
;;
'use')
_zsh_nvm_nvm "$@"
export NVM_AUTO_USE_ACTIVE=false
;;
*)
_zsh_nvm_nvm "$@"
;;
esac
}
}
2016-08-27 17:05:40 +08:00
_zsh_nvm_lazy_load() {
# Get all global node module binaries including node
# (only if NVM_NO_USE is off)
local global_binaries
if [[ "$NVM_NO_USE" == true ]]; then
global_binaries=()
else
global_binaries=($(_zsh_nvm_global_binaries))
fi
2016-08-27 17:05:40 +08:00
# Add yarn lazy loader if it's been installed by something other than npm
_zsh_nvm_has yarn && global_binaries+=('yarn')
2016-08-27 17:05:40 +08:00
# Add nvm
2016-08-28 03:23:44 +08:00
global_binaries+=('nvm')
2016-08-27 17:05:40 +08:00
# Remove any binaries that conflict with current aliases
local cmds
cmds=()
for bin in $global_binaries; do
[[ "$(which $bin)" = "$bin: aliased to "* ]] || cmds+=($bin)
done
2016-08-27 17:05:40 +08:00
# Create function for each command
for cmd in $cmds; do
2016-08-27 17:05:40 +08:00
# When called, unset all lazy loaders, load nvm then run current command
eval "$cmd(){
unset -f $cmds
2016-08-27 17:05:40 +08:00
_zsh_nvm_load
$cmd \"\$@\"
}"
done
}
2016-07-09 03:15:49 +08:00
nvm_update() {
echo 'Deprecated, please use `nvm upgrade`'
}
_zsh_nvm_upgrade() {
2016-07-26 02:20:29 +08:00
# Use default upgrade if it's built in
if [[ -n "$(_zsh_nvm_nvm help | grep 'nvm upgrade')" ]]; then
2016-07-26 02:20:29 +08:00
_zsh_nvm_nvm upgrade
return
fi
# Otherwise use our own
local installed_version=$(cd "$NVM_DIR" && git describe --tags)
echo "Installed version is $installed_version"
2016-07-09 04:19:35 +08:00
echo "Checking latest version of nvm..."
local latest_version=$(_zsh_nvm_latest_release_tag)
if [[ "$installed_version" = "$latest_version" ]]; then
echo "You're already up to date"
else
echo "Updating to $latest_version..."
echo "$installed_version" > "$ZSH_NVM_DIR/previous_version"
2016-07-22 03:05:02 +08:00
$(cd "$NVM_DIR" && git fetch --quiet && git checkout "$latest_version")
_zsh_nvm_load
fi
2016-07-09 03:15:49 +08:00
}
2016-07-27 06:20:32 +08:00
_zsh_nvm_previous_version() {
cat "$ZSH_NVM_DIR/previous_version" 2>/dev/null
}
_zsh_nvm_revert() {
local previous_version="$(_zsh_nvm_previous_version)"
if [[ -n "$previous_version" ]]; then
2016-07-27 06:20:32 +08:00
local installed_version=$(cd "$NVM_DIR" && git describe --tags)
if [[ "$installed_version" = "$previous_version" ]]; then
echo "Already reverted to $installed_version"
return
fi
echo "Installed version is $installed_version"
echo "Reverting to $previous_version..."
$(cd "$NVM_DIR" && git checkout "$previous_version")
_zsh_nvm_load
else
echo "No previous version found"
fi
}
2017-01-03 19:04:20 +08:00
autoload -U add-zsh-hook
_zsh_nvm_auto_use() {
_zsh_nvm_has nvm_find_nvmrc || return
2017-01-03 19:04:20 +08:00
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" != "N/A" ] && [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
export NVM_AUTO_USE_ACTIVE=true
2017-01-03 19:04:20 +08:00
fi
elif [ "$node_version" != "$(nvm version default)" ] && [ "$NVM_AUTO_USE_ACTIVE" = true ]; then
2017-01-03 19:04:20 +08:00
echo "Reverting to nvm default version"
nvm use default
fi
}
# Don't init anything if this is true (debug/testing only)
if [[ "$ZSH_NVM_NO_LOAD" != true ]]; then
2016-07-09 02:55:22 +08:00
# Install nvm if it isn't already installed
[[ ! -f "$NVM_DIR/nvm.sh" ]] && _zsh_nvm_install
# If nvm is installed
if [[ -f "$NVM_DIR/nvm.sh" ]]; then
# Load it
[[ "$NVM_LAZY_LOAD" == true ]] && _zsh_nvm_lazy_load || _zsh_nvm_load
2017-01-03 19:04:20 +08:00
# Auto use nvm on chpwd
[[ "$NVM_AUTO_USE" == true ]] && add-zsh-hook chpwd _zsh_nvm_auto_use && _zsh_nvm_auto_use
fi
2016-08-27 17:18:45 +08:00
fi
return true