diff --git a/nvm.sh b/nvm.sh index bb3f2c7..49ce17b 100755 --- a/nvm.sh +++ b/nvm.sh @@ -21,14 +21,18 @@ fi # Auto detect the NVM_DIR if [ ! -d "$NVM_DIR" ]; then - export NVM_DIR=$(cd $NVM_CD_FLAGS $(dirname ${BASH_SOURCE[0]:-$0}) > /dev/null && pwd) + if [ -n "$BASH_SOURCE" ]; then + export NVM_DIR=$(cd $NVM_CD_FLAGS $(dirname ${BASH_SOURCE[0]:-$0}) > /dev/null && pwd) + else + export NVM_DIR=$HOME/.nvm + fi fi nvm_set_nullglob() { if has "setopt"; then # Zsh setopt NULL_GLOB - else + elif has "shopt"; then # Bash shopt -s nullglob fi @@ -36,8 +40,8 @@ nvm_set_nullglob() { # Obtain nvm version from rc file rc_nvm_version() { - if [ -e .nvmrc ]; then - RC_VERSION=`cat .nvmrc | head -n 1` + if [ -e ~/.nvmrc ]; then + RC_VERSION=`cat ~/.nvmrc | head -n 1` echo "Found .nvmrc files with version <$RC_VERSION>" fi } @@ -81,7 +85,7 @@ nvm_ls() { return fi # If it looks like an explicit version, don't do anything funny - if [[ "$PATTERN" == v?*.?*.?* ]]; then + if [ `expr "$PATTERN" : "v.*.\?.*.\?.*$"` != 0 ]; then VERSIONS="$PATTERN" else VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "v$PATTERN*" -exec basename '{}' ';' \ @@ -91,7 +95,7 @@ nvm_ls() { echo "N/A" return fi - echo "$VERSIONS" + printf "$VERSIONS\n" return } @@ -135,12 +139,12 @@ print_versions() { local PADDED_VERSION='' for VERSION in $1; do PADDED_VERSION=`printf '%10s' $VERSION` - if [[ -d "$NVM_DIR/$VERSION" ]]; then + if [ -d "$NVM_DIR/$VERSION" ]; then PADDED_VERSION="\033[0;34m$PADDED_VERSION\033[0m" fi OUTPUT="$OUTPUT\n$PADDED_VERSION" done - echo -e "$OUTPUT" + printf "$OUTPUT\n" } nvm() { @@ -317,10 +321,10 @@ nvm() { nvm use $VERSION if ! has "npm" ; then echo "Installing npm..." - if [[ "`expr match $VERSION '\(^v0\.1\.\)'`" != '' ]]; then + if [ "`expr match $VERSION '\(^v0\.1\.\)'`" != '' ]; then echo "npm requires node v0.2.3 or higher" - elif [[ "`expr match $VERSION '\(^v0\.2\.\)'`" != '' ]]; then - if [[ "`expr match $VERSION '\(^v0\.2\.[0-2]$\)'`" != '' ]]; then + elif [ "`expr match $VERSION '\(^v0\.2\.\)'`" != '' ]; then + if [ "`expr match $VERSION '\(^v0\.2\.[0-2]$\)'`" != '' ]; then echo "npm requires node v0.2.3 or higher" else curl https://npmjs.org/install.sh | clean=yes npm_install=0.2.19 sh @@ -336,7 +340,7 @@ nvm() { ;; "uninstall" ) [ $# -ne 2 ] && nvm help && return - if [[ $2 == `nvm_version` ]]; then + if [ $2 == `nvm_version` ]; then echo "nvm: Cannot uninstall currently-active node version, $2." return 1 fi @@ -364,14 +368,14 @@ nvm() { ;; "deactivate" ) - if [[ $PATH == *$NVM_DIR/*/bin* ]]; then + if [ `expr $PATH : ".*$NVM_DIR/.*/bin.*"` != 0 ] ; then export PATH=${PATH%$NVM_DIR/*/bin*}${PATH#*$NVM_DIR/*/bin:} hash -r echo "$NVM_DIR/*/bin removed from \$PATH" else echo "Could not find $NVM_DIR/*/bin in \$PATH" fi - if [[ $MANPATH == *$NVM_DIR/*/share/man* ]]; then + if [ `expr $MANPATH : ".*$NVM_DIR/.*/share/man.*"` != 0 ] ; then export MANPATH=${MANPATH%$NVM_DIR/*/share/man*}${MANPATH#*$NVM_DIR/*/share/man:} echo "$NVM_DIR/*/share/man removed from \$MANPATH" else @@ -402,7 +406,7 @@ nvm() { echo "$VERSION version is not installed yet" return 1 fi - if [[ $PATH == *$NVM_DIR/*/bin* ]]; then + if [ `expr $PATH : ".*$NVM_DIR/.*/bin"` != 0 ]; then PATH=${PATH%$NVM_DIR/*/bin*}$NVM_DIR/$VERSION/bin${PATH#*$NVM_DIR/*/bin} else PATH="$NVM_DIR/$VERSION/bin:$PATH" @@ -411,7 +415,7 @@ nvm() { MANPATH=$(manpath) fi MANPATH=${MANPATH#*$NVM_DIR/*/man:} - if [[ $MANPATH == *$NVM_DIR/*/share/man* ]]; then + if [ `expr $MANPATH : ".*$NVM_DIR/.*/share/man"` != 0 ]; then MANPATH=${MANPATH%$NVM_DIR/*/share/man*}$NVM_DIR/$VERSION/share/man${MANPATH#*$NVM_DIR/*/share/man} else MANPATH="$NVM_DIR/$VERSION/share/man:$MANPATH" @@ -440,7 +444,7 @@ nvm() { "ls" | "list" ) print_versions "`nvm_ls $2`" if [ $# -eq 1 ]; then - echo -ne "current: \t"; nvm_version current + printf "current: \t"; nvm_version current nvm alias fi return @@ -501,7 +505,7 @@ nvm() { # declare local INSTALLS first, otherwise it doesn't work in zsh local INSTALLS - INSTALLS=( `nvm use $VERSION > /dev/null && npm -g -p ll | \grep "$ROOT\/[^/]\+$" | cut -d '/' -f $(($ROOTDEPTH + 2)) | cut -d ":" -f 2 | \grep -v npm | tr "\n" " "` ) + INSTALLS=`nvm use $VERSION > /dev/null && npm -g -p ll | \grep "$ROOT\/[^/]\+$" | cut -d '/' -f $(($ROOTDEPTH + 2)) | cut -d ":" -f 2 | \grep -v npm | tr "\n" " "` npm install -g ${INSTALLS[@]} ;; @@ -518,4 +522,4 @@ nvm() { esac } -nvm ls default &>/dev/null && nvm use default >/dev/null || true +nvm ls default >/dev/null && nvm use default >/dev/null || true diff --git "a/test/fast/Listing versions/Running \"nvm ls\" should display all installed versions." "b/test/fast/Listing versions/Running \"nvm ls\" should display all installed versions." index dfba0da..5c7d724 100755 --- "a/test/fast/Listing versions/Running \"nvm ls\" should display all installed versions." +++ "b/test/fast/Listing versions/Running \"nvm ls\" should display all installed versions." @@ -2,13 +2,9 @@ . ../../../nvm.sh -mkdir ../../../v0.0.{1,3,9} -mkdir ../../../v0.3.{1,3,9} +mkdir ../../../v0.1 +mkdir ../../../v0.3 # The result should contain the version numbers. -nvm ls | grep v0.0.1 && -nvm ls | grep v0.0.3 && -nvm ls | grep v0.0.9 && -nvm ls | grep v0.3.1 && -nvm ls | grep v0.3.3 && -nvm ls | grep v0.3.9 +nvm ls | grep v0.1 && +nvm ls | grep v0.3 diff --git "a/test/fast/Running \"nvm current\" should display current nvm environment." "b/test/fast/Running \"nvm current\" should display current nvm environment." index 09b8931..a2c93a5 100755 --- "a/test/fast/Running \"nvm current\" should display current nvm environment." +++ "b/test/fast/Running \"nvm current\" should display current nvm environment." @@ -3,4 +3,4 @@ die () { echo $@ ; exit 1; } . ../../nvm.sh -[[ $(nvm current) == *"current"* ]] || die "Failed to find current version" +[ `expr "$(nvm current)" : ".*current"` != 0 ] || die "Failed to find current version" diff --git "a/test/fast/Running \"nvm deactivate\" should unset the nvm environment variables." "b/test/fast/Running \"nvm deactivate\" should unset the nvm environment variables." index 0f2ad3b..813f226 100755 --- "a/test/fast/Running \"nvm deactivate\" should unset the nvm environment variables." +++ "b/test/fast/Running \"nvm deactivate\" should unset the nvm environment variables." @@ -4,11 +4,11 @@ mkdir -p ../../v0.2.3 die () { echo $@ ; exit 1; } -[[ $PATH != *v0.2.3/*/bin* ]] || echo "WARNING: Unexpectedly found v0.2.3 already active" >&2 +[ `expr $PATH : ".*v0.2.3/.*/bin"` == 0 ] || echo "WARNING: Unexpectedly found v0.2.3 already active" >&2 . ../../nvm.sh nvm use v0.2.3 && -[[ $PATH == *v0.2.3/*/bin* ]] || die "Failed to activate v0.2.3" +[ `expr $PATH : ".*v0.2.3/.*/bin"` != 0 ] || die "Failed to activate v0.2.3" nvm deactivate && -[[ $PATH != *v0.2.3/*/bin* ]] || die "Failed to deactivate v0.2.3" +[ `expr $PATH : ".*v0.2.3/.*/bin"` != 0 ] || die "Failed to deactivate v0.2.3"