Skip to content

Commit 315f0df

Browse files
[Refactor] minor "if" and "return" improvement
1 parent ec3ba19 commit 315f0df

File tree

1 file changed

+34
-47
lines changed

1 file changed

+34
-47
lines changed

nvm.sh

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -205,18 +205,16 @@ nvm_rc_version() {
205205
export NVM_RC_VERSION=''
206206
local NVMRC_PATH
207207
NVMRC_PATH="$(nvm_find_nvmrc)"
208-
if [ -e "${NVMRC_PATH}" ]; then
209-
read -r NVM_RC_VERSION < "${NVMRC_PATH}" || printf ''
210-
if [ -n "${NVM_RC_VERSION}" ]; then
211-
nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>"
212-
else
213-
nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\""
214-
return 2
215-
fi
216-
else
208+
if [ ! -e "${NVMRC_PATH}" ]; then
217209
nvm_err "No .nvmrc file found"
218210
return 1
219211
fi
212+
read -r NVM_RC_VERSION < "${NVMRC_PATH}" || printf ''
213+
if [ ! -n "${NVM_RC_VERSION}" ]; then
214+
nvm_err "Warning: empty .nvmrc file found at \"${NVMRC_PATH}\""
215+
return 2
216+
fi
217+
nvm_echo "Found '${NVMRC_PATH}' with version <${NVM_RC_VERSION}>"
220218
}
221219

222220
nvm_clang_version() {
@@ -341,9 +339,8 @@ nvm_version() {
341339
if [ -z "${VERSION}" ] || [ "_${VERSION}" = "_N/A" ]; then
342340
nvm_echo "N/A"
343341
return 3;
344-
else
345-
nvm_echo "${VERSION}"
346342
fi
343+
nvm_echo "${VERSION}"
347344
}
348345

349346
nvm_remote_version() {
@@ -1261,10 +1258,9 @@ nvm_checksum() {
12611258
elif [ -z "${2-}" ]; then
12621259
nvm_echo 'Checksums empty' #missing in raspberry pi binary
12631260
return
1264-
else
1265-
nvm_err 'Checksums do not match.'
1266-
return 1
12671261
fi
1262+
nvm_err 'Checksums do not match.'
1263+
return 1
12681264
}
12691265

12701266
nvm_print_versions() {
@@ -1540,8 +1536,7 @@ nvm_ensure_default_set() {
15401536
if [ -z "$VERSION" ]; then
15411537
nvm_err 'nvm_ensure_default_set: a version is required'
15421538
return 1
1543-
fi
1544-
if nvm_alias default >/dev/null 2>&1; then
1539+
elif nvm_alias default >/dev/null 2>&1; then
15451540
# default already set
15461541
return 0
15471542
fi
@@ -1918,7 +1913,7 @@ nvm_install_source() {
19181913
TARBALL="$(nvm_download_artifact "${FLAVOR}" source "${TYPE}" "${VERSION}" | command tail -1)" && \
19191914
[ -f "${TARBALL}" ] && \
19201915
TMPDIR="$(dirname "${TARBALL}")/files" && \
1921-
if (
1916+
if ! (
19221917
# shellcheck disable=SC2086
19231918
command mkdir -p "${TMPDIR}" && \
19241919
command "${tar}" -x${tar_compression_flag}f "${TARBALL}" -C "${TMPDIR}" --strip-components 1 && \
@@ -1930,12 +1925,10 @@ nvm_install_source() {
19301925
command rm -f "${VERSION_PATH}" 2>/dev/null && \
19311926
$make -j "${NVM_MAKE_JOBS}" ${MAKE_CXX-} install
19321927
); then
1933-
return $?
1928+
nvm_err "nvm: install ${VERSION} failed!"
1929+
command rm -rf "${TMPDIR-}"
1930+
return 1
19341931
fi
1935-
1936-
nvm_err "nvm: install ${VERSION} failed!"
1937-
command rm -rf "${TMPDIR-}"
1938-
return 1
19391932
}
19401933

19411934
nvm_use_if_needed() {
@@ -2031,9 +2024,7 @@ nvm_die_on_prefix() {
20312024
nvm_err "nvm is not compatible with the \"NPM_CONFIG_PREFIX\" environment variable: currently set to \"$NPM_CONFIG_PREFIX\""
20322025
nvm_err 'Run `unset NPM_CONFIG_PREFIX` to unset it.'
20332026
return 4
2034-
fi
2035-
2036-
if ! nvm_has 'npm'; then
2027+
elif ! nvm_has 'npm'; then
20372028
return
20382029
fi
20392030

@@ -2141,20 +2132,20 @@ nvm_check_file_permissions() {
21412132
setopt nonomatch
21422133
fi
21432134
for FILE in $1/* $1/.[!.]* $1/..?* ; do
2144-
if [ -d "$FILE" ]; then
2145-
if ! nvm_check_file_permissions "$FILE"; then
2146-
if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then
2147-
setopt nomatch
2148-
fi
2149-
return 2
2150-
fi
2151-
elif [ -e "$FILE" ] && [ ! -w "$FILE" ] && [ ! -O "$FILE" ]; then
2152-
nvm_err "file is not writable or self-owned: $(nvm_sanitize_path "$FILE")"
2135+
if [ -d "$FILE" ]; then
2136+
if ! nvm_check_file_permissions "$FILE"; then
21532137
if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then
21542138
setopt nomatch
21552139
fi
2156-
return 1
2140+
return 2
2141+
fi
2142+
elif [ -e "$FILE" ] && [ ! -w "$FILE" ] && [ ! -O "$FILE" ]; then
2143+
nvm_err "file is not writable or self-owned: $(nvm_sanitize_path "$FILE")"
2144+
if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then
2145+
setopt nomatch
21572146
fi
2147+
return 1
2148+
fi
21582149
done
21592150
if [ "${ZSH_HAS_NONOMATCH_UNSET}" -eq 1 ] && nvm_has "setopt"; then
21602151
setopt nomatch
@@ -2378,12 +2369,10 @@ nvm() {
23782369
fi
23792370
else
23802371
nvm_rc_version
2381-
if [ $version_not_provided -eq 1 ]; then
2382-
if [ -z "$NVM_RC_VERSION" ]; then
2383-
unset NVM_RC_VERSION
2384-
>&2 nvm --help
2385-
return 127
2386-
fi
2372+
if [ $version_not_provided -eq 1 ] && [ -z "$NVM_RC_VERSION" ]; then
2373+
unset NVM_RC_VERSION
2374+
>&2 nvm --help
2375+
return 127
23872376
fi
23882377
provided_version="$NVM_RC_VERSION"
23892378
unset NVM_RC_VERSION
@@ -3003,10 +2992,9 @@ nvm() {
30032992
if [ -n "$NVM_OUTPUT" ]; then
30042993
NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "$NVM_OUTPUT"
30052994
return $EXIT_CODE
3006-
else
3007-
NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "N/A"
3008-
return 3
30092995
fi
2996+
NVM_NO_COLORS="${NVM_NO_COLORS-}" nvm_print_versions "N/A"
2997+
return 3
30102998
;;
30112999
"current" )
30123000
nvm_version current
@@ -3040,10 +3028,9 @@ nvm() {
30403028
return
30413029
fi
30423030
return 1
3043-
else
3044-
nvm_err 'System version of node not found.'
3045-
return 127
30463031
fi
3032+
nvm_err 'System version of node not found.'
3033+
return 127
30473034
elif [ "_$VERSION" = "_∞" ]; then
30483035
nvm_err "The alias \"$2\" leads to an infinite loop. Aborting."
30493036
return 8

0 commit comments

Comments
 (0)