2012-04-09 14:25:49 +08:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
# bash completion for Node Version Manager (NVM)
|
|
|
|
|
2017-01-01 18:13:13 +08:00
|
|
|
if ! nvm &> /dev/null; then
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
2012-04-09 14:25:49 +08:00
|
|
|
__nvm_generate_completion()
|
|
|
|
{
|
|
|
|
declare current_word
|
|
|
|
current_word="${COMP_WORDS[COMP_CWORD]}"
|
2017-12-09 17:47:14 +08:00
|
|
|
# shellcheck disable=SC2207
|
2012-04-09 14:25:49 +08:00
|
|
|
COMPREPLY=($(compgen -W "$1" -- "$current_word"))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
__nvm_commands ()
|
|
|
|
{
|
|
|
|
declare current_word
|
|
|
|
declare command
|
|
|
|
|
|
|
|
current_word="${COMP_WORDS[COMP_CWORD]}"
|
|
|
|
|
2016-11-24 15:59:16 +08:00
|
|
|
COMMANDS='
|
|
|
|
help install uninstall use run exec
|
|
|
|
alias unalias reinstall-packages
|
|
|
|
current list ls list-remote ls-remote
|
2017-06-23 14:21:10 +08:00
|
|
|
install-latest-npm
|
|
|
|
cache deactivate unload
|
2017-03-29 18:13:14 +08:00
|
|
|
version version-remote which'
|
2012-04-09 14:25:49 +08:00
|
|
|
|
|
|
|
if [ ${#COMP_WORDS[@]} == 4 ]; then
|
|
|
|
|
|
|
|
command="${COMP_WORDS[COMP_CWORD-2]}"
|
|
|
|
case "${command}" in
|
|
|
|
alias) __nvm_installed_nodes ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
case "${current_word}" in
|
|
|
|
-*) __nvm_options ;;
|
|
|
|
*) __nvm_generate_completion "$COMMANDS" ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
__nvm_options ()
|
|
|
|
{
|
|
|
|
OPTIONS=''
|
|
|
|
__nvm_generate_completion "$OPTIONS"
|
|
|
|
}
|
|
|
|
|
|
|
|
__nvm_installed_nodes ()
|
|
|
|
{
|
|
|
|
__nvm_generate_completion "$(nvm_ls) $(__nvm_aliases)"
|
|
|
|
}
|
|
|
|
|
|
|
|
__nvm_aliases ()
|
|
|
|
{
|
|
|
|
declare aliases
|
|
|
|
aliases=""
|
2016-11-24 15:59:16 +08:00
|
|
|
if [ -d "$NVM_DIR/alias" ]; then
|
|
|
|
aliases="$(cd "$NVM_DIR/alias" && command find "$PWD" -type f | command sed "s:$PWD/::")"
|
2012-04-09 14:25:49 +08:00
|
|
|
fi
|
2016-07-19 14:40:32 +08:00
|
|
|
echo "${aliases} node stable unstable iojs"
|
2012-04-09 14:25:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
__nvm_alias ()
|
|
|
|
{
|
|
|
|
__nvm_generate_completion "$(__nvm_aliases)"
|
|
|
|
}
|
|
|
|
|
|
|
|
__nvm ()
|
|
|
|
{
|
|
|
|
declare previous_word
|
|
|
|
previous_word="${COMP_WORDS[COMP_CWORD-1]}"
|
|
|
|
|
|
|
|
case "$previous_word" in
|
2015-02-16 16:39:05 +08:00
|
|
|
use|run|exec|ls|list|uninstall) __nvm_installed_nodes ;;
|
2012-04-09 14:25:49 +08:00
|
|
|
alias|unalias) __nvm_alias ;;
|
|
|
|
*) __nvm_commands ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2014-09-30 05:26:19 +08:00
|
|
|
# complete is a bash builtin, but recent versions of ZSH come with a function
|
|
|
|
# called bashcompinit that will create a complete in ZSH. If the user is in
|
2013-03-02 02:55:58 +08:00
|
|
|
# ZSH, load and run bashcompinit before calling the complete function.
|
|
|
|
if [[ -n ${ZSH_VERSION-} ]]; then
|
2016-11-11 19:43:36 +08:00
|
|
|
autoload -U +X bashcompinit && bashcompinit
|
2013-03-02 02:55:58 +08:00
|
|
|
fi
|
|
|
|
|
2017-03-26 06:10:53 +08:00
|
|
|
complete -o default -F __nvm nvm
|