From 66455f7c77f327cfdeb86cd7a696d0e8e84d885d Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 28 Apr 2014 14:48:36 -0700 Subject: [PATCH 1/7] Refactor so "finding .nvmrc" can be done in a bash function. --- nvm.sh | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/nvm.sh b/nvm.sh index 7d2bb1e..8b23a12 100644 --- a/nvm.sh +++ b/nvm.sh @@ -34,11 +34,18 @@ if [ -z "$NVM_NODEJS_ORG_MIRROR" ]; then export NVM_NODEJS_ORG_MIRROR="http://nodejs.org/dist" fi +nvm_find_nvmrc() { + if [ -e '.nvmrc' ]; then + echo '.nvmrc' + fi +} + # Obtain nvm version from rc file nvm_rc_version() { - if [ -e .nvmrc ]; then - NVM_RC_VERSION=`cat .nvmrc | head -n 1` - echo "Found .nvmrc files with version <$NVM_RC_VERSION>" + local NVMRC_PATH=$(nvm_find_nvmrc) + if [ -e "$NVMRC_PATH" ]; then + NVM_RC_VERSION=`cat "$NVMRC_PATH" | head -n 1` + echo "Found $NVMRC_PATH with version <$NVM_RC_VERSION>" fi } From 038c1f3d0f4791b1bb48a56691a39f0153df01d7 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Mon, 28 Apr 2014 23:27:10 -0700 Subject: [PATCH 2/7] Find `.nvmrc` files upwards. --- nvm.sh | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/nvm.sh b/nvm.sh index 8b23a12..6f0b955 100644 --- a/nvm.sh +++ b/nvm.sh @@ -35,9 +35,13 @@ if [ -z "$NVM_NODEJS_ORG_MIRROR" ]; then fi nvm_find_nvmrc() { - if [ -e '.nvmrc' ]; then - echo '.nvmrc' - fi + typeset dir="$PWD" + typeset found="" + while [ "$dir" != "/" ] && [ "$found" = "" ]; do + found=$(find "$dir" -maxdepth 1 -name ".nvmrc") + dir=$(cd "$dir/.." && pwd -P) + done + echo $found } # Obtain nvm version from rc file From 80e349edb14869eb29a4b5bb581402eb98ba7142 Mon Sep 17 00:00:00 2001 From: Koen Punt Date: Wed, 30 Apr 2014 10:31:51 +0200 Subject: [PATCH 3/7] locate .nvmrc without `find` instead of using find and actually cd'ing into directories we're now using simple string replacement on the `pwd` --- nvm.sh | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/nvm.sh b/nvm.sh index 6f0b955..1523b80 100644 --- a/nvm.sh +++ b/nvm.sh @@ -34,14 +34,22 @@ if [ -z "$NVM_NODEJS_ORG_MIRROR" ]; then export NVM_NODEJS_ORG_MIRROR="http://nodejs.org/dist" fi -nvm_find_nvmrc() { - typeset dir="$PWD" - typeset found="" - while [ "$dir" != "/" ] && [ "$found" = "" ]; do - found=$(find "$dir" -maxdepth 1 -name ".nvmrc") - dir=$(cd "$dir/.." && pwd -P) +# Traverse up in directory tree to find containing folder +nvm_find_up() { + local path + path=$(pwd) + while [[ "$path" != "" && ! -e "$path/$1" ]]; do + path=${path%/*} done - echo $found + echo "$path" +} + + +nvm_find_nvmrc() { + local dir=$(nvm_find_up '.nvmrc') + if [ -e "$dir/.nvmrc" ]; then + echo "$dir/.nvmrc" + fi } # Obtain nvm version from rc file From 87516039a83141b4374d9072ff7ea46b5c2e8062 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Fri, 2 May 2014 23:28:42 -0700 Subject: [PATCH 4/7] Using portable conditional syntax. --- nvm.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nvm.sh b/nvm.sh index 1523b80..4af2f5b 100644 --- a/nvm.sh +++ b/nvm.sh @@ -37,8 +37,8 @@ fi # Traverse up in directory tree to find containing folder nvm_find_up() { local path - path=$(pwd) - while [[ "$path" != "" && ! -e "$path/$1" ]]; do + path=$PWD + while [ "$path" != "" ] && [ ! -f "$path/$1" ]; do path=${path%/*} done echo "$path" From e195fccdb7ceac31ecc9c17324ce028bc9a96ccf Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 3 May 2014 23:29:21 -0700 Subject: [PATCH 5/7] Escaping backticks --- .../Running \"nvm run\" should pick up .nvmrc version" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/test/slow/nvm run/Running \"nvm run\" should pick up .nvmrc version" "b/test/slow/nvm run/Running \"nvm run\" should pick up .nvmrc version" index 181e3fc..58151ef 100755 --- "a/test/slow/nvm run/Running \"nvm run\" should pick up .nvmrc version" +++ "b/test/slow/nvm run/Running \"nvm run\" should pick up .nvmrc version" @@ -6,7 +6,7 @@ die () { echo $@ ; exit 1; } . ../../../nvm.sh echo "0.10.7" > .nvmrc -[ "$(nvm run --version | tail -1)" = "v0.10.7" ] || die "`nvm run` failed to run with the .nvmrc version" +[ "$(nvm run --version | tail -1)" = "v0.10.7" ] || die "\`nvm run\` failed to run with the .nvmrc version" -[ "$(nvm run --version | head -1)" = "Found .nvmrc files with version <0.10.7>" ] || die "`nvm run` failed to print out the \"found in .nvmrc\" message" +[ "$(nvm run --version | head -1)" = "Found .nvmrc files with version <0.10.7>" ] || die "\`nvm run\` failed to print out the \"found in .nvmrc\" message" From c77be55cdc18a0f0c45a2f0967f9a401a4c0967c Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 4 May 2014 00:39:32 -0700 Subject: [PATCH 6/7] Updating "nvm run" tests. --- .../nvm run/Running \"nvm run\" should pick up .nvmrc version" | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git "a/test/slow/nvm run/Running \"nvm run\" should pick up .nvmrc version" "b/test/slow/nvm run/Running \"nvm run\" should pick up .nvmrc version" index 58151ef..d462488 100755 --- "a/test/slow/nvm run/Running \"nvm run\" should pick up .nvmrc version" +++ "b/test/slow/nvm run/Running \"nvm run\" should pick up .nvmrc version" @@ -6,7 +6,8 @@ die () { echo $@ ; exit 1; } . ../../../nvm.sh echo "0.10.7" > .nvmrc + [ "$(nvm run --version | tail -1)" = "v0.10.7" ] || die "\`nvm run\` failed to run with the .nvmrc version" -[ "$(nvm run --version | head -1)" = "Found .nvmrc files with version <0.10.7>" ] || die "\`nvm run\` failed to print out the \"found in .nvmrc\" message" +[ "$(nvm run --version | head -1)" = "Found '$PWD/.nvmrc' with version <0.10.7>" ] || die "\`nvm run\` failed to print out the \"found in .nvmrc\" message" From 4a7275a0c60a5633b3924577ba5d048febd008b2 Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sun, 4 May 2014 00:39:46 -0700 Subject: [PATCH 7/7] Ensuring paths are in quotes, to preserve spaces. --- nvm.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/nvm.sh b/nvm.sh index 4af2f5b..4ed0926 100644 --- a/nvm.sh +++ b/nvm.sh @@ -46,7 +46,7 @@ nvm_find_up() { nvm_find_nvmrc() { - local dir=$(nvm_find_up '.nvmrc') + local dir="$(nvm_find_up '.nvmrc')" if [ -e "$dir/.nvmrc" ]; then echo "$dir/.nvmrc" fi @@ -54,10 +54,10 @@ nvm_find_nvmrc() { # Obtain nvm version from rc file nvm_rc_version() { - local NVMRC_PATH=$(nvm_find_nvmrc) + local NVMRC_PATH="$(nvm_find_nvmrc)" if [ -e "$NVMRC_PATH" ]; then NVM_RC_VERSION=`cat "$NVMRC_PATH" | head -n 1` - echo "Found $NVMRC_PATH with version <$NVM_RC_VERSION>" + echo "Found '$NVMRC_PATH' with version <$NVM_RC_VERSION>" fi }