[Refactor] `nvm alias`: reorganize so alias listing is done last

Jordan Harband 2016-08-08 15:50:05 -07:00
parent 091c64c7c5
commit b6eb565ea1
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
1 changed files with 31 additions and 25 deletions

56
nvm.sh
View File

@ -2630,6 +2630,13 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | nvm_grep -v "N/A" | command sed '/^$/d')"
"alias" ) "alias" )
shift shift
local NVM_ALIAS_DIR
NVM_ALIAS_DIR="$(nvm_alias_path)"
local NVM_CURRENT
NVM_CURRENT="$(nvm_ls_current)"
command mkdir -p "${NVM_ALIAS_DIR}/lts"
local ALIAS local ALIAS
ALIAS='--' ALIAS='--'
local TARGET local TARGET
@ -2653,23 +2660,36 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | nvm_grep -v "N/A" | command sed '/^$/d')"
shift shift
done done
local NVM_ALIAS_DIR if [ -z "${TARGET}" ]; then
NVM_ALIAS_DIR="$(nvm_alias_path)" # for some reason the empty string was explicitly passed as the target
command mkdir -p "${NVM_ALIAS_DIR}/lts" # so, unalias it.
local NVM_CURRENT nvm unalias "${ALIAS}"
NVM_CURRENT="$(nvm_ls_current)" return $?
if [ "${TARGET}" = '--' ]; then elif [ "${TARGET}" != '--' ]; then
if [ "${ALIAS}" = '--' ]; then # a target was passed: create an alias
ALIAS='' if [ "${ALIAS#*\/}" != "${ALIAS}" ]; then
nvm_err 'Aliases in subdirectories are not supported.'
return 1
fi fi
VERSION="$(nvm_version "${TARGET}" || return 0)"
if [ "${VERSION}" = 'N/A' ]; then
nvm_err "! WARNING: Version '${TARGET}' does not exist."
fi
nvm_make_alias "${ALIAS}" "${TARGET}"
NVM_CURRENT="${NVM_CURRENT-}" DEFAULT=false nvm_print_formatted_alias "${ALIAS}" "${TARGET}" "$VERSION"
else
if [ "${ALIAS-}" = '--' ]; then
unset 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}"
done done
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}" ] && ([ -z "${ALIAS}" ] || [ "~${ALIAS_NAME}" = "~${ALIAS}" ]); then if [ ! -f "${NVM_ALIAS_DIR}/${ALIAS_NAME}" ] && ([ -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
@ -2683,20 +2703,6 @@ $NVM_LS_REMOTE_POST_MERGED_OUTPUT" | nvm_grep -v "N/A" | command sed '/^$/d')"
done done
return return
fi fi
if [ -z "${TARGET}" ]; then
nvm unalias "${ALIAS}"
return $?
fi
if [ "${ALIAS#*\/}" != "${ALIAS}" ]; then
nvm_err 'Aliases in subdirectories are not supported.'
return 1
fi
VERSION="$(nvm_version "${TARGET}" || return 0)"
if [ "$VERSION" = 'N/A' ]; then
nvm_err "! WARNING: Version '${TARGET}' does not exist."
fi
nvm_make_alias "${ALIAS}" "${TARGET}"
NVM_CURRENT="${NVM_CURRENT-}" DEFAULT=false nvm_print_formatted_alias "${ALIAS}" "${TARGET}" "${VERSION}"
;; ;;
"unalias" ) "unalias" )
local NVM_ALIAS_DIR local NVM_ALIAS_DIR