Merge pull request #364 from frasertweedale/fix/portability

Fix/portability
master
Jordan Harband 2014-03-04 17:31:17 -08:00
commit 0d44ed1126
9 changed files with 64 additions and 68 deletions

View File

@ -5,11 +5,11 @@ function fatalExit (){
} }
# an alternative URL that could be used: https://github.com/creationix/nvm/tarball/master # an alternative URL that could be used: https://github.com/creationix/nvm/tarball/master
if [ "$NVM_SOURCE" == "" ]; then if [ "$NVM_SOURCE" = "" ]; then
NVM_SOURCE="https://raw.github.com/creationix/nvm/master/nvm.sh" NVM_SOURCE="https://raw.github.com/creationix/nvm/master/nvm.sh"
fi fi
if [ "$NVM_DIR" == "" ]; then if [ "$NVM_DIR" = "" ]; then
NVM_DIR="$HOME/.nvm" NVM_DIR="$HOME/.nvm"
fi fi

69
nvm.sh
View File

@ -1,5 +1,3 @@
#!/bin/sh
# Node Version Manager # Node Version Manager
# Implemented as a bash function # Implemented as a bash function
# To use source this file from your bash profile # To use source this file from your bash profile
@ -21,24 +19,21 @@ fi
# Auto detect the NVM_DIR # Auto detect the NVM_DIR
if [ ! -d "$NVM_DIR" ]; then if [ ! -d "$NVM_DIR" ]; then
if [ -n "$BASH_SOURCE" ]; then
export NVM_DIR=$(cd $NVM_CD_FLAGS $(dirname ${BASH_SOURCE[0]:-$0}) > /dev/null && pwd) export NVM_DIR=$(cd $NVM_CD_FLAGS $(dirname ${BASH_SOURCE[0]:-$0}) > /dev/null && pwd)
else
export NVM_DIR=$HOME/.nvm
fi fi
fi
DASH_E=""
[ -z "$(echo -n -e)" ] && DASH_E="-e"
# Setup mirror location if not already set # Setup mirror location if not already set
if [ -z "$NVM_NODEJS_ORG_MIRROR" ]; then if [ -z "$NVM_NODEJS_ORG_MIRROR" ]; then
export NVM_NODEJS_ORG_MIRROR="http://nodejs.org/dist" export NVM_NODEJS_ORG_MIRROR="http://nodejs.org/dist"
fi fi
nvm_set_nullglob() {
if has "setopt"; then
# Zsh
setopt NULL_GLOB
else
# Bash
shopt -s nullglob
fi
}
# Obtain nvm version from rc file # Obtain nvm version from rc file
rc_nvm_version() { rc_nvm_version() {
if [ -e .nvmrc ]; then if [ -e .nvmrc ]; then
@ -88,7 +83,7 @@ nvm_ls() {
return return
fi fi
# If it looks like an explicit version, don't do anything funny # If it looks like an explicit version, don't do anything funny
if [[ "$PATTERN" == v?*.?*.?* ]]; then if [ `expr "$PATTERN" : "v[[:digit:]]*\.[[:digit:]]*\.[[:digit:]]*$"` != 0 ]; then
VERSIONS="$PATTERN" VERSIONS="$PATTERN"
else else
VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "v$PATTERN*" -exec basename '{}' ';' \ VERSIONS=`find "$NVM_DIR/" -maxdepth 1 -type d -name "v$PATTERN*" -exec basename '{}' ';' \
@ -145,8 +140,11 @@ nvm_checksum() {
} }
colorize_version() { colorize_version() {
local VERSION=$1 if [ -n "$DASH_E" ]; then
echo -e "\033[0;34m$VERSION\033[0m" echo $DASH_E "\033[0;34m$1\033[0m"
else
echo $1
fi
} }
print_versions() { print_versions() {
@ -154,7 +152,7 @@ print_versions() {
local PADDED_VERSION local PADDED_VERSION
for VERSION in $1; do for VERSION in $1; do
PADDED_VERSION=`printf '%10s' $VERSION` PADDED_VERSION=`printf '%10s' $VERSION`
if [[ -d "$NVM_DIR/$VERSION" ]]; then if [ -d "$NVM_DIR/$VERSION" ]; then
colorize_version "$PADDED_VERSION" colorize_version "$PADDED_VERSION"
else else
echo "$PADDED_VERSION" echo "$PADDED_VERSION"
@ -309,6 +307,7 @@ nvm() {
make='make' make='make'
if [ "$os" = "freebsd" ]; then if [ "$os" = "freebsd" ]; then
make='gmake' make='gmake'
MAKE_CXX="CXX=c++"
fi fi
local tmpdir="$NVM_DIR/src" local tmpdir="$NVM_DIR/src"
local tmptarball="$tmpdir/node-$VERSION.tar.gz" local tmptarball="$tmpdir/node-$VERSION.tar.gz"
@ -326,18 +325,18 @@ nvm() {
tar -xzf "$tmptarball" -C "$tmpdir" && \ tar -xzf "$tmptarball" -C "$tmpdir" && \
cd "$tmpdir/node-$VERSION" && \ cd "$tmpdir/node-$VERSION" && \
./configure --prefix="$NVM_DIR/$VERSION" $ADDITIONAL_PARAMETERS && \ ./configure --prefix="$NVM_DIR/$VERSION" $ADDITIONAL_PARAMETERS && \
$make && \ $make $MAKE_CXX && \
rm -f "$NVM_DIR/$VERSION" 2>/dev/null && \ rm -f "$NVM_DIR/$VERSION" 2>/dev/null && \
$make install $make $MAKE_CXX install
) )
then then
nvm use $VERSION nvm use $VERSION
if ! has "npm" ; then if ! has "npm" ; then
echo "Installing npm..." echo "Installing npm..."
if [[ "`expr match $VERSION '\(^v0\.1\.\)'`" != '' ]]; then if [ "`expr "$VERSION" : '\(^v0\.1\.\)'`" != '' ]; then
echo "npm requires node v0.2.3 or higher" echo "npm requires node v0.2.3 or higher"
elif [[ "`expr match $VERSION '\(^v0\.2\.\)'`" != '' ]]; then elif [ "`expr "$VERSION" : '\(^v0\.2\.\)'`" != '' ]; then
if [[ "`expr match $VERSION '\(^v0\.2\.[0-2]$\)'`" != '' ]]; then if [ "`expr "$VERSION" : '\(^v0\.2\.[0-2]$\)'`" != '' ]; then
echo "npm requires node v0.2.3 or higher" echo "npm requires node v0.2.3 or higher"
else else
curl https://npmjs.org/install.sh | clean=yes npm_install=0.2.19 sh curl https://npmjs.org/install.sh | clean=yes npm_install=0.2.19 sh
@ -353,7 +352,7 @@ nvm() {
;; ;;
"uninstall" ) "uninstall" )
[ $# -ne 2 ] && nvm help && return [ $# -ne 2 ] && nvm help && return
if [[ $2 == `nvm_version` ]]; then if [ "$2" = `nvm_version` ]; then
echo "nvm: Cannot uninstall currently-active node version, $2." echo "nvm: Cannot uninstall currently-active node version, $2."
return 1 return 1
fi fi
@ -381,20 +380,20 @@ nvm() {
;; ;;
"deactivate" ) "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:} export PATH=${PATH%$NVM_DIR/*/bin*}${PATH#*$NVM_DIR/*/bin:}
hash -r hash -r
echo "$NVM_DIR/*/bin removed from \$PATH" echo "$NVM_DIR/*/bin removed from \$PATH"
else else
echo "Could not find $NVM_DIR/*/bin in \$PATH" echo "Could not find $NVM_DIR/*/bin in \$PATH"
fi 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:} export MANPATH=${MANPATH%$NVM_DIR/*/share/man*}${MANPATH#*$NVM_DIR/*/share/man:}
echo "$NVM_DIR/*/share/man removed from \$MANPATH" echo "$NVM_DIR/*/share/man removed from \$MANPATH"
else else
echo "Could not find $NVM_DIR/*/share/man in \$MANPATH" echo "Could not find $NVM_DIR/*/share/man in \$MANPATH"
fi fi
if [[ $NODE_PATH == *$NVM_DIR/*/lib/node_modules* ]]; then if [ `expr "$NODE_PATH" : ".*$NVM_DIR/.*/lib/node_modules.*"` != 0 ] ; then
export NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}${NODE_PATH#*$NVM_DIR/*/lib/node_modules:} export NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}${NODE_PATH#*$NVM_DIR/*/lib/node_modules:}
echo "$NVM_DIR/*/lib/node_modules removed from \$NODE_PATH" echo "$NVM_DIR/*/lib/node_modules removed from \$NODE_PATH"
else else
@ -425,7 +424,7 @@ nvm() {
echo "$VERSION version is not installed yet" echo "$VERSION version is not installed yet"
return 1 return 1
fi 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} PATH=${PATH%$NVM_DIR/*/bin*}$NVM_DIR/$VERSION/bin${PATH#*$NVM_DIR/*/bin}
else else
PATH="$NVM_DIR/$VERSION/bin:$PATH" PATH="$NVM_DIR/$VERSION/bin:$PATH"
@ -434,12 +433,12 @@ nvm() {
MANPATH=$(manpath) MANPATH=$(manpath)
fi fi
MANPATH=${MANPATH#*$NVM_DIR/*/man:} 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} MANPATH=${MANPATH%$NVM_DIR/*/share/man*}$NVM_DIR/$VERSION/share/man${MANPATH#*$NVM_DIR/*/share/man}
else else
MANPATH="$NVM_DIR/$VERSION/share/man:$MANPATH" MANPATH="$NVM_DIR/$VERSION/share/man:$MANPATH"
fi fi
if [[ $NODE_PATH == *$NVM_DIR/*/lib/node_modules* ]]; then if [ `expr "$NODE_PATH" : ".*$NVM_DIR/.*/lib/node_modules.*"` != 0 ]; then
NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}$NVM_DIR/$VERSION/lib/node_modules${NODE_PATH#*$NVM_DIR/*/lib/node_modules} NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}$NVM_DIR/$VERSION/lib/node_modules${NODE_PATH#*$NVM_DIR/*/lib/node_modules}
else else
NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH" NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH"
@ -463,7 +462,7 @@ nvm() {
echo "$VERSION version is not installed yet" echo "$VERSION version is not installed yet"
return; return;
fi fi
if [[ $NODE_PATH == *$NVM_DIR/*/lib/node_modules* ]]; then if [ `expr "$NODE_PATH" : ".*$NVM_DIR/.*/lib/node_modules.*"` != 0 ]; then
RUN_NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}$NVM_DIR/$VERSION/lib/node_modules${NODE_PATH#*$NVM_DIR/*/lib/node_modules} RUN_NODE_PATH=${NODE_PATH%$NVM_DIR/*/lib/node_modules*}$NVM_DIR/$VERSION/lib/node_modules${NODE_PATH#*$NVM_DIR/*/lib/node_modules}
else else
RUN_NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH" RUN_NODE_PATH="$NVM_DIR/$VERSION/lib/node_modules:$NODE_PATH"
@ -474,7 +473,7 @@ nvm() {
"ls" | "list" ) "ls" | "list" )
print_versions "`nvm_ls $2`" print_versions "`nvm_ls $2`"
if [ $# -eq 1 ]; then if [ $# -eq 1 ]; then
echo -ne "current: \t"; nvm_version current printf "current: \t"; nvm_version current
nvm alias nvm alias
fi fi
return return
@ -484,13 +483,14 @@ nvm() {
return return
;; ;;
"current" ) "current" )
echo -ne "current: \t"; nvm_version current echo $DASH_E -n "current: \t"; nvm_version current
;; ;;
"alias" ) "alias" )
mkdir -p $NVM_DIR/alias mkdir -p $NVM_DIR/alias
if [ $# -le 2 ]; then if [ $# -le 2 ]; then
local DEST local DEST
for ALIAS in $(nvm_set_nullglob; echo $NVM_DIR/alias/$2* ); do for ALIAS in $NVM_DIR/alias/$2*; do
if [ -e "$ALIAS" ]; then
DEST=`cat $ALIAS` DEST=`cat $ALIAS`
VERSION=`nvm_version $DEST` VERSION=`nvm_version $DEST`
if [ "$DEST" = "$VERSION" ]; then if [ "$DEST" = "$VERSION" ]; then
@ -498,6 +498,7 @@ nvm() {
else else
echo "$(basename $ALIAS) -> $DEST (-> $VERSION)" echo "$(basename $ALIAS) -> $DEST (-> $VERSION)"
fi fi
fi
done done
return return
fi fi
@ -536,7 +537,7 @@ nvm() {
# declare local INSTALLS first, otherwise it doesn't work in zsh # declare local INSTALLS first, otherwise it doesn't work in zsh
local INSTALLS 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[@]} npm install -g ${INSTALLS[@]}
;; ;;
@ -553,5 +554,5 @@ nvm() {
esac esac
} }
nvm ls default &>/dev/null && nvm use default >/dev/null || true nvm ls default >/dev/null && nvm use default >/dev/null || true

View File

@ -2,8 +2,12 @@
. ../../../nvm.sh . ../../../nvm.sh
mkdir ../../../v0.0.{1,3,9} mkdir ../../../v0.0.1
mkdir ../../../v0.3.{1,3,9} mkdir ../../../v0.0.3
mkdir ../../../v0.0.9
mkdir ../../../v0.3.1
mkdir ../../../v0.3.3
mkdir ../../../v0.3.9
# The result should contain the version numbers. # The result should contain the version numbers.
nvm ls | grep v0.0.1 && nvm ls | grep v0.0.1 &&

View File

@ -3,4 +3,4 @@
die () { echo $@ ; exit 1; } die () { echo $@ ; exit 1; }
. ../../nvm.sh . ../../nvm.sh
[[ $(nvm current) == *"current"* ]] || die "Failed to find current version" [ `expr "$(nvm current)" : ".*current"` != 0 ] || die "Failed to find current version"

View File

@ -4,11 +4,11 @@ mkdir -p ../../v0.2.3
die () { echo $@ ; exit 1; } 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.sh
nvm use v0.2.3 && 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 && 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"

View File

@ -5,8 +5,9 @@
# Back up # Back up
type setopt >/dev/null 2>&1 && setopt NULL_GLOB
type shopt >/dev/null 2>&1 && shopt -s nullglob
mkdir -p bak mkdir -p bak
mv v* src alias bak || sleep 0s for SRC in v* src alias; do
[ -e "$SRC" ] && mv "$SRC" bak
done
true
) )

View File

@ -1,14 +0,0 @@
#!/bin/sh
set -e
. ../../nvm.sh
# Remove the stuff we're clobbering.
[ -e ../../v0.6.14 ] && rm -R ../../v0.6.14
# Install
nvm install 0.6.14
# Check
[ -d ../../v0.6.14 ]
nvm run v0.6.14 --version | grep v0.6.14

View File

@ -3,12 +3,14 @@
set -e set -e
. ../../nvm.sh . ../../nvm.sh
VERSION=v0.10.26
# Remove the stuff we're clobbering. # Remove the stuff we're clobbering.
[ -e ../../v0.8.6 ] && rm -R ../../v0.8.6 [ -e ../../$VERSION ] && rm -R ../../$VERSION
# Install from binary # Install from binary
nvm install 0.8.6 nvm install $VERSION
# Check # Check
[ -d ../../v0.8.6 ] [ -d ../../$VERSION ]
nvm run v0.8.6 --version | grep v0.8.6 nvm run $VERSION --version | grep $VERSION

View File

@ -3,12 +3,14 @@
set -e set -e
. ../../nvm.sh . ../../nvm.sh
VERSION=v0.10.26
# Remove the stuff we're clobbering. # Remove the stuff we're clobbering.
[ -e ../../v0.8.6 ] && rm -R ../../v0.8.6 [ -e ../../$VERSION ] && rm -R ../../$VERSION
# Install from source # Install from source
nvm install -s 0.8.6 nvm install -s $VERSION
# Check # Check
[ -d ../../v0.8.6 ] [ -d ../../$VERSION ]
nvm run v0.8.6 --version | grep v0.8.6 nvm run $VERSION --version | grep $VERSION