Skip to content

Commit faadbf8

Browse files
author
edwmurph
committed
added a couple minor optimizations; polished a few things
1 parent b057b56 commit faadbf8

File tree

1 file changed

+41
-40
lines changed

1 file changed

+41
-40
lines changed

Diff for: nvm.sh

+41-40
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ nvm_string_contains_regexp() {
309309
return 1
310310
fi
311311
# e.g. "nvm_string_contains_regexp abbc ^aa?b+.$" returns 0
312-
command printf "%s" "$string" | command awk "/$regexp/{ exit 0 }{ exit 1 }"
312+
command printf '%s' "$string" | command awk "/$regexp/{ exit 0 }{ exit 1 }"
313313
}
314314

315315
# Validates that the given semver adheres to the following grammar:
@@ -322,7 +322,7 @@ nvm_is_valid_semver() {
322322
}
323323

324324
nvm_trim_and_reduce_whitespace_to_one_space() {
325-
command printf "%s" "${1-}" |
325+
command printf '%s' "${1-}" |
326326
command tr -d '\n\r' |
327327
command tr '\t' ' ' |
328328
command tr -s ' ' |
@@ -346,12 +346,12 @@ nvm_normalize_semver() {
346346
local validated_semver
347347
validated_semver='';
348348
while [ -n "$semver" ]; do
349-
comparator_set=$(command printf "%s" "$semver" | command head -n1)
350-
semver=$(command printf "%s" "$semver" | command tail -n +2)
349+
comparator_set=$(command printf '%s' "$semver" | command head -n1)
350+
semver=$(command printf '%s' "$semver" | command tail -n +2)
351351
[ -n "$comparator_set" ] || continue
352352

353353
# convert comparators into required grammar
354-
validated_comparator_set=$(command printf " %s " "$comparator_set" |
354+
validated_comparator_set=$(command printf ' %s ' "$comparator_set" |
355355
command sed -E "
356356
# exactly 1 space is needed before and after every comparator (including the first and last comparators)
357357
s/\011/ /g;s/ +/ /g;
@@ -473,10 +473,10 @@ nvm_normalize_semver() {
473473
fi
474474
done
475475

476-
validated_semver=$(command printf "%s" "$validated_semver" | command sed -E 's/^ \|\| //')
476+
validated_semver=$(command printf '%s' "$validated_semver" | command sed -E 's/^ \|\| //')
477477

478478
if nvm_is_valid_semver "$validated_semver"; then
479-
command printf "%s" "$validated_semver"
479+
command printf '%s' "$validated_semver"
480480
else
481481
return 1
482482
fi
@@ -498,7 +498,7 @@ nvm_interpret_complex_semver() {
498498
# - Resolve the comparator_set to its newest compatible version.
499499
# - Add the discovered newest compatible version to highest_compatible_versions.
500500
# - Choose the highest version among all the versions collected in highest_compatible_versions.
501-
semver=$(command printf "%s" "$semver" | command tr '||' '\n')
501+
semver=$(command printf '%s' "$semver" | command tr '||' '\n')
502502
local version_list_copy
503503
local current_comparator_set
504504
local current_version
@@ -509,28 +509,28 @@ nvm_interpret_complex_semver() {
509509
highest_compatible_versions=''
510510

511511
while [ -n "$semver" ]; do
512-
version_list_copy=$(command printf "%s" "$version_list")
513-
current_comparator_set=$(command printf "%s" "$semver" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
514-
semver=$(command printf "%s" "$semver" | command tail -n +2)
512+
version_list_copy=$(command printf '%s' "$version_list")
513+
current_comparator_set=$(command printf '%s' "$semver" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
514+
semver=$(command printf '%s' "$semver" | command tail -n +2)
515515
[ -n "$current_comparator_set" ] || continue
516516

517517
# For each version in the version_list_copy (iterating from newest to oldest):
518518
# - If current_version satisfies all comparators in current_comparator_set, we've found the newest version compatible with all comparators in current current_comparator_set.
519519
# - Add discovered version to highest_compatible_versions and stop iterating through versions for current_comparator_set.
520520
while [ -n "$version_list_copy" ]; do
521-
current_version=$(command printf "%s" "$version_list_copy" | command tail -n1 | command sed -E 's/^ +//;s/ +$//' | nvm_grep -o '^[0-9]\+\.[0-9]\+\.[0-9]\+$')
522-
version_list_copy=$(command printf "%s" "$version_list_copy" | command sed '$d')
521+
current_version=$(command printf '%s' "$version_list_copy" | command tail -n1 | command sed -E 's/^ +//;s/ +$//' | nvm_grep -o '^[0-9]\+\.[0-9]\+\.[0-9]\+$')
522+
version_list_copy=$(command printf '%s' "$version_list_copy" | command sed '$d')
523523
[ -n "$current_version" ] || continue
524524

525525
# For each comparator in the current_comparator_set_copy:
526526
# - If current_version is compatible with all comparators, we know current_version is the newest compatible version
527-
current_comparator_set_copy=$(command printf "%s" "$current_comparator_set" | command tr ' ' '\n')
527+
current_comparator_set_copy=$(command printf '%s' "$current_comparator_set" | command tr ' ' '\n')
528528
while [ -n "$current_comparator_set_copy" ]; do
529-
current_comparator=$(command printf "%s" "$current_comparator_set_copy" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
530-
current_comparator_set_copy=$(command printf "%s" "$current_comparator_set_copy" | command tail -n +2)
529+
current_comparator=$(command printf '%s' "$current_comparator_set_copy" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
530+
current_comparator_set_copy=$(command printf '%s' "$current_comparator_set_copy" | command tail -n +2)
531531
[ -n "$current_comparator" ] || continue
532532

533-
stripped_version_from_comparator=$(command printf "%s" "$current_comparator" | nvm_grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+$')
533+
stripped_version_from_comparator=$(command printf '%s' "$current_comparator" | nvm_grep -o '[0-9]\+\.[0-9]\+\.[0-9]\+$')
534534
if [ -z "$stripped_version_from_comparator" ]; then
535535
return 1
536536
fi
@@ -619,18 +619,18 @@ nvm_interpret_complex_semver() {
619619
local highest_compatible_version
620620
local compatible_node_version
621621
highest_compatible_version='0.0.0'
622-
highest_compatible_versions=$(command printf "%s" "$highest_compatible_versions" | command tr ' ' '\n')
622+
highest_compatible_versions=$(command printf '%s' "$highest_compatible_versions" | command tr ' ' '\n')
623623
while [ -n "$highest_compatible_versions" ]; do
624-
compatible_node_version=$(command printf "%s" "$highest_compatible_versions" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
625-
highest_compatible_versions=$(command printf "%s" "$highest_compatible_versions" | command tail -n +2)
624+
compatible_node_version=$(command printf '%s' "$highest_compatible_versions" | command head -n1 | command sed -E 's/^ +//;s/ +$//')
625+
highest_compatible_versions=$(command printf '%s' "$highest_compatible_versions" | command tail -n +2)
626626
[ -n "$compatible_node_version" ] || continue
627627

628628
if nvm_version_greater "$compatible_node_version" "$highest_compatible_version"; then
629629
highest_compatible_version="$compatible_node_version"
630630
fi
631631
done
632632
if [ "$highest_compatible_version" != '0.0.0' ]; then
633-
command printf "%s" "$highest_compatible_version"
633+
command printf '%s' "$highest_compatible_version"
634634
else
635635
return 1
636636
fi
@@ -646,29 +646,29 @@ nvm_interpret_simple_semver() {
646646
return 1
647647
fi
648648
local stripped_version_from_semver
649-
stripped_version_from_semver=$(command printf "%s" "$semver" | nvm_grep -o '^[0-9]\+\.[0-9]\+\.[0-9]\+$')
649+
stripped_version_from_semver=$(command printf '%s' "$semver" | nvm_grep -o '^[0-9]\+\.[0-9]\+\.[0-9]\+$')
650650
local newest_version_from_list
651-
newest_version_from_list=$(command printf "%s" "$version_list" | tail -n 1 | nvm_grep -o '^[0-9]\+\.[0-9]\+\.[0-9]\+$')
651+
newest_version_from_list=$(command printf '%s' "$version_list" | tail -n 1 | nvm_grep -o '^[0-9]\+\.[0-9]\+\.[0-9]\+$')
652652
if [ -z "$stripped_version_from_semver" ] || [ -z "$newest_version_from_list" ]; then
653653
return 1
654654
fi
655655
local retrieved_version
656656
# if the semver is looking for an exact match, and it exists in the provided list of versions, resolve to that version
657657
if nvm_string_contains_regexp "$semver" '^[0-9]+\.[0-9]+\.[0-9]+$'; then
658-
retrieved_version=$(command printf "%s" "$version_list" | nvm_grep "^$stripped_version_from_semver$")
658+
retrieved_version=$(command printf '%s' "$version_list" | nvm_grep "^$stripped_version_from_semver$")
659659
if [ -n "$retrieved_version" ]; then
660-
command printf "%s" "$retrieved_version"
660+
command printf '%s' "$retrieved_version"
661661
return 0
662662
else
663-
# TODO we know it's not worth doing the complex semver interpratation at this point
663+
command printf '%s' 'STOP' # we have determined no node version will be compatible with the semver
664664
return 1
665665
fi
666666

667667
# Semver is looking for the newest version that is <= to a sepcific version, and the version exists in the provided list of versions, resolve to that version
668668
elif nvm_string_contains_regexp "$semver" '^<=[0-9]+\.[0-9]+\.[0-9]+$'; then
669-
retrieved_version=$(command printf "%s" "$version_list" | nvm_grep "^$stripped_version_from_semver$")
669+
retrieved_version=$(command printf '%s' "$version_list" | nvm_grep "^$stripped_version_from_semver$")
670670
if [ -n "$retrieved_version" ]; then
671-
command printf "%s" "$retrieved_version"
671+
command printf '%s' "$retrieved_version"
672672
return 0
673673
else
674674
return 1 # go on to try complex semver interpretation
@@ -677,7 +677,7 @@ nvm_interpret_simple_semver() {
677677
# Semver is looking for the newest version >= a specific version, and the newest version in the provided list of versions is >= the specified version, resolve to that version.
678678
elif nvm_string_contains_regexp "$semver" '^>=[0-9]+\.[0-9]+\.[0-9]+$'; then
679679
if nvm_version_greater_than_or_equal_to "$newest_version_from_list" "$stripped_version_from_semver"; then
680-
command printf "%s" "$newest_version_from_list"
680+
command printf '%s' "$newest_version_from_list"
681681
return 0
682682
else
683683
# TODO we know it's not worth doing the complex semver interpretation at this point
@@ -686,10 +686,10 @@ nvm_interpret_simple_semver() {
686686

687687
elif nvm_string_contains_regexp "$semver" '^>[0-9]+\.[0-9]+\.[0-9]+$'; then
688688
if nvm_version_greater "$newest_version_from_list" "$stripped_version_from_semver"; then
689-
command printf "%s" "$newest_version_from_list"
689+
command printf '%s' "$newest_version_from_list"
690690
return 0
691691
else
692-
# TODO we know it's not worth doing the complex semver interpretation at this point
692+
command printf '%s' 'STOP' # we have determined no node version will be compatible with the semver
693693
return 1
694694
fi
695695

@@ -720,19 +720,20 @@ nvm_interpret_node_semver() {
720720
return 1
721721
fi
722722

723-
# TODO update nvm_interpret_simple_semver failure output to indicate if it is worth doing complex semver interpretation
724723
# If semver is a single comparator, use quick algorithm to determine newest compatible version
725724
local resolved_version
726725
resolved_version=$(nvm_interpret_simple_semver "$valid_transformed_semver" "$remote_node_versions")
727-
if [ -n "$resolved_version" ]; then
728-
command printf "%s" "$resolved_version"
726+
if [ "$resolved_version" = 'STOP' ]; then
727+
return 1 # nvm_interpret_simple_semver determined no node version will be compatible with the semver
728+
elif [ -n "$resolved_version" ]; then
729+
command printf '%s' "$resolved_version"
729730
return 0
730731
fi
731732

732733
# If semver is a semver with > 1 comparator, iterate through each remote node version from newest to oldest until finding the newest version compatible with all comparators.
733734
resolved_version=$(nvm_interpret_complex_semver "$valid_transformed_semver" "$remote_node_versions")
734735
if [ -n "$resolved_version" ]; then
735-
command printf "%s" "$resolved_version"
736+
command printf '%s' "$resolved_version"
736737
return 0
737738
fi
738739

@@ -742,7 +743,7 @@ nvm_interpret_node_semver() {
742743
nvm_find_package_json() {
743744
dir="$(nvm_find_up 'package.json')"
744745
if [ -e "${dir}/package.json" ]; then
745-
command printf "%s" "${dir}/package.json"
746+
command printf '%s' "${dir}/package.json"
746747
fi
747748
}
748749

@@ -752,7 +753,7 @@ nvm_find_package_json() {
752753
# - semantic expression must match regexp: "[|<> [:alnum:].^=~*-]\+"
753754
nvm_get_node_from_pkg_json() {
754755
local package_json_contents
755-
package_json_contents=${1-}
756+
package_json_contents="${1-}"
756757
local engines_node_value
757758
engines_node_value=''
758759
local open_brackets
@@ -768,23 +769,23 @@ nvm_get_node_from_pkg_json() {
768769
| while read -r i; do
769770
engines_node_value="$engines_node_value$i"
770771
if [ "$i" = '"' ]; then
771-
if [ "$in_quotes" = 1 ]; then
772+
if [ $in_quotes -eq 1 ]; then
772773
in_quotes=0
773774
else
774775
in_quotes=1
775776
fi
776777
# spaces are interpretted as '' here but they need to be retained
777778
elif [ "$i" = '' ]; then
778779
engines_node_value="$engines_node_value "
779-
elif [ "$in_quotes" = 1 ]; then
780+
elif [ $in_quotes -eq 1 ]; then
780781
if [ "$i" = '{' ]; then
781782
open_brackets=$((open_brackets+1))
782783
elif [ "$i" = '}' ]; then
783784
closed_brackets=$((closed_brackets+1))
784785
fi
785786
fi
786787
if [ "$open_brackets" -ne 0 ] && [ "$open_brackets" -eq "$closed_brackets" ]; then
787-
command printf "%s" "$engines_node_value" \
788+
command printf '%s' "$engines_node_value" \
788789
| nvm_grep -o '"node": \?"[|<> [:alnum:].^=~*-]\+"' \
789790
| command tr -d '"' \
790791
| command awk -F: '{ print $2 }' \

0 commit comments

Comments
 (0)