Merge pull request #502 from creationix/nvm_version_path
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Store versions 0.12.0 and newer in a `versions` subdirectory
authored by Jordan Harband · committed by Jordan Harband · parents 4278663, 3747735 · Browse files · 7 files·+77 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+77 added−9 removed
- tests:+41 added−2 removed
- other:+36 added−7 removed
Provenance
- Commit
1c85068- Tree
6fd33c2- Author
Jordan Harband <ljharb@gmail.com>- Committer
Jordan Harband <ljharb@gmail.com>- Parent 1
4278663- Parent 2
3747735
7 files changed+77 added−9 removed
added:test/fast/Listing versions/Running "nvm ls" should list versions in the "versions" directory+12 added
1+#!/bin/sh2+3+die () { echo $@ ; exit 1; }4+5+mkdir -p ../../../versions/v0.12.16+mkdir ../../../v0.1.37+8+. ../../../nvm.sh9+10+nvm ls 0.12 | grep v0.12.1 || die '"nvm ls" did not list a version in the versions/ directory'11+nvm ls 0.1 | grep v0.1.3 || die '"nvm ls" did not list a version not in the versions/ directory'12+modified:test/fast/Listing versions/teardown+1 added
5 unmodified lines6 rmdir ../../../v0.2.37 rmdir ../../../v0.3.18 rmdir ../../../v0.3.39 rmdir ../../../v0.3.910+rmdir ../../../versions11 unalias nvm_has_system_node12 added:test/fast/Unit tests/nvm_version_dir+11 added
1+#!/bin/sh2+3+die () { echo $@ ; exit 1; }4+5+. ../../../nvm.sh6+7+[ "$(nvm_version_dir)" = "$NVM_DIR/versions" ] || die '"nvm_version_dir" did not return new dir path'8+[ "$(nvm_version_dir new)" = "$(nvm_version_dir)" ] || die '"nvm_version_dir new" did not return new dir path'9+[ "$(nvm_version_dir old)" = "$NVM_DIR" ] || die '"nvm_version_dir old" did not return old dir path'10+[ "$(nvm_version_dir foo 2>&1)" = "unknown version dir" ] || die '"nvm_version_dir foo" did not error out'11+added:test/fast/Unit tests/nvm_version_greater+10 added
1+#!/bin/sh2+3+die () { echo $@ ; exit 1; }4+5+. ../../../nvm.sh6+7+nvm_version_greater 0.10.0 0.2.12 || die '"nvm_version_greater 0.10.0 0.2.12" did not return true'8+! nvm_version_greater 0.10.0 0.20.12 || die '"nvm_version_greater 0.10.0 0.20.12" returned true'9+! nvm_version_greater 0.10.0 0.10.0 || die '"nvm_version_greater" returned false for the same two versions'10+modified:test/fast/Unit tests/nvm_version_path+4 added-2 removed
2 unmodified lines3 die () { echo $@ ; exit 1; }4 5 . ../../../nvm.sh6 7-[ "$(nvm_version_path)" = "$NVM_DIR" ] &&8-[ "$(nvm_version_path foo)" = "$NVM_DIR/foo" ]7+[ "$(nvm_version_path foo)" = "$NVM_DIR/foo" ] || die '"nvm_version_path foo" did not return correct location'8+[ "$(nvm_version_path 2>&1)" = "version is required" ] || die '"nvm_version_path" did not error out'9+[ "$(nvm_version_path v0.11.0)" = "$NVM_DIR/v0.11.0" ] || die 'old version has the wrong path'10+[ "$(nvm_version_path v0.12.0)" = "$NVM_DIR/versions/v0.12.0" ] || die 'new version has the wrong path'11 modified:test/fast/setup_dir+3 added
7 unmodified lines8 mkdir -p bak9 for SRC in v* src alias; do10 [ -e "$SRC" ] && mv "$SRC" bak11 done12+ if [ -d versions ]; then13+ mv versions bak14+ fi15 true16 )modified:nvm.sh+36 added-7 removed
93 unmodified lines94 echo "Found '$NVMRC_PATH' with version <$NVM_RC_VERSION>"95 fi96 }97 98+nvm_version_greater() {99+ local LHS100+ LHS=$(echo "$1" | awk -F. '{for (i=1;i<=NF;++i) printf "%010d",$i}')101+ local RHS102+ RHS=$(echo "$2" | awk -F. '{for (i=1;i<=NF;++i) printf "%010d",$i}')103+ [ $LHS \> $RHS ];104+}105+106+nvm_version_dir() {107+ local NVM_USE_NEW_DIR108+ NVM_USE_NEW_DIR="$1"109+ if [ -z "$NVM_USE_NEW_DIR" ] || [ "$NVM_USE_NEW_DIR" = "new" ]; then110+ echo "$NVM_DIR/versions"111+ elif [ "$NVM_USE_NEW_DIR" = "old" ]; then112+ echo "$NVM_DIR"113+ else114+ echo "unknown version dir" >&2115+ return 3116+ fi117+}118+119 nvm_version_path() {120 local VERSION121 VERSION="$1"122 if [ -z "$VERSION" ]; then102- echo "$NVM_DIR"103- elif [ ! -z "$VERSION" ]; then104- echo "$NVM_DIR/$VERSION"123+ echo "version is required" >&2124+ return 3125+ elif nvm_version_greater 0.12.0 "$VERSION"; then126+ echo "$(nvm_version_dir old)/$VERSION"127+ else128+ echo "$(nvm_version_dir new)/$VERSION"129 fi130 }131 132 # Expand a version using the version cache95 unmodified lines228 PATTERN=$(nvm_format_version $PATTERN)229 if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*$"` != 0 ]; then230 PATTERN="$PATTERN."231 fi232+ if [ -d "$(nvm_version_dir new)" ]; then233+ VERSIONS=`find "$(nvm_version_dir new)/" "$(nvm_version_dir old)/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \234+ | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'`235+ else208- VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \209- | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'`236+ VERSIONS=`find "$(nvm_version_dir old)/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \237+ | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'`238+ fi239 fi240 if [ -z "$VERSIONS" ]; then241 echo "N/A"242 return 3538 unmodified lines781 782 npm install -g --quiet $INSTALLS783 ;;784 "clear-cache" )756- rm -f $NVM_DIR/v* 2>/dev/null785+ rm -f $NVM_DIR/v* "$(nvm_version_dir)" 2>/dev/null786 echo "Cache cleared."787 ;;788 "version" )789 nvm_version $21 unmodified line791 "--version" )792 echo "0.13.1"793 ;;794 "unload" )766- unset -f nvm nvm_print_versions nvm_checksum nvm_ls_remote nvm_ls nvm_remote_version nvm_version nvm_rc_version > /dev/null 2>&1795+ unset -f nvm nvm_print_versions nvm_checksum nvm_ls_remote nvm_ls nvm_remote_version nvm_version nvm_rc_version nvm_version_greater > /dev/null 2>&1796 unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1797 ;;798 * )799 nvm help6 unmodified lines