11#! /usr/bin/env bash
22# Summary: Install a version of NodeJS.
33#
4- # Usage: nodenv install <version>
4+ # Usage: nodenv install <version> [--source]
55#
66# Versions should be in the form of vN.N.N
7+ #
8+ # If --source is given, tries to build NodeJS from source.
79
810# Bomb out if we hit an error, ever
911set -e
@@ -15,37 +17,72 @@ set -e
1517
1618# Pull the desired version out of ARGV
1719version=" $1 "
20+ version_dir=" $NODENV_ROOT /versions/$version "
1821
19- # Determine OS
20- os=" darwin"
21-
22- # Determine arch
23- arch=" x64"
24-
25- # URL to download from
26- download=" http://nodejs.org/dist/${version} /node-${version} -${os} -${arch} .tar.gz"
27-
28- # Save current PWD
22+ # stash the pwd
2923OLDPWD=$( pwd)
30- version_dir=" $NODENV_ROOT /versions/$version "
3124
32- # Install from bin
25+ # Are we going to compile this instead?
26+ compile=" $2 "
27+
28+ # Make the version dir and get in there
3329mkdir -p " $version_dir "
3430cd " $version_dir "
3531
36- # Can't get too clever here
37- set +e
32+ if [ " $compile " = " --source" ]; then
33+ # Let's fetch the source and build it
34+ download=" http://nodejs.org/dist/${version} /node-${version} .tar.gz"
3835
39- # Download binary tarball and install
40- curl -s -f " $download " > /tmp/node-$version .tar.gz && \
41- tar zxf /tmp/node-$version .tar.gz --strip-components 1 || \
42- {
43- cd $OLDPWD
44- rmdir " $version_dir "
36+ # Can't get too clever here
37+ set +e
4538
46- echo " nodenv: unable to install NodeJS \` ${version} ' from binary, download not available"
47- exit 1
48- }
39+ # Download source and compile it
40+ curl -s -f " $download " > /tmp/node-$version .tar.gz && \
41+ tar zxf /tmp/node-$version .tar.gz -C /tmp && \
42+ cd /tmp/node-$version && \
43+ (./configure --prefix=" $version_dir " && make && make install) 2>&1 > /tmp/nodenv-install-$version .log && \
44+ rm /tmp/node-$version .tar.gz && \
45+ rm -rf /tmp/node-$version || \
46+ {
47+ cd $OLDPWD
48+ rm -rf " $version_dir " /tmp/node-$version .tar.gz /tmp/node-$version
49+
50+ echo " nodenv: installation of $version from source failed" >&2
51+ exit 1
52+ }
53+ else
54+ # Determine url to download from
55+ if [ " $( uname -s) " = " Darwin" ]; then
56+ platform=" darwin"
57+ else
58+ platform=" $( uname -s) "
59+ fi
60+
61+ if [ " $( uname -m) " = " x86_64" ]; then
62+ arch=" x64"
63+ else
64+ arch=" $( uname -p) "
65+ fi
66+
67+ # URL to download from
68+ download=" http://nodejs.org/dist/${version} /node-${version} -${platform} -${arch} .tar.gz"
69+
70+ # Can't get too clever here
71+ set +e
72+
73+ # Download binary tarball and install
74+ curl -s -f " $download " > /tmp/node-$version .tar.gz && \
75+ tar zxf /tmp/node-$version .tar.gz --strip-components 1 && \
76+ rm /tmp/node-$version .tar.gz || \
77+ {
78+ cd $OLDPWD
79+ rmdir " $version_dir "
80+
81+ echo " nodenv: unable to install NodeJS \` ${version} ' from binary, download not available"
82+ exit 1
83+ }
84+ fi
4985
5086echo " Installed ${version} "
5187cd $OLDPWD
88+
0 commit comments