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
|
|
|
|
|
|
|
|
nvm()
|
|
|
|
{
|
2010-04-16 00:20:27 +08:00
|
|
|
START=`pwd`
|
|
|
|
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-04-16 01:22:16 +08:00
|
|
|
echo " nvm help (Show this message)"
|
2010-04-16 00:53:39 +08:00
|
|
|
echo " nvm install version (Download and install a released version)"
|
2010-04-16 01:22:16 +08:00
|
|
|
echo " nvm list (Show all installed versions)"
|
|
|
|
echo " nvm use version (Set this version in the PATH)"
|
2010-05-07 01:51:43 +08:00
|
|
|
echo " nvm use (Use the latest stable version)"
|
2010-04-21 07:51:07 +08:00
|
|
|
echo " nvm deactivate (Remove nvm entry from PATH)"
|
2010-04-27 01:50:18 +08:00
|
|
|
echo " nvm addlib (Copies the module in cwd to the current env)"
|
|
|
|
echo " nvm linklib (Links the module in cwd to the current env)"
|
2010-05-07 01:51:43 +08:00
|
|
|
echo " nvm listlibs (Show the modules in the current env)"
|
2010-04-16 00:53:39 +08:00
|
|
|
echo
|
|
|
|
echo "Example:"
|
2010-05-07 01:51:43 +08:00
|
|
|
echo " nvm install v0.1.94"
|
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
|
|
|
|
mkdir -p "$NVM_DIR/src" && \
|
|
|
|
cd "$NVM_DIR/src" && \
|
|
|
|
wget "http://nodejs.org/dist/node-$2.tar.gz" -N && \
|
|
|
|
tar -xzf "node-$2.tar.gz" && \
|
|
|
|
cd "node-$2" && \
|
|
|
|
./configure --prefix="$NVM_DIR/$2" && \
|
|
|
|
make && \
|
|
|
|
make install && \
|
|
|
|
nvm use $2
|
|
|
|
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:}
|
|
|
|
unset NODE_PATH
|
|
|
|
echo "$NVM_DIR/*/bin removed from \$PATH"
|
|
|
|
else
|
|
|
|
echo "Could not find $NVM_DIR/*/bin in \$PATH"
|
|
|
|
fi
|
|
|
|
;;
|
2010-04-27 01:40:35 +08:00
|
|
|
"addlib" )
|
|
|
|
mkdir -p $NODE_PATH
|
2010-04-27 01:50:18 +08:00
|
|
|
mkdir -p $NODE_BIN
|
|
|
|
if [ -d `pwd`/lib ]; then
|
|
|
|
cp -r `pwd`/lib/ "$NODE_PATH/"
|
|
|
|
cp -r `pwd`/bin/ "$NODE_BIN/"
|
|
|
|
else
|
|
|
|
echo "Can't find lib dir at `pwd`/lib"
|
|
|
|
fi
|
|
|
|
;;
|
|
|
|
"linklib" )
|
|
|
|
mkdir -p $NODE_PATH
|
|
|
|
mkdir -p $NODE_BIN
|
2010-04-27 01:40:35 +08:00
|
|
|
if [ -d `pwd`/lib ]; then
|
|
|
|
ln -sf `pwd`/lib/* "$NODE_PATH/"
|
|
|
|
ln -sf `pwd`/bin/* "$NODE_BIN/"
|
|
|
|
else
|
2010-04-27 01:50:18 +08:00
|
|
|
echo "Can't find lib dir at `pwd`/lib"
|
2010-04-27 01:40:35 +08:00
|
|
|
fi
|
|
|
|
;;
|
2010-04-16 00:20:27 +08:00
|
|
|
"use" )
|
2010-04-16 00:53:39 +08:00
|
|
|
if [ $# -ne 2 ]; then
|
2010-05-07 01:51:43 +08:00
|
|
|
for f in $NVM_DIR/v*; do
|
|
|
|
nvm use ${f##*/} > /dev/null
|
|
|
|
done
|
2010-04-16 00:20:27 +08:00
|
|
|
return;
|
|
|
|
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
|
|
|
|
export PATH
|
2010-05-07 01:51:43 +08:00
|
|
|
export NODE_PATH="$NVM_DIR/$2/lib/node"
|
2010-04-27 01:40:35 +08:00
|
|
|
export NODE_BIN="$NVM_DIR/$2/bin"
|
2010-04-27 01:50:18 +08:00
|
|
|
mkdir -p "$NODE_PATH"
|
|
|
|
mkdir -p "$NODE_BIN"
|
2010-04-16 00:20:27 +08:00
|
|
|
echo "Now using node $2"
|
|
|
|
;;
|
2010-05-07 01:51:43 +08:00
|
|
|
"listlibs" )
|
|
|
|
ls $NODE_PATH | grep -v wafadmin
|
|
|
|
;;
|
2010-04-16 00:20:27 +08:00
|
|
|
"list" )
|
2010-04-16 00:53:39 +08:00
|
|
|
if [ $# -ne 1 ]; then
|
|
|
|
nvm help
|
|
|
|
return;
|
|
|
|
fi
|
2010-04-19 23:46:58 +08:00
|
|
|
if [ -d $NVM_DIR/HEAD ]; then
|
|
|
|
if [[ $PATH == *$NVM_DIR/HEAD/bin* ]]; then
|
|
|
|
echo "HEAD *"
|
|
|
|
else
|
|
|
|
echo "HEAD"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
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-04-16 00:13:33 +08:00
|
|
|
}
|