Merge pull request #502 from creationix/nvm_version_path

Store versions 0.12.0 and newer in a `versions` subdirectory
master
Jordan Harband 2014-08-20 22:49:17 -07:00
commit 1c85068091
7 changed files with 77 additions and 9 deletions

43
nvm.sh
View File

@ -95,13 +95,37 @@ nvm_rc_version() {
fi fi
} }
nvm_version_greater() {
local LHS
LHS=$(echo "$1" | awk -F. '{for (i=1;i<=NF;++i) printf "%010d",$i}')
local RHS
RHS=$(echo "$2" | awk -F. '{for (i=1;i<=NF;++i) printf "%010d",$i}')
[ $LHS \> $RHS ];
}
nvm_version_dir() {
local NVM_USE_NEW_DIR
NVM_USE_NEW_DIR="$1"
if [ -z "$NVM_USE_NEW_DIR" ] || [ "$NVM_USE_NEW_DIR" = "new" ]; then
echo "$NVM_DIR/versions"
elif [ "$NVM_USE_NEW_DIR" = "old" ]; then
echo "$NVM_DIR"
else
echo "unknown version dir" >&2
return 3
fi
}
nvm_version_path() { nvm_version_path() {
local VERSION local VERSION
VERSION="$1" VERSION="$1"
if [ -z "$VERSION" ]; then if [ -z "$VERSION" ]; then
echo "$NVM_DIR" echo "version is required" >&2
elif [ ! -z "$VERSION" ]; then return 3
echo "$NVM_DIR/$VERSION" elif nvm_version_greater 0.12.0 "$VERSION"; then
echo "$(nvm_version_dir old)/$VERSION"
else
echo "$(nvm_version_dir new)/$VERSION"
fi fi
} }
@ -205,8 +229,13 @@ nvm_ls() {
if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*$"` != 0 ]; then if [ `expr "$PATTERN" : "v[0-9]*\.[0-9]*$"` != 0 ]; then
PATTERN="$PATTERN." PATTERN="$PATTERN."
fi fi
VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \ if [ -d "$(nvm_version_dir new)" ]; then
| sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'` VERSIONS=`find "$(nvm_version_dir new)/" "$(nvm_version_dir old)/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \
| sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'`
else
VERSIONS=`find "$(nvm_version_dir old)/" -maxdepth 1 -type d -name "$PATTERN*" -exec basename '{}' ';' \
| sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n | \grep -v '^ *\.' | \grep -e '^v'`
fi
fi fi
if [ -z "$VERSIONS" ]; then if [ -z "$VERSIONS" ]; then
echo "N/A" echo "N/A"
@ -753,7 +782,7 @@ nvm() {
npm install -g --quiet $INSTALLS npm install -g --quiet $INSTALLS
;; ;;
"clear-cache" ) "clear-cache" )
rm -f $NVM_DIR/v* 2>/dev/null rm -f $NVM_DIR/v* "$(nvm_version_dir)" 2>/dev/null
echo "Cache cleared." echo "Cache cleared."
;; ;;
"version" ) "version" )
@ -763,7 +792,7 @@ nvm() {
echo "0.13.1" echo "0.13.1"
;; ;;
"unload" ) "unload" )
unset -f nvm nvm_print_versions nvm_checksum nvm_ls_remote nvm_ls nvm_remote_version nvm_version nvm_rc_version > /dev/null 2>&1 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>&1
unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1 unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_DIR NVM_CD_FLAGS > /dev/null 2>&1
;; ;;
* ) * )

View File

@ -0,0 +1,12 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
mkdir -p ../../../versions/v0.12.1
mkdir ../../../v0.1.3
. ../../../nvm.sh
nvm ls 0.12 | grep v0.12.1 || die '"nvm ls" did not list a version in the versions/ directory'
nvm ls 0.1 | grep v0.1.3 || die '"nvm ls" did not list a version not in the versions/ directory'

View File

@ -7,5 +7,6 @@ rmdir ../../../v0.2.3
rmdir ../../../v0.3.1 rmdir ../../../v0.3.1
rmdir ../../../v0.3.3 rmdir ../../../v0.3.3
rmdir ../../../v0.3.9 rmdir ../../../v0.3.9
rmdir ../../../versions
unalias nvm_has_system_node unalias nvm_has_system_node

View File

@ -0,0 +1,11 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
[ "$(nvm_version_dir)" = "$NVM_DIR/versions" ] || die '"nvm_version_dir" did not return new dir path'
[ "$(nvm_version_dir new)" = "$(nvm_version_dir)" ] || die '"nvm_version_dir new" did not return new dir path'
[ "$(nvm_version_dir old)" = "$NVM_DIR" ] || die '"nvm_version_dir old" did not return old dir path'
[ "$(nvm_version_dir foo 2>&1)" = "unknown version dir" ] || die '"nvm_version_dir foo" did not error out'

View File

@ -0,0 +1,10 @@
#!/bin/sh
die () { echo $@ ; exit 1; }
. ../../../nvm.sh
nvm_version_greater 0.10.0 0.2.12 || die '"nvm_version_greater 0.10.0 0.2.12" did not return true'
! nvm_version_greater 0.10.0 0.20.12 || die '"nvm_version_greater 0.10.0 0.20.12" returned true'
! nvm_version_greater 0.10.0 0.10.0 || die '"nvm_version_greater" returned false for the same two versions'

View File

@ -4,6 +4,8 @@ die () { echo $@ ; exit 1; }
. ../../../nvm.sh . ../../../nvm.sh
[ "$(nvm_version_path)" = "$NVM_DIR" ] && [ "$(nvm_version_path foo)" = "$NVM_DIR/foo" ] || die '"nvm_version_path foo" did not return correct location'
[ "$(nvm_version_path foo)" = "$NVM_DIR/foo" ] [ "$(nvm_version_path 2>&1)" = "version is required" ] || die '"nvm_version_path" did not error out'
[ "$(nvm_version_path v0.11.0)" = "$NVM_DIR/v0.11.0" ] || die 'old version has the wrong path'
[ "$(nvm_version_path v0.12.0)" = "$NVM_DIR/versions/v0.12.0" ] || die 'new version has the wrong path'

View File

@ -9,5 +9,8 @@
for SRC in v* src alias; do for SRC in v* src alias; do
[ -e "$SRC" ] && mv "$SRC" bak [ -e "$SRC" ] && mv "$SRC" bak
done done
if [ -d versions ]; then
mv versions bak
fi
true true
) )