Permalink
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 125 lines (102 sloc) 3.24 KB
#!/usr/bin/env bash
# Summary: Install a version of NodeJS.
#
# Usage: nodenv install <version> [--source]
#
# Versions should be in the form of vN.N.N
#
# If --source is given, tries to build NodeJS from source.
# Bomb out if we hit an error, ever
set -e
# Verbose output in debug mode
[ -n "$NODENV_DEBUG" ] && {
set -x
}
list_definitions() {
{ curl -s "http://nodejs.org/dist/" | \
awk 'match($0,/>(v[0-9]+\.[0-9]+\.[0-9]+)\/</) { print substr($0, RSTART + 1, RLENGTH - 3); }' -
} | sort_versions | uniq
}
sort_versions() {
LC_ALL=C sort -t. -k 1,1 -k 2,2n -k 3,3n -k 4,4n -k 5,5n
}
# Provide nodenv completions
if [ "$1" = "--complete" ]; then
list_definitions
exit 0
fi
# Sugar for install latest version of iojs
if [[ $1 == iojs ]]; then
iolatest=$(curl -s -f https://iojs.org/dist/index.tab | head -n 2 | awk 'NR==2{print $1}')
set -- "iojs-$iolatest" "${@:2}"
fi
# Pull the desired version out of ARGV
version="$1"
version_dir="$NODENV_ROOT/versions/$version"
# stash the pwd
OLDPWD=$(pwd)
# Are we going to compile this instead?
compile="$2"
# Make the version dir and get in there
mkdir -p "$version_dir"
cd "$version_dir"
if [ "$compile" = "--source" ]; then
# Let's fetch the source and build it
if [[ $1 == iojs-* ]]; then
ioversion=${1##*-}
work_dir="/tmp/iojs-$ioversion"
download="https://iojs.org/dist/${ioversion}/iojs-${ioversion}.tar.gz"
# There is no tarballs at alternatives places on io.js project, so try again!
alt_download="https://iojs.org/dist/${ioversion}/iojs-${ioversion}.tar.gz"
else
download="http://nodejs.org/dist/${version}/node-${version}.tar.gz"
alt_download="http://nodejs.org/dist/node-${version}.tar.gz"
work_dir="/tmp/node-$version"
fi
# Can't get too clever here
set +e
# Download source and compile it
(curl -s -f "$download" > /tmp/node-$version.tar.gz || \
curl -s -f "$alt_download" > /tmp/node-$version.tar.gz) && \
tar zxf /tmp/node-$version.tar.gz -C /tmp && \
cd $work_dir && \
($PYTHON ./configure --prefix="$version_dir" && make && make install) 2>&1 > /tmp/nodenv-install-$version.log && \
rm /tmp/node-$version.tar.gz && \
rm -rf $work_dir || \
{
cd $OLDPWD
rm -rf "$version_dir" /tmp/node-$version.tar.gz $work_dir
echo "nodenv: installation of $version from source failed" >&2
exit 1
}
else
# Determine url to download from
platform="$(uname -s | tr '[:upper:]' '[:lower:]')"
if [ "$(uname -m)" = "x86_64" ]; then
arch="x64"
else
arch="$(uname -p)"
fi
# URL to download from
if [[ $1 == iojs-* ]];then
ioversion=${1##*-}
download="https://iojs.org/dist/${ioversion}/iojs-${ioversion}-${platform}-${arch}.tar.gz"
else
download="http://nodejs.org/dist/${version}/node-${version}-${platform}-${arch}.tar.gz"
fi
# Can't get too clever here
set +e
# Download binary tarball and install
curl -s -f "$download" > /tmp/node-$version.tar.gz && \
tar zxf /tmp/node-$version.tar.gz --strip-components 1 && \
rm /tmp/node-$version.tar.gz || \
{
cd $OLDPWD
rmdir "$version_dir"
echo "nodenv: unable to install NodeJS \`${version}' from binary, download not available"
exit 1
}
fi
chmod -R 755 $version_dir
echo "Installed ${version}"
cd $OLDPWD