From f1b484ee875988b5af5b8b5b08f4f59149deff25 Mon Sep 17 00:00:00 2001 From: Emanuele Aina Date: Thu, 11 Apr 2013 19:56:18 +0200 Subject: [PATCH 1/2] Silence stderr warning on `nvm uninstall' with no configured alias --- nvm.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nvm.sh b/nvm.sh index c577cf7..3ad9e84 100755 --- a/nvm.sh +++ b/nvm.sh @@ -339,7 +339,7 @@ nvm() echo "Uninstalled node $VERSION" # Rm any aliases that point to uninstalled version. - for A in `grep -l $VERSION $NVM_DIR/alias/*` + for A in `grep -l $VERSION $NVM_DIR/alias/* 2>/dev/null` do nvm unalias `basename $A` done From 42915fc21c41cbf5c855a450f707361b8b2d1338 Mon Sep 17 00:00:00 2001 From: Emanuele Aina Date: Thu, 11 Apr 2013 19:57:56 +0200 Subject: [PATCH 2/2] Reduce usage of `cd' to avoid issues with shell aliases Since we run in interactive context the builtin `cd' may have been aliased to print some additional info when invoked, thus it's safer for us to avoid parsing the output from subshells which calls it. For instance in .bash_profile I have the following function to redefine `cd' such that it will list the destination directory contents: cd () { builtin cd "$@" && ls -F --color=auto } --- nvm.sh | 66 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 37 insertions(+), 29 deletions(-) diff --git a/nvm.sh b/nvm.sh index 3ad9e84..49ed0e9 100755 --- a/nvm.sh +++ b/nvm.sh @@ -7,7 +7,7 @@ # Auto detect the NVM_DIR if [ ! -d "$NVM_DIR" ]; then - export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}) && pwd) + export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}) > /dev/null && pwd) fi # Make zsh glob matching behave same as bash @@ -16,6 +16,16 @@ if [ ! -z "$(which unsetopt 2>/dev/null)" ]; then unsetopt nomatch 2>/dev/null fi +function nvm_set_nullglob { + if type setopt > /dev/null 2>&1; then + # Zsh + setopt NULL_GLOB + else + # Bash + shopt -s nullglob + fi +} + # Obtain nvm version from rc file function rc_nvm_version { if [ -e .nvmrc ]; then @@ -69,7 +79,7 @@ nvm_ls() if [[ "$PATTERN" == v?*.?*.?* ]]; then VERSIONS="$PATTERN" else - VERSIONS=`(cd $NVM_DIR; \ls -d v${PATTERN}* 2>/dev/null) | sort -t. -k 1.2,1n -k 2,2n -k 3,3n` + VERSIONS=`nvm_set_nullglob; basename -a $NVM_DIR/v${PATTERN}* 2>/dev/null | sort -t. -k 1.2,1n -k 2,2n -k 3,3n` fi if [ ! "$VERSIONS" ]; then echo "N/A" @@ -244,21 +254,22 @@ nvm() t="$VERSION-$os-$arch" url="http://nodejs.org/dist/$VERSION/node-${t}.tar.gz" sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | grep node-${t}.tar.gz | awk '{print $1}'` + local tmpdir="$NVM_DIR/bin/node-${t}" + local tmptarball="$tmpdir/node-${t}.tar.gz" if ( - mkdir -p "$NVM_DIR/bin/node-${t}" && \ - cd "$NVM_DIR/bin" && \ - curl -C - --progress-bar $url -o "node-${t}.tar.gz" && \ - nvm_checksum `${shasum} node-${t}.tar.gz | awk '{print $1}'` $sum && \ - tar -xzf "node-${t}.tar.gz" -C "node-${t}" --strip-components 1 && \ - mv "node-${t}" "../$VERSION" && \ - rm -f "node-${t}.tar.gz" + mkdir -p "$tmpdir" && \ + curl -C - --progress-bar $url -o "$tmptarball" && \ + nvm_checksum `${shasum} "$tmptarball" | awk '{print $1}'` $sum && \ + tar -xzf "$tmptarball" -C "$tmpdir" --strip-components 1 && \ + mv "$tmpdir" "$NVM_DIR/$VERSION" && \ + rm -f "$tmptarball" ) then nvm use $VERSION return; else echo "Binary download failed, trying source." >&2 - cd "$NVM_DIR/bin" && rm -rf "node-${t}.tar.gz" "node-${t}" + rm -rf "$tmptarball" "$tmpdir" fi fi fi @@ -272,6 +283,8 @@ nvm() if [ "$os" = "freebsd" ]; then make='gmake' fi + local tmpdir="$NVM_DIR/src" + local tmptarball="$tmpdir/node-$VERSION.tar.gz" if [ "`curl -Is "http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" | grep '200 OK'`" != '' ]; then tarball="http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" sum=`curl -s http://nodejs.org/dist/$VERSION/SHASUMS.txt | grep node-$VERSION.tar.gz | awk '{print $1}'` @@ -280,12 +293,11 @@ nvm() fi if ( [ ! -z $tarball ] && \ - mkdir -p "$NVM_DIR/src" && \ - cd "$NVM_DIR/src" && \ - curl --progress-bar $tarball -o "node-$VERSION.tar.gz" && \ - if [ "$sum" = "" ]; then : ; else nvm_checksum `${shasum} node-$VERSION.tar.gz | awk '{print $1}'` $sum; fi && \ - tar -xzf "node-$VERSION.tar.gz" && \ - cd "node-$VERSION" && \ + mkdir -p "$tmpdir" && \ + curl --progress-bar $tarball -o "$tmptarball" && \ + if [ "$sum" = "" ]; then : ; else nvm_checksum `${shasum} "$tmptarball" | awk '{print $1}'` $sum; fi && \ + tar -xzf "$tmptarball" -C "$tmpdir" && \ + cd "$tmpdir/node-$VERSION" && \ ./configure --prefix="$NVM_DIR/$VERSION" $ADDITIONAL_PARAMETERS && \ $make && \ rm -f "$NVM_DIR/$VERSION" 2>/dev/null && \ @@ -327,15 +339,11 @@ nvm() t="$VERSION-$os-$arch" # Delete all files related to target version. - (mkdir -p "$NVM_DIR/src" && \ - cd "$NVM_DIR/src" && \ - rm -rf "node-$VERSION" 2>/dev/null && \ - rm -f "node-$VERSION.tar.gz" 2>/dev/null && \ - mkdir -p "$NVM_DIR/bin" && \ - cd "$NVM_DIR/bin" && \ - rm -rf "node-${t}" 2>/dev/null && \ - rm -f "node-${t}.tar.gz" 2>/dev/null && \ - rm -rf "$NVM_DIR/$VERSION" 2>/dev/null) + rm -rf "$NVM_DIR/src/node-$VERSION" \ + "$NVM_DIR/src/node-$VERSION.tar.gz" \ + "$NVM_DIR/bin/node-${t}" \ + "$NVM_DIR/bin/node-${t}.tar.gz" \ + "$NVM_DIR/$VERSION" 2>/dev/null echo "Uninstalled node $VERSION" # Rm any aliases that point to uninstalled version. @@ -430,15 +438,15 @@ nvm() "alias" ) mkdir -p $NVM_DIR/alias if [ $# -le 2 ]; then - (cd $NVM_DIR/alias && for ALIAS in `\ls $2* 2>/dev/null`; do + for ALIAS in $(nvm_set_nullglob; echo $NVM_DIR/alias/$2* ); do DEST=`cat $ALIAS` VERSION=`nvm_version $DEST` if [ "$DEST" = "$VERSION" ]; then - echo "$ALIAS -> $DEST" + echo "$(basename $ALIAS) -> $DEST" else - echo "$ALIAS -> $DEST (-> $VERSION)" + echo "$(basename $ALIAS) -> $DEST (-> $VERSION)" fi - done) + done return fi if [ ! "$3" ]; then