[Refactor] minor "if" and "return" improvement

Peter Dave Hello 2017-04-06 03:55:42 +08:00 committed by Jordan Harband
parent 2ec2d31012
commit e87f395b7a
No known key found for this signature in database
GPG Key ID: 64A196AEE0916D55
1 changed files with 34 additions and 47 deletions

81
nvm.sh
View File

@ -205,18 +205,16 @@ nvm_rc_version() {
export NVM_RC_VERSION='' export NVM_RC_VERSION=''
local NVMRC_PATH local NVMRC_PATH
NVMRC_PATH="$(nvm_find_nvmrc)" NVMRC_PATH="$(nvm_find_nvmrc)"
if [ -e "${NVMRC_PATH}" ]; then if [ ! -e "${NVMRC_PATH}" ]; then
read -r NVM_RC_VERSION < "${NVMRC_PATH}" || printf ''
if [ -n "${NVM_RC_VERSION}" ]; then
nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>"
else
nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\""
return 2
fi
else
nvm_err "No .nvmrc file found" nvm_err "No .nvmrc file found"
return 1 return 1
fi fi
read -r NVM_RC_VERSION < "${NVMRC_PATH}" || printf ''
if [ ! -n "${NVM_RC_VERSION}" ]; then
nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\""
return 2
fi
nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>"
} }
nvm_clang_version() { nvm_clang_version() {
@ -341,9 +339,8 @@ nvm_version() {
if [ -z "${VERSION}" ] || [ "_${VERSION}" = "_N/A" ]; then if [ -z "${VERSION}" ] || [ "_${VERSION}" = "_N/A" ]; then
nvm_echo "N/A" nvm_echo "N/A"
return 3; return 3;
else
nvm_echo "${VERSION}"
fi fi
nvm_echo "${VERSION}"
} }
nvm_remote_version() { nvm_remote_version() {
@ -1261,10 +1258,9 @@ nvm_checksum() {
elif [ -z "${2-}" ]; then elif [ -z "${2-}" ]; then
nvm_echo 'Checksums empty' #missing in raspberry pi binary nvm_echo 'Checksums empty' #missing in raspberry pi binary
return return
else
nvm_err 'Checksums do not match.'
return 1
fi fi
nvm_err 'Checksums do not match.'
return 1
} }
nvm_print_versions() { nvm_print_versions() {
@ -1540,8 +1536,7 @@ nvm_ensure_default_set() {
if [ -z "$VERSION" ]; then if [ -z "$VERSION" ]; then
nvm_err 'nvm_ensure_default_set: a version is required' nvm_err 'nvm_ensure_default_set: a version is required'
return 1 return 1
fi elif nvm_alias default >/dev/null 2>&1; then
if nvm_alias default >/dev/null 2>&1; then
# default already set # default already set
return 0 return 0
fi fi
@ -1918,7 +1913,7 @@ nvm_install_source() {
TARBALL="$(nvm_download_artifact "${FLAVOR}" source "${TYPE}" "${VERSION}" | command tail -1)" && \ TARBALL="$(nvm_download_artifact "${FLAVOR}" source "${TYPE}" "${VERSION}" | command tail -1)" && \
[ -f "${TARBALL}" ] && \ [ -f "${TARBALL}" ] && \
TMPDIR="$(dirname "${TARBALL}")/files" && \ TMPDIR="$(dirname "${TARBALL}")/files" && \
if ( if ! (
# shellcheck disable=SC2086 # shellcheck disable=SC2086
command mkdir -p "${TMPDIR}" && \ command mkdir -p "${TMPDIR}" && \
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 && \ command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 && \
@ -1930,12 +1925,10 @@ nvm_install_source() {
command rm -f "${VERSION_PATH}" 2>/dev/null && \ command rm -f "${VERSION_PATH}" 2>/dev/null && \
$make -j "${NVM_MAKE_JOBS}" ${MAKE_CXX-} install $make -j "${NVM_MAKE_JOBS}" ${MAKE_CXX-} install
); then ); then
return $? nvm_err "nvm: install ${VERSION} failed!"
command rm -rf "${TMPDIR-}"
return 1
fi fi
nvm_err "nvm: install ${VERSION} failed!"
command rm -rf "${TMPDIR-}"
return 1
} }
nvm_use_if_needed() { nvm_use_if_needed() {
@ -2031,9 +2024,7 @@ nvm_die_on_prefix() {
nvm_err "nvm is not compatible with the \"NPM_CONFIG_PREFIX\" environment variable: currently set to \"$NPM_CONFIG_PREFIX\"" nvm_err "nvm is not compatible with the \"NPM_CONFIG_PREFIX\" environment variable: currently set to \"$NPM_CONFIG_PREFIX\""
nvm_err 'Run `unset NPM_CONFIG_PREFIX` to unset it.' nvm_err 'Run `unset NPM_CONFIG_PREFIX` to unset it.'
return 4 return 4
fi elif ! nvm_has 'npm'; then
if ! nvm_has 'npm'; then
return return
fi fi
@ -2141,20 +2132,20 @@ nvm_check_file_permissions() {
setopt nonomatch setopt nonomatch
fi fi
for FILE in $1/* $1/.[!.]* $1/..?* ; do for FILE in $1/* $1/.[!.]* $1/..?* ; do
if [ -d "$FILE" ]; then if [ -d "$FILE" ]; then
if ! nvm_check_file_permissions "$FILE"; then if ! nvm_check_file_permissions "$FILE"; then
if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then
setopt nomatch
fi
return 2
fi
elif [ -e "$FILE" ] && [ ! -w "$FILE" ] && [ ! -O "$FILE" ]; then
nvm_err "file is not writable or self-owned: $(nvm_sanitize_path "$FILE")"
if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then
setopt nomatch setopt nomatch
fi fi
return 1 return 2
fi fi
elif [ -e "$FILE" ] && [ ! -w "$FILE" ] && [ ! -O "$FILE" ]; then
nvm_err "file is not writable or self-owned: $(nvm_sanitize_path "$FILE")"
if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then
setopt nomatch
fi
return 1
fi
done done
if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then
setopt nomatch setopt nomatch
@ -2378,12 +2369,10 @@ nvm() {
fi fi
else else
nvm_rc_version nvm_rc_version
if [ $version_not_provided -eq 1 ]; then if [ $version_not_provided -eq 1 ] && [ -z "$NVM_RC_VERSION" ]; then
if [ -z "$NVM_RC_VERSION" ]; then unset NVM_RC_VERSION
unset NVM_RC_VERSION >&2 nvm --help
>&2 nvm --help return 127
return 127
fi
fi fi
provided_version="$NVM_RC_VERSION" provided_version="$NVM_RC_VERSION"
unset NVM_RC_VERSION unset NVM_RC_VERSION
@ -3003,10 +2992,9 @@ nvm() {
if [ -n "$NVM_OUTPUT" ]; then if [ -n "$NVM_OUTPUT" ]; then
NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "$NVM_OUTPUT" NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "$NVM_OUTPUT"
return $EXIT_CODE return $EXIT_CODE
else
NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "N/A"
return 3
fi fi
NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "N/A"
return 3
;; ;;
"current" ) "current" )
nvm_version current nvm_version current
@ -3040,10 +3028,9 @@ nvm() {
return return
fi fi
return 1 return 1
else
nvm_err 'System version of node not found.'
return 127
fi fi
nvm_err 'System version of node not found.'
return 127
elif [ "_$VERSION" = "_∞" ]; then elif [ "_$VERSION" = "_∞" ]; then
nvm_err "The alias \"$2\" leads to an infinite loop. Aborting." nvm_err "The alias \"$2\" leads to an infinite loop. Aborting."
return 8 return 8