[Refactor] `nvm alias`: add in `--`-based argument support.

Jordan Harband 2016-08-08 13:37:14 -07:00
parent 21b8f556b2
commit 091c64c7c5
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
1 changed files with 32 additions and 11 deletions

43
nvm.sh
View File

@ -2631,16 +2631,37 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | nvm_grep -v "N/A" | command sed '/^$/d')"
shift shift
local ALIAS local ALIAS
ALIAS="${1-}" ALIAS='--'
local TARGET local TARGET
TARGET="${2-}" TARGET='--'
while [ $# -gt 0 ]
do
case "${1-}" in
--) ;;
--*)
nvm_err "Unsupported option \"${1}\"."
return 55
;;
*)
if [ "${ALIAS}" = '--' ]; then
ALIAS="${1-}"
elif [ "${TARGET}" = '--' ]; then
TARGET="${1-}"
fi
;;
esac
shift
done
local NVM_ALIAS_DIR local NVM_ALIAS_DIR
NVM_ALIAS_DIR="$(nvm_alias_path)" NVM_ALIAS_DIR="$(nvm_alias_path)"
command mkdir -p "$NVM_ALIAS_DIR/lts" command mkdir -p "${NVM_ALIAS_DIR}/lts"
local NVM_CURRENT local NVM_CURRENT
NVM_CURRENT="$(nvm_ls_current)" NVM_CURRENT="$(nvm_ls_current)"
if [ $# -le 1 ]; then if [ "${TARGET}" = '--' ]; then
if [ "${ALIAS}" = '--' ]; then
ALIAS=''
fi
local ALIAS_PATH local ALIAS_PATH
for ALIAS_PATH in "$NVM_ALIAS_DIR/${ALIAS}"*; do for ALIAS_PATH in "$NVM_ALIAS_DIR/${ALIAS}"*; do
NVM_CURRENT="${NVM_CURRENT}" nvm_print_alias_path "$NVM_ALIAS_DIR" "$ALIAS_PATH" NVM_CURRENT="${NVM_CURRENT}" nvm_print_alias_path "$NVM_ALIAS_DIR" "$ALIAS_PATH"
@ -2648,16 +2669,16 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | nvm_grep -v "N/A" | command sed '/^$/d')"
local ALIAS_NAME local ALIAS_NAME
for ALIAS_NAME in "$(nvm_node_prefix)" "stable" "unstable" "$(nvm_iojs_prefix)"; do for ALIAS_NAME in "$(nvm_node_prefix)" "stable" "unstable" "$(nvm_iojs_prefix)"; do
if [ ! -f "${NVM_ALIAS_DIR}/${ALIAS}" ] && ([ $# -lt 1 ] || [ "~${ALIAS_NAME}" = "~${ALIAS}" ]); then if [ ! -f "${NVM_ALIAS_DIR}/${ALIAS}" ] && ([ -z "${ALIAS}" ] || [ "~${ALIAS_NAME}" = "~${ALIAS}" ]); then
NVM_CURRENT="${NVM_CURRENT}" nvm_print_default_alias "$ALIAS_NAME" NVM_CURRENT="${NVM_CURRENT}" nvm_print_default_alias "${ALIAS_NAME}"
fi fi
done done
local LTS_ALIAS local LTS_ALIAS
for ALIAS_PATH in "$NVM_ALIAS_DIR/lts/${ALIAS}"*; do for ALIAS_PATH in "${NVM_ALIAS_DIR}/lts/${ALIAS}"*; do
LTS_ALIAS="$(NVM_LTS=true nvm_print_alias_path "$NVM_ALIAS_DIR" "$ALIAS_PATH")" LTS_ALIAS="$(NVM_LTS=true nvm_print_alias_path "${NVM_ALIAS_DIR}" "${ALIAS_PATH}")"
if [ -n "$LTS_ALIAS" ]; then if [ -n "${LTS_ALIAS}" ]; then
nvm_echo "${LTS_ALIAS-}" nvm_echo "${LTS_ALIAS}"
fi fi
done done
return return
@ -2675,7 +2696,7 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | nvm_grep -v "N/A" | command sed '/^$/d')"
nvm_err "! WARNING: Version '${TARGET}' does not exist." nvm_err "! WARNING: Version '${TARGET}' does not exist."
fi fi
nvm_make_alias "${ALIAS}" "${TARGET}" nvm_make_alias "${ALIAS}" "${TARGET}"
NVM_CURRENT="${NVM_CURRENT-}" DEFAULT=false nvm_print_formatted_alias "${ALIAS}" "${TARGET}" "$VERSION" NVM_CURRENT="${NVM_CURRENT-}" DEFAULT=false nvm_print_formatted_alias "${ALIAS}" "${TARGET}" "${VERSION}"
;; ;;
"unalias" ) "unalias" )
local NVM_ALIAS_DIR local NVM_ALIAS_DIR