From 0b97ee1d2a3ec36ceea8c28b8583d47d508ba181 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Tue, 18 Mar 2014 00:53:04 +0100 Subject: [PATCH 1/2] Failing test for when using nvm ls with v* version --- ...unning \"nvm ls 0.2\" should display only 0.2.x versions." | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git "a/test/fast/Listing versions/Running \"nvm ls 0.2\" should display only 0.2.x versions." "b/test/fast/Listing versions/Running \"nvm ls 0.2\" should display only 0.2.x versions." index 5a3d450..dda41ba 100755 --- "a/test/fast/Listing versions/Running \"nvm ls 0.2\" should display only 0.2.x versions." +++ "b/test/fast/Listing versions/Running \"nvm ls 0.2\" should display only 0.2.x versions." @@ -7,4 +7,6 @@ mkdir ../../../v0.2.3 # The result should contain only the appropriate version numbers. nvm ls 0.2 | grep v0.2.3 && -nvm ls 0.1 | grep -v v0.2.3 +nvm ls 0.1 | grep -v v0.2.3 && +nvm ls 0.1 | grep v0.1.3 && +nvm ls v0.2 | grep v0.2.3 From a34c080d77cacb94f8a5435efb8a564e8ab9ac49 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Sun, 21 Oct 2012 22:57:32 +0200 Subject: [PATCH 2/2] Fix nvm ls for v* numbers Previously calling nvm ls with v* number (nvm ls v0.2) would not return any results. So now the v is prepended if its not already there --- nvm.sh | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/nvm.sh b/nvm.sh index 4d6bbca..28f4a26 100644 --- a/nvm.sh +++ b/nvm.sh @@ -67,6 +67,21 @@ nvm_remote_version() { fi } +nvm_normalize_version() { + echo "$1" | sed -e 's/^v//' | awk -F. '{ printf("%d%03d%03d\n", $1,$2,$3); }' +} + +nvm_format_version() { + echo "$1" | sed -e 's/^\([0-9]\)/v\1/g' +} + +nvm_binary_available() { + # binaries started with node 0.8.6 + local MINIMAL="0.8.6" + local VERSION=$1 + [ $(nvm_normalize_version $VERSION) -ge $(nvm_normalize_version $MINIMAL) ] +} + nvm_ls() { local PATTERN=$1 local VERSIONS='' @@ -83,7 +98,7 @@ nvm_ls() { if [ `expr "$PATTERN" : "v[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*$"` != 0 ]; then VERSIONS="$PATTERN" else - VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "v$PATTERN*" -exec basename '{}' ';' \ + VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "$(nvm_format_version $PATTERN)*" -exec basename '{}' ';' \ | sort -t. -u -k 1.2,1n -k 2,2n -k 3,3n` fi if [ -z "$VERSIONS" ]; then @@ -99,9 +114,7 @@ nvm_ls_remote() { local VERSIONS local GREP_OPTIONS='' if [ -n "$PATTERN" ]; then - if echo "${PATTERN}" | \grep -v '^v' ; then - PATTERN=v$PATTERN - fi + PATTERN=`nvm_format_version "$PATTERN"` else PATTERN=".*" fi @@ -262,14 +275,7 @@ nvm() { if [ $nobinary -ne 1 ]; then # shortcut - try the binary if possible. if [ -n "$os" ]; then - binavail= - # binaries started with node 0.8.6 - case "$VERSION" in - v0.8.[012345]) binavail=0 ;; - v0.[1234567].*) binavail=0 ;; - *) binavail=1 ;; - esac - if [ $binavail -eq 1 ]; then + if nvm_binary_available "$VERSION"; then t="$VERSION-$os-$arch" url="$NVM_NODEJS_ORG_MIRROR/$VERSION/node-${t}.tar.gz" sum=`curl -s $NVM_NODEJS_ORG_MIRROR/$VERSION/SHASUMS.txt | \grep node-${t}.tar.gz | awk '{print $1}'` @@ -346,12 +352,13 @@ nvm() { ;; "uninstall" ) [ $# -ne 2 ] && nvm help && return - if [ "$2" = `nvm_version` ]; then - echo "nvm: Cannot uninstall currently-active node version, $2." + PATTERN=`nvm_format_version $2` + if [[ $PATTERN == `nvm_version` ]]; then + echo "nvm: Cannot uninstall currently-active node version, $PATTERN." return 1 fi - VERSION=`nvm_version $2` - if [ ! -d "$NVM_DIR/$VERSION" ]; then + VERSION=`nvm_version $PATTERN` + if [ ! -d $NVM_DIR/$VERSION ]; then echo "$VERSION version is not installed..." return; fi