Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated node version manager plugin to current master #107

Merged
merged 1 commit into from Mar 13, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
172 changes: 88 additions & 84 deletions plugins/available/nvm.plugin.bash
Expand Up @@ -16,62 +16,61 @@ if [ ! -d "$NVM_DIR" ]; then
export NVM_DIR=$(cd $(dirname ${BASH_SOURCE[0]:-$0}); pwd)
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
nvm_version()
{
PATTERN=$1
VERSION=''
if [ -f "$NVM_DIR/alias/$PATTERN" ]; then
nvm_version `cat $NVM_DIR/alias/$PATTERN`
return
fi
# If it looks like an explicit version, don't do anything funny
if [[ "$PATTERN" == v*.*.* ]]; then
VERSION="$PATTERN"
fi
# The default version is the current one
if [ ! "$PATTERN" -o "$PATTERN" = 'current' ]; then
VERSION=`node -v 2>/dev/null`
if [ ! "$PATTERN" ]; then
PATTERN='current'
fi
if [ "$PATTERN" = 'stable' ]; then
PATTERN='*.*[02468].'

VERSION=`nvm_ls $PATTERN | tail -n1`
echo "$VERSION"

if [ "$VERSION" = 'N/A' ]; then
return 13
fi
if [ "$PATTERN" = 'latest' ]; then
PATTERN='*.*.'
}

nvm_ls()
{
PATTERN=$1
VERSIONS=''
if [ "$PATTERN" = 'current' ]; then
echo `node -v 2>/dev/null`
return
fi
if [ "$PATTERN" = 'all' ]; then
(cd $NVM_DIR; \ls -dG v* 2>/dev/null || echo "N/A")

if [ -f "$NVM_DIR/alias/$PATTERN" ]; then
nvm_version `cat $NVM_DIR/alias/$PATTERN`
return
fi
if [ ! "$VERSION" ]; then
VERSION=`(cd $NVM_DIR; \ls -d v${PATTERN}* 2>/dev/null) | sort -t. -k 2,1n -k 2,2n -k 3,3n | tail -n1`
# If it looks like an explicit version, don't do anything funny
if [[ "$PATTERN" == v?*.?*.?* ]]; then
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
if [ ! "$VERSION" ]; then
if [ ! "$VERSIONS" ]; then
echo "N/A"
return 13
elif [ -e "$NVM_DIR/$VERSION" ]; then
(cd $NVM_DIR; \ls -dG "$VERSION")
else
echo "$VERSION"
return
fi
echo "$VERSIONS"
return
}

print_versions()
{
OUTPUT=''
for VERSION in $1; do
PADDED_VERSION=`printf '%10s' $VERSION`
if [[ -d "$NVM_DIR/$VERSION" ]]; then
PADDED_VERSION="\033[0;34m$PADDED_VERSION\033[0m"
fi
OUTPUT="$OUTPUT\n$PADDED_VERSION"
done
echo -e "$OUTPUT" | column
}

nvm()
Expand All @@ -90,30 +89,35 @@ nvm()
echo " nvm install <version> Download and install a <version>"
echo " nvm uninstall <version> Uninstall a 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 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 <name> <version> Set an alias named <name> pointing to <version>"
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
echo "Example:"
echo " nvm install v0.4.0 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 install v0.4.12 Install a specific version number"
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
;;
"install" )
if [ ! `which curl` ]; then
echo 'NVM Needs curl to proceed.' >&2;
fi

if [ $# -ne 2 ]; then
nvm help
return
fi
[ "$NOCURL" ] && curl && return
VERSION=`nvm_version $2`

[ -d "$NVM_DIR/$VERSION" ] && echo "$VERSION is already installed." && return

tarball=''
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"
Expand All @@ -124,7 +128,7 @@ nvm()
[ ! -z $tarball ] && \
mkdir -p "$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" && \
cd "node-$VERSION" && \
./configure --prefix="$NVM_DIR/$VERSION" && \
Expand All @@ -136,8 +140,17 @@ nvm()
nvm use $VERSION
if ! which npm ; then
echo "Installing npm..."
# TODO: if node version 0.2.x add npm_install=0.2.19 before sh
curl http://npmjs.org/install.sh | clean=yes sh
if [[ "`expr match $VERSION '\(^v0\.1\.\)'`" != '' ]]; then
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
else
echo "nvm: install $VERSION failed!"
Expand All @@ -156,10 +169,9 @@ nvm()
fi

# Delete all files related to target version.
(cd "$NVM_DIR" && \
rm -rf "node-$VERSION" 2>/dev/null && \
mkdir -p "$NVM_DIR/src" && \
(mkdir -p "$NVM_DIR/src" && \
cd "$NVM_DIR/src" && \
rm -rf "node-$VERSION" 2>/dev/null && \
rm -f "node-$VERSION.tar.gz" 2>/dev/null && \
rm -rf "$NVM_DIR/$VERSION" 2>/dev/null)
echo "Uninstalled node $VERSION"
Expand All @@ -170,8 +182,6 @@ nvm()
nvm unalias `basename $A`
done

# Run sync in order to restore version stub file in $NVM_DIR.
nvm sync 1>/dev/null
;;
"deactivate" )
if [[ $PATH == *$NVM_DIR/*/bin* ]]; then
Expand Down Expand Up @@ -215,17 +225,27 @@ nvm()
export NVM_BIN="$NVM_DIR/$VERSION/bin"
echo "Now using node $VERSION"
;;
"ls" )
if [ $# -ne 1 ]; then
nvm_version $2
"run" )
# run given version of node
if [ $# -lt 2 ]; then
nvm help
return
fi
nvm_version all
for P in {stable,latest,current}; do
echo -ne "$P: \t"; nvm_version $P
done
nvm alias
echo "# use 'nvm sync' to update from nodejs.org"
VERSION=`nvm_version $2`
if [ ! -d $NVM_DIR/$VERSION ]; then
echo "$VERSION version is not installed yet"
return;
fi
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" )
mkdir -p $NVM_DIR/alias
Expand Down Expand Up @@ -254,7 +274,6 @@ nvm()
echo $3 > "$NVM_DIR/alias/$2"
if [ ! "$3" = "$VERSION" ]; then
echo "$2 -> $3 (-> $VERSION)"
echo "! WARNING: Moving target. Aliases to implicit versions may change without warning."
else
echo "$2 -> $3"
fi
Expand All @@ -266,21 +285,6 @@ nvm()
rm -f $NVM_DIR/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" )
if [ $# -ne 2 ]; then
nvm help
Expand All @@ -296,7 +300,7 @@ nvm()
echo "Cache cleared."
;;
"version" )
nvm_version $2
print_versions "`nvm_version $2`"
;;
* )
nvm help
Expand Down