nvm/nvm.sh

101 lines
2.4 KiB
Bash
Raw Normal View History

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)"
echo " nvm clone (Clone and install HEAD version)"
echo " nvm update (Pull and rebuild HEAD 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-04-16 00:53:39 +08:00
echo
echo "Example:"
echo " nvm install v0.1.91"
2010-04-16 00:20:27 +08:00
echo
;;
2010-04-16 00:53:39 +08:00
"clone" )
if [ $# -ne 1 ]; then
nvm help
return;
fi
mkdir -p "$NVM_DIR/src" && \
cd "$NVM_DIR/src" && \
git clone git://github.com/ry/node.git && \
cd node && \
./configure --prefix="$NVM_DIR/HEAD" && \
make && \
make install && \
nvm use HEAD
cd $START
;;
"update" )
if [ $# -ne 1 ]; then
nvm help
return;
fi
cd "$NVM_DIR/src/node" && \
git pull --rebase origin master
./configure && \
make clean all && \
make install && \
nvm use HEAD
cd $START
;;
2010-04-16 00:20:27 +08:00
"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
;;
"use" )
2010-04-16 00:53:39 +08:00
if [ $# -ne 2 ]; then
2010-04-16 00:20:27 +08:00
nvm help
return;
fi
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-04-16 00:20:27 +08:00
echo "Now using node $2"
;;
"list" )
2010-04-16 00:53:39 +08:00
if [ $# -ne 1 ]; then
nvm help
return;
fi
2010-04-16 00:20:27 +08:00
# TODO: put a star by the current active one if possible
2010-04-16 01:14:52 +08:00
ls "$NVM_DIR" | grep -v src | grep -v nvm.sh | grep -v README.markdown
2010-04-16 00:20:27 +08:00
;;
* )
nvm help
;;
esac
2010-04-16 00:13:33 +08:00
}