Support binary installs

Heavily inspired by isaacs/nave
master
Marsup 2012-10-12 09:20:46 +02:00
parent eea0b05ece
commit a0c4229abe
1 changed files with 51 additions and 2 deletions

53
nvm.sh
View File

@ -16,6 +16,19 @@ if [ ! -z "$(which unsetopt 2>/dev/null)" ]; then
unsetopt nomatch 2>/dev/null unsetopt nomatch 2>/dev/null
fi fi
# Try to figure out the os and arch for binary fetching
uname="$(uname -a)"
os=
arch=x86
case "$uname" in
Linux\ *) os=linux ;;
Darwin\ *) os=darwin ;;
SunOS\ *) os=sunos ;;
esac
case "$uname" in
*x86_64*) arch=x64 ;;
esac
# Expand a version using the version cache # Expand a version using the version cache
nvm_version() nvm_version()
{ {
@ -156,10 +169,40 @@ nvm()
shift shift
done done
echo "Additional options while compiling: $ADDITIONAL_PARAMETERS"
[ -d "$NVM_DIR/$VERSION" ] && echo "$VERSION is already installed." && return [ -d "$NVM_DIR/$VERSION" ] && echo "$VERSION is already installed." && return
# shortcut - try the binary if possible.
if [ -n "$os" ]; then
binavail=
# binaries started with node 0.8.6
case "$VERSION" in
v0.8.[012345]) binavail=0 ;;
v0.[1234567]) binavail=0 ;;
*) binavail=1 ;;
esac
if [ $binavail -eq 1 ]; then
t="$VERSION-$os-$arch"
url="http://nodejs.org/dist/$VERSION/node-${t}.tar.gz"
if (
mkdir -p "$NVM_DIR/bin/node-${t}" && \
cd "$NVM_DIR/bin" && \
curl -C - --progress-bar $url -o "node-${t}.tar.gz" && \
tar -xzf "node-${t}.tar.gz" -C "node-${t}" --strip-components 1 && \
mv "node-${t}" "../$VERSION" && \
rm -f "node-${t}.tar.gz"
)
then
nvm use $VERSION
return;
else
echo "Binary download failed, trying source." >&2
cd "$NVM_DIR/bin" && rm -rf "node-${t}.tar.gz" "node-${t}"
fi
fi
fi
echo "Additional options while compiling: $ADDITIONAL_PARAMETERS"
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"
@ -211,11 +254,17 @@ nvm()
return; return;
fi fi
t="$VERSION-$os-$arch"
# Delete all files related to target version. # Delete all files related to target version.
(mkdir -p "$NVM_DIR/src" && \ (mkdir -p "$NVM_DIR/src" && \
cd "$NVM_DIR/src" && \ cd "$NVM_DIR/src" && \
rm -rf "node-$VERSION" 2>/dev/null && \ 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 && \
mkdir -p "$NVM_DIR/bin" && \
cd "$NVM_DIR/bin" && \
rm -rf "node-${t}" 2>/dev/null && \
rm -f "node-${t}.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"