Updated node version manager plugin to current master

pull/107/head
Antono Vasiljev 2012-03-05 02:13:53 +03:00
parent 39f8ef9875
commit 1527630754
1 changed files with 89 additions and 85 deletions

View File

@ -16,62 +16,61 @@ if [ ! -d "$NVM_DIR" ]; then
export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}); pwd) export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}); pwd)
fi fi
# 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
# Expand a version using the version cache # Expand a version using the version cache
nvm_version() nvm_version()
{ {
PATTERN=$1 PATTERN=$1
VERSION='' # The default version is the current one
if [ ! "$PATTERN" ]; then
PATTERN='current'
fi
VERSION=`nvm_ls $PATTERN | tail -n1`
echo "$VERSION"
if [ "$VERSION" = 'N/A' ]; then
return 13
fi
}
nvm_ls()
{
PATTERN=$1
VERSIONS=''
if [ "$PATTERN" = 'current' ]; then
echo `node -v 2>/dev/null`
return
fi
if [ -f "$NVM_DIR/alias/$PATTERN" ]; then if [ -f "$NVM_DIR/alias/$PATTERN" ]; then
nvm_version `cat $NVM_DIR/alias/$PATTERN` nvm_version `cat $NVM_DIR/alias/$PATTERN`
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 [[ "$PATTERN" == v?*.?*.?* ]]; then
VERSION="$PATTERN" VERSIONS="$PATTERN"
else
VERSIONS=`(cd $NVM_DIR; \ls -d v${PATTERN}* 2>/dev/null) | sort -t. -k 1.2,1n -k 2,2n -k 3,3n`
fi fi
# The default version is the current one if [ ! "$VERSIONS" ]; then
if [ ! "$PATTERN" -o "$PATTERN" = 'current' ]; then echo "N/A"
VERSION=`node -v 2>/dev/null`
fi
if [ "$PATTERN" = 'stable' ]; then
PATTERN='*.*[02468].'
fi
if [ "$PATTERN" = 'latest' ]; then
PATTERN='*.*.'
fi
if [ "$PATTERN" = 'all' ]; then
(cd $NVM_DIR; \ls -dG v* 2>/dev/null || echo "N/A")
return return
fi fi
if [ ! "$VERSION" ]; then echo "$VERSIONS"
VERSION=`(cd $NVM_DIR; \ls -d v${PATTERN}* 2>/dev/null) | sort -t. -k 2,1n -k 2,2n -k 3,3n | tail -n1` return
fi }
if [ ! "$VERSION" ]; then
echo "N/A" print_versions()
return 13 {
elif [ -e "$NVM_DIR/$VERSION" ]; then OUTPUT=''
(cd $NVM_DIR; \ls -dG "$VERSION") for VERSION in $1; do
else PADDED_VERSION=`printf '%10s' $VERSION`
echo "$VERSION" if [[ -d "$NVM_DIR/$VERSION" ]]; then
fi PADDED_VERSION="\033[0;34m$PADDED_VERSION\033[0m"
fi
OUTPUT="$OUTPUT\n$PADDED_VERSION"
done
echo -e "$OUTPUT" | column
} }
nvm() nvm()
@ -90,30 +89,35 @@ nvm()
echo " nvm install <version> Download and install a <version>" echo " nvm install <version> Download and install a <version>"
echo " nvm uninstall <version> Uninstall a version" echo " nvm uninstall <version> Uninstall a version"
echo " nvm use <version> Modify PATH to use <version>" echo " nvm use <version> Modify PATH to use <version>"
echo " nvm ls List versions (installed versions are blue)" echo " nvm run <version> [<args>] Run <version> with <args> as arguments"
echo " nvm ls List installed versions"
echo " nvm ls <version> List versions matching a given description" echo " nvm ls <version> List versions matching a given description"
echo " nvm deactivate Undo effects of NVM on current shell" echo " nvm deactivate Undo effects of NVM on current shell"
echo " nvm sync Update the local cache of available versions"
echo " nvm alias [<pattern>] Show all aliases beginning with <pattern>" echo " nvm alias [<pattern>] Show all aliases beginning with <pattern>"
echo " nvm alias <name> <version> Set an alias named <name> pointing to <version>" echo " nvm alias <name> <version> Set an alias named <name> pointing to <version>"
echo " nvm unalias <name> Deletes the alias named <name>" echo " nvm unalias <name> Deletes the alias named <name>"
echo " nvm copy-packages <version> Install global NPM packages contained in <version> to current version" echo " nvm copy-packages <version> Install global NPM packages contained in <version> to current version"
echo echo
echo "Example:" echo "Example:"
echo " nvm install v0.4.0 Install a specific version number" echo " nvm install v0.4.12 Install a specific version number"
echo " nvm use stable Use the stable release"
echo " nvm install latest Install the latest, possibly unstable version"
echo " nvm use 0.2 Use the latest available 0.2.x release" echo " nvm use 0.2 Use the latest available 0.2.x release"
echo " nvm alias default v0.4.0 Set v0.4.0 as the default" echo " nvm run 0.4.12 myApp.js Run myApp.js using node v0.4.12"
echo " nvm alias default 0.4 Auto use the latest installed v0.4.x version"
echo echo
;; ;;
"install" ) "install" )
if [ ! `which curl` ]; then
echo 'NVM Needs curl to proceed.' >&2;
fi
if [ $# -ne 2 ]; then if [ $# -ne 2 ]; then
nvm help nvm help
return return
fi fi
[ "$NOCURL" ] && curl && return
VERSION=`nvm_version $2` VERSION=`nvm_version $2`
[ -d "$NVM_DIR/$VERSION" ] && echo "$VERSION is already installed." && return
tarball='' tarball=''
if [ "`curl -Is "http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" | grep '200 OK'`" != '' ]; then if [ "`curl -Is "http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" | grep '200 OK'`" != '' ]; then
tarball="http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz" tarball="http://nodejs.org/dist/$VERSION/node-$VERSION.tar.gz"
@ -124,7 +128,7 @@ nvm()
[ ! -z $tarball ] && \ [ ! -z $tarball ] && \
mkdir -p "$NVM_DIR/src" && \ mkdir -p "$NVM_DIR/src" && \
cd "$NVM_DIR/src" && \ cd "$NVM_DIR/src" && \
curl -C - -# $tarball -o "node-$VERSION.tar.gz" && \ curl -C - --progress-bar $tarball -o "node-$VERSION.tar.gz" && \
tar -xzf "node-$VERSION.tar.gz" && \ tar -xzf "node-$VERSION.tar.gz" && \
cd "node-$VERSION" && \ cd "node-$VERSION" && \
./configure --prefix="$NVM_DIR/$VERSION" && \ ./configure --prefix="$NVM_DIR/$VERSION" && \
@ -136,8 +140,17 @@ nvm()
nvm use $VERSION nvm use $VERSION
if ! which npm ; then if ! which npm ; then
echo "Installing npm..." echo "Installing npm..."
# TODO: if node version 0.2.x add npm_install=0.2.19 before sh if [[ "`expr match $VERSION '\(^v0\.1\.\)'`" != '' ]]; then
curl http://npmjs.org/install.sh | clean=yes sh 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
echo "npm requires node v0.2.3 or higher"
else
curl http://npmjs.org/install.sh | clean=yes npm_install=0.2.19 sh
fi
else
curl http://npmjs.org/install.sh | clean=yes sh
fi
fi fi
else else
echo "nvm: install $VERSION failed!" echo "nvm: install $VERSION failed!"
@ -156,10 +169,9 @@ nvm()
fi fi
# Delete all files related to target version. # Delete all files related to target version.
(cd "$NVM_DIR" && \ (mkdir -p "$NVM_DIR/src" && \
rm -rf "node-$VERSION" 2>/dev/null && \
mkdir -p "$NVM_DIR/src" && \
cd "$NVM_DIR/src" && \ cd "$NVM_DIR/src" && \
rm -rf "node-$VERSION" 2>/dev/null && \
rm -f "node-$VERSION.tar.gz" 2>/dev/null && \ rm -f "node-$VERSION.tar.gz" 2>/dev/null && \
rm -rf "$NVM_DIR/$VERSION" 2>/dev/null) rm -rf "$NVM_DIR/$VERSION" 2>/dev/null)
echo "Uninstalled node $VERSION" echo "Uninstalled node $VERSION"
@ -170,8 +182,6 @@ nvm()
nvm unalias `basename $A` nvm unalias `basename $A`
done done
# Run sync in order to restore version stub file in $NVM_DIR.
nvm sync 1>/dev/null
;; ;;
"deactivate" ) "deactivate" )
if [[ $PATH == *$NVM_DIR/*/bin* ]]; then if [[ $PATH == *$NVM_DIR/*/bin* ]]; then
@ -215,17 +225,27 @@ nvm()
export NVM_BIN="$NVM_DIR/$VERSION/bin" export NVM_BIN="$NVM_DIR/$VERSION/bin"
echo "Now using node $VERSION" echo "Now using node $VERSION"
;; ;;
"ls" ) "run" )
if [ $# -ne 1 ]; then # run given version of node
nvm_version $2 if [ $# -lt 2 ]; then
nvm help
return return
fi fi
nvm_version all VERSION=`nvm_version $2`
for P in {stable,latest,current}; do if [ ! -d $NVM_DIR/$VERSION ]; then
echo -ne "$P: \t"; nvm_version $P echo "$VERSION version is not installed yet"
done return;
nvm alias fi
echo "# use 'nvm sync' to update from nodejs.org" echo "Running node $VERSION"
$NVM_DIR/$VERSION/bin/node "${@:3}"
;;
"ls" | "list" )
print_versions "`nvm_ls $2`"
if [ $# -eq 1 ]; then
echo -ne "current: \t"; nvm_version current
nvm alias
fi
return
;; ;;
"alias" ) "alias" )
mkdir -p $NVM_DIR/alias mkdir -p $NVM_DIR/alias
@ -254,7 +274,6 @@ nvm()
echo $3 > "$NVM_DIR/alias/$2" echo $3 > "$NVM_DIR/alias/$2"
if [ ! "$3" = "$VERSION" ]; then if [ ! "$3" = "$VERSION" ]; then
echo "$2 -> $3 (-> $VERSION)" echo "$2 -> $3 (-> $VERSION)"
echo "! WARNING: Moving target. Aliases to implicit versions may change without warning."
else else
echo "$2 -> $3" echo "$2 -> $3"
fi fi
@ -266,21 +285,6 @@ nvm()
rm -f $NVM_DIR/alias/$2 rm -f $NVM_DIR/alias/$2
echo "Deleted alias $2" echo "Deleted alias $2"
;; ;;
"sync" )
[ "$NOCURL" ] && curl && return
LATEST=`nvm_version latest`
STABLE=`nvm_version stable`
(cd $NVM_DIR
rm -f v* 2>/dev/null
printf "# syncing with nodejs.org..."
for VER in `curl -s http://nodejs.org/dist/ -o - | grep 'v[0-9].*' | sed -e 's/.*node-//' -e 's/\.tar\.gz.*//' -e 's/<[^>]*>//' -e 's/\/<[^>]*>.*//'`; do
touch $VER
done
echo " done."
)
[ "$STABLE" = `nvm_version stable` ] || echo "NEW stable: `nvm_version stable`"
[ "$LATEST" = `nvm_version latest` ] || echo "NEW latest: `nvm_version latest`"
;;
"copy-packages" ) "copy-packages" )
if [ $# -ne 2 ]; then if [ $# -ne 2 ]; then
nvm help nvm help
@ -296,7 +300,7 @@ nvm()
echo "Cache cleared." echo "Cache cleared."
;; ;;
"version" ) "version" )
nvm_version $2 print_versions "`nvm_version $2`"
;; ;;
* ) * )
nvm help nvm help