@@ -309,7 +309,7 @@ nvm_string_contains_regexp() {
309
309
return 1
310
310
fi
311
311
# 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 }"
313
313
}
314
314
315
315
# Validates that the given semver adheres to the following grammar:
@@ -322,7 +322,7 @@ nvm_is_valid_semver() {
322
322
}
323
323
324
324
nvm_trim_and_reduce_whitespace_to_one_space () {
325
- command printf " %s " " ${1-} " |
325
+ command printf ' %s ' " ${1-} " |
326
326
command tr -d ' \n\r' |
327
327
command tr ' \t' ' ' |
328
328
command tr -s ' ' |
@@ -346,12 +346,12 @@ nvm_normalize_semver() {
346
346
local validated_semver
347
347
validated_semver=' ' ;
348
348
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)
351
351
[ -n " $comparator_set " ] || continue
352
352
353
353
# convert comparators into required grammar
354
- validated_comparator_set=$( command printf " %s " " $comparator_set " |
354
+ validated_comparator_set=$( command printf ' %s ' " $comparator_set " |
355
355
command sed -E "
356
356
# exactly 1 space is needed before and after every comparator (including the first and last comparators)
357
357
s/\011/ /g;s/ +/ /g;
@@ -473,10 +473,10 @@ nvm_normalize_semver() {
473
473
fi
474
474
done
475
475
476
- validated_semver=$( command printf " %s " " $validated_semver " | command sed -E ' s/^ \|\| //' )
476
+ validated_semver=$( command printf ' %s ' " $validated_semver " | command sed -E ' s/^ \|\| //' )
477
477
478
478
if nvm_is_valid_semver " $validated_semver " ; then
479
- command printf " %s " " $validated_semver "
479
+ command printf ' %s ' " $validated_semver "
480
480
else
481
481
return 1
482
482
fi
@@ -498,7 +498,7 @@ nvm_interpret_complex_semver() {
498
498
# - Resolve the comparator_set to its newest compatible version.
499
499
# - Add the discovered newest compatible version to highest_compatible_versions.
500
500
# - 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' )
502
502
local version_list_copy
503
503
local current_comparator_set
504
504
local current_version
@@ -509,28 +509,28 @@ nvm_interpret_complex_semver() {
509
509
highest_compatible_versions=' '
510
510
511
511
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)
515
515
[ -n " $current_comparator_set " ] || continue
516
516
517
517
# For each version in the version_list_copy (iterating from newest to oldest):
518
518
# - 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.
519
519
# - Add discovered version to highest_compatible_versions and stop iterating through versions for current_comparator_set.
520
520
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' )
523
523
[ -n " $current_version " ] || continue
524
524
525
525
# For each comparator in the current_comparator_set_copy:
526
526
# - 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' )
528
528
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)
531
531
[ -n " $current_comparator " ] || continue
532
532
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]\+$' )
534
534
if [ -z " $stripped_version_from_comparator " ]; then
535
535
return 1
536
536
fi
@@ -619,18 +619,18 @@ nvm_interpret_complex_semver() {
619
619
local highest_compatible_version
620
620
local compatible_node_version
621
621
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' )
623
623
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)
626
626
[ -n " $compatible_node_version " ] || continue
627
627
628
628
if nvm_version_greater " $compatible_node_version " " $highest_compatible_version " ; then
629
629
highest_compatible_version=" $compatible_node_version "
630
630
fi
631
631
done
632
632
if [ " $highest_compatible_version " != ' 0.0.0' ]; then
633
- command printf " %s " " $highest_compatible_version "
633
+ command printf ' %s ' " $highest_compatible_version "
634
634
else
635
635
return 1
636
636
fi
@@ -646,29 +646,29 @@ nvm_interpret_simple_semver() {
646
646
return 1
647
647
fi
648
648
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]\+$' )
650
650
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]\+$' )
652
652
if [ -z " $stripped_version_from_semver " ] || [ -z " $newest_version_from_list " ]; then
653
653
return 1
654
654
fi
655
655
local retrieved_version
656
656
# if the semver is looking for an exact match, and it exists in the provided list of versions, resolve to that version
657
657
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 $" )
659
659
if [ -n " $retrieved_version " ]; then
660
- command printf " %s " " $retrieved_version "
660
+ command printf ' %s ' " $retrieved_version "
661
661
return 0
662
662
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
664
664
return 1
665
665
fi
666
666
667
667
# 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
668
668
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 $" )
670
670
if [ -n " $retrieved_version " ]; then
671
- command printf " %s " " $retrieved_version "
671
+ command printf ' %s ' " $retrieved_version "
672
672
return 0
673
673
else
674
674
return 1 # go on to try complex semver interpretation
@@ -677,7 +677,7 @@ nvm_interpret_simple_semver() {
677
677
# 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.
678
678
elif nvm_string_contains_regexp " $semver " ' ^>=[0-9]+\.[0-9]+\.[0-9]+$' ; then
679
679
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 "
681
681
return 0
682
682
else
683
683
# TODO we know it's not worth doing the complex semver interpretation at this point
@@ -686,10 +686,10 @@ nvm_interpret_simple_semver() {
686
686
687
687
elif nvm_string_contains_regexp " $semver " ' ^>[0-9]+\.[0-9]+\.[0-9]+$' ; then
688
688
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 "
690
690
return 0
691
691
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
693
693
return 1
694
694
fi
695
695
@@ -720,19 +720,20 @@ nvm_interpret_node_semver() {
720
720
return 1
721
721
fi
722
722
723
- # TODO update nvm_interpret_simple_semver failure output to indicate if it is worth doing complex semver interpretation
724
723
# If semver is a single comparator, use quick algorithm to determine newest compatible version
725
724
local resolved_version
726
725
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 "
729
730
return 0
730
731
fi
731
732
732
733
# 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.
733
734
resolved_version=$( nvm_interpret_complex_semver " $valid_transformed_semver " " $remote_node_versions " )
734
735
if [ -n " $resolved_version " ]; then
735
- command printf " %s " " $resolved_version "
736
+ command printf ' %s ' " $resolved_version "
736
737
return 0
737
738
fi
738
739
@@ -742,7 +743,7 @@ nvm_interpret_node_semver() {
742
743
nvm_find_package_json () {
743
744
dir=" $( nvm_find_up ' package.json' ) "
744
745
if [ -e " ${dir} /package.json" ]; then
745
- command printf " %s " " ${dir} /package.json"
746
+ command printf ' %s ' " ${dir} /package.json"
746
747
fi
747
748
}
748
749
@@ -752,7 +753,7 @@ nvm_find_package_json() {
752
753
# - semantic expression must match regexp: "[|<> [:alnum:].^=~*-]\+"
753
754
nvm_get_node_from_pkg_json () {
754
755
local package_json_contents
755
- package_json_contents=${1-}
756
+ package_json_contents=" ${1-} "
756
757
local engines_node_value
757
758
engines_node_value=' '
758
759
local open_brackets
@@ -768,23 +769,23 @@ nvm_get_node_from_pkg_json() {
768
769
| while read -r i; do
769
770
engines_node_value=" $engines_node_value$i "
770
771
if [ " $i " = ' "' ]; then
771
- if [ " $in_quotes " = 1 ]; then
772
+ if [ $in_quotes -eq 1 ]; then
772
773
in_quotes=0
773
774
else
774
775
in_quotes=1
775
776
fi
776
777
# spaces are interpretted as '' here but they need to be retained
777
778
elif [ " $i " = ' ' ]; then
778
779
engines_node_value=" $engines_node_value "
779
- elif [ " $in_quotes " = 1 ]; then
780
+ elif [ $in_quotes -eq 1 ]; then
780
781
if [ " $i " = ' {' ]; then
781
782
open_brackets=$(( open_brackets+ 1 ))
782
783
elif [ " $i " = ' }' ]; then
783
784
closed_brackets=$(( closed_brackets+ 1 ))
784
785
fi
785
786
fi
786
787
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 " \
788
789
| nvm_grep -o ' "node": \?"[|<> [:alnum:].^=~*-]\+"' \
789
790
| command tr -d ' "' \
790
791
| command awk -F: ' { print $2 }' \
0 commit comments