2010-04-16 00:13:33 +08:00
|
|
|
# Node Version Manager
|
|
|
|
# Implemented as a bash function
|
|
|
|
# To use source this file from your bash profile
|
|
|
|
#
|
|
|
|
# Implemented by Tim Caswell <tim@creationix.com>
|
|
|
|
# with much bash help from Matthew Ranney
|
|
|
|
|
2010-12-11 03:32:16 +08:00
|
|
|
# Auto detect the NVM_DIR using magic bash 3.x stuff
|
|
|
|
export NVM_DIR=$(dirname ${BASH_ARGV[0]})
|
|
|
|
|
2011-01-23 04:07:27 +08:00
|
|
|
# Emulate curl with wget, if necessary
|
|
|
|
if [ ! `which curl` ]; then
|
|
|
|
if [ `which wget` ]; then
|
|
|
|
curl() {
|
|
|
|
ARGS="$* "
|
|
|
|
ARGS=${ARGS/-s /-q }
|
|
|
|
ARGS=${ARGS/-\# /}
|
|
|
|
ARGS=${ARGS/-C - /-c }
|
|
|
|
ARGS=${ARGS/-o /-O }
|
|
|
|
|
|
|
|
wget $ARGS
|
|
|
|
}
|
|
|
|
else
|
|
|
|
NOCURL='nocurl'
|
|
|
|
curl() { echo 'Need curl or wget to proceed.' >&2; }
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
2010-04-16 00:13:33 +08:00
|
|
|
nvm()
|
|
|
|
{
|
2010-04-16 00:20:27 +08:00
|
|
|
if [ $# -lt 1 ]; then
|
|
|
|
nvm help
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
case $1 in
|
|
|
|
"help" )
|
|
|
|
echo
|
|
|
|
echo "Node Version Manager"
|
2010-04-16 00:53:39 +08:00
|
|
|
echo
|
2010-04-16 00:20:27 +08:00
|
|
|
echo "Usage:"
|
2010-12-11 03:32:16 +08:00
|
|
|
echo " nvm help Show this message"
|
|
|
|
echo " nvm install <version> Download and install a <version>"
|
|
|
|
echo " nvm use <version> Modify PATH to use <version>"
|
|
|
|
echo " nvm ls List versions currently installed"
|
|
|
|
echo " nvm deactivate Undo effects of NVM on current shell"
|
2010-04-16 00:53:39 +08:00
|
|
|
echo
|
|
|
|
echo "Example:"
|
2010-12-11 03:32:16 +08:00
|
|
|
echo " nvm install v0.2.5"
|
|
|
|
echo " nvm use v0.2.5"
|
2010-04-16 00:20:27 +08:00
|
|
|
echo
|
|
|
|
;;
|
|
|
|
"install" )
|
2010-04-16 00:53:39 +08:00
|
|
|
if [ $# -ne 2 ]; then
|
2010-04-16 00:20:27 +08:00
|
|
|
nvm help
|
|
|
|
return;
|
|
|
|
fi
|
2011-01-23 04:07:27 +08:00
|
|
|
[ "$NOCURL" ] && curl && return
|
2010-12-11 04:52:58 +08:00
|
|
|
START=`pwd`
|
2010-04-16 00:20:27 +08:00
|
|
|
mkdir -p "$NVM_DIR/src" && \
|
|
|
|
cd "$NVM_DIR/src" && \
|
2011-01-23 04:07:27 +08:00
|
|
|
curl -C - -# "http://nodejs.org/dist/node-$2.tar.gz" -o "node-$2.tar.gz" && \
|
2010-04-16 00:20:27 +08:00
|
|
|
tar -xzf "node-$2.tar.gz" && \
|
|
|
|
cd "node-$2" && \
|
|
|
|
./configure --prefix="$NVM_DIR/$2" && \
|
|
|
|
make && \
|
|
|
|
make install && \
|
|
|
|
nvm use $2
|
2010-12-11 04:43:27 +08:00
|
|
|
if ! which npm ; then
|
|
|
|
echo "Installing npm..."
|
2011-01-23 04:07:27 +08:00
|
|
|
curl -# http://npmjs.org/install.sh -o - | sh
|
2010-12-11 04:43:27 +08:00
|
|
|
fi
|
2010-04-16 00:20:27 +08:00
|
|
|
cd $START
|
|
|
|
;;
|
2010-04-21 07:51:07 +08:00
|
|
|
"deactivate" )
|
|
|
|
if [[ $PATH == *$NVM_DIR/*/bin* ]]; then
|
|
|
|
export PATH=${PATH%$NVM_DIR/*/bin*}${PATH#*$NVM_DIR/*/bin:}
|
|
|
|
echo "$NVM_DIR/*/bin removed from \$PATH"
|
|
|
|
else
|
|
|
|
echo "Could not find $NVM_DIR/*/bin in \$PATH"
|
|
|
|
fi
|
2010-12-12 12:19:23 +08:00
|
|
|
if [[ $MANPATH == *$NVM_DIR/*/share/man* ]]; then
|
|
|
|
export MANPATH=${MANPATH%$NVM_DIR/*/share/man*}${MANPATH#*$NVM_DIR/*/share/man:}
|
|
|
|
echo "$NVM_DIR/*/share/man removed from \$MANPATH"
|
|
|
|
else
|
|
|
|
echo "Could not find $NVM_DIR/*/share/man in \$MANPATH"
|
|
|
|
fi
|
2010-04-27 01:40:35 +08:00
|
|
|
;;
|
2010-04-16 00:20:27 +08:00
|
|
|
"use" )
|
2010-04-16 00:53:39 +08:00
|
|
|
if [ $# -ne 2 ]; then
|
2010-12-11 03:32:16 +08:00
|
|
|
nvm help
|
|
|
|
return
|
2010-04-16 00:20:27 +08:00
|
|
|
fi
|
2010-04-19 23:46:58 +08:00
|
|
|
if [ ! -d $NVM_DIR/$2 ]; then
|
|
|
|
echo "$2 version is not installed yet"
|
|
|
|
return;
|
|
|
|
fi
|
2010-04-19 23:22:28 +08:00
|
|
|
if [[ $PATH == *$NVM_DIR/*/bin* ]]; then
|
|
|
|
PATH=${PATH%$NVM_DIR/*/bin*}$NVM_DIR/$2/bin${PATH#*$NVM_DIR/*/bin}
|
|
|
|
else
|
|
|
|
PATH="$NVM_DIR/$2/bin:$PATH"
|
|
|
|
fi
|
2010-12-12 12:19:23 +08:00
|
|
|
if [[ $MANPATH == *$NVM_DIR/*/share/man* ]]; then
|
|
|
|
MANPATH=${MANPATH%$NVM_DIR/*/share/man*}$NVM_DIR/$2/share/man${MANPATH#*$NVM_DIR/*/share/man}
|
|
|
|
else
|
|
|
|
MANPATH="$NVM_DIR/$2/share/man:$MANPATH"
|
|
|
|
fi
|
2010-04-19 23:22:28 +08:00
|
|
|
export PATH
|
2010-12-12 12:19:23 +08:00
|
|
|
export MANPATH
|
2010-05-15 01:39:54 +08:00
|
|
|
export NVM_PATH="$NVM_DIR/$2/lib/node"
|
|
|
|
export NVM_BIN="$NVM_DIR/$2/bin"
|
2010-04-16 00:20:27 +08:00
|
|
|
echo "Now using node $2"
|
|
|
|
;;
|
2010-12-11 03:32:16 +08:00
|
|
|
"ls" )
|
2010-04-16 00:53:39 +08:00
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
nvm help
|
|
|
|
return;
|
|
|
|
fi
|
2010-04-19 23:46:58 +08:00
|
|
|
for f in $NVM_DIR/v*; do
|
|
|
|
if [[ $PATH == *$f/bin* ]]; then
|
|
|
|
echo "v${f##*v} *"
|
|
|
|
else
|
|
|
|
echo "v${f##*v}"
|
|
|
|
fi
|
|
|
|
done
|
2010-04-16 00:20:27 +08:00
|
|
|
;;
|
|
|
|
* )
|
|
|
|
nvm help
|
|
|
|
;;
|
|
|
|
esac
|
2010-12-11 03:32:16 +08:00
|
|
|
}
|