Commit ad1e2ba5 authored by zeke's avatar zeke

when the default stable version doesn't satisfy, get all available versions from nodejs.org/dist

parent 89484b77
...@@ -4,9 +4,9 @@ ...@@ -4,9 +4,9 @@
set -e set -e
# debug # debug
set -x # set -x
download_node() { download_and_install_node() {
version="$1" version="$1"
status "Downloading node $version" status "Downloading node $version"
...@@ -33,13 +33,15 @@ query_latest_version() { ...@@ -33,13 +33,15 @@ query_latest_version() {
| tail -n1 | tail -n1
} }
all_versions() { query_all_versions() {
curl -s http://nodejs.org/dist/ \ curl -s http://nodejs.org/dist/ \
| egrep -o '[0-9]+\.[0-9]+\.[0-9]+' \ | egrep -o '[0-9]+\.[0-9]+\.[0-9]+' \
| sort -u -k 1,1n -k 2,2n -k 3,3n -t . | sort -u -k 1,1n -k 2,2n -k 3,3n -t .
} }
function error() {
error() {
echo " ! $*" >&2 echo " ! $*" >&2
exit 1 exit 1
} }
......
...@@ -2,34 +2,88 @@ ...@@ -2,34 +2,88 @@
build_dir=$1 build_dir=$1
cache_dir=$2 cache_dir=$2
lp_dir=$(cd $(dirname $0); cd ..; pwd) bp_dir=$(cd $(dirname $0); cd ..; pwd)
stable_version="0.10.15" stable_version="0.10.17"
source $lp_dir/bin/common.sh source $bp_dir/bin/common.sh
# Output debug info on exit # Output debug info on exit
trap cat_npm_debug_log EXIT trap cat_npm_debug_log EXIT
download_node $stable_version status "Downloading node $stable_version"
download_and_install_node $stable_version
# cd $build_dir
# Is a node version specified in package.json? # Is a node version specified in package.json?
requested_version=$(cat $build_dir/package.json | node $lp_dir/vendor/json engines.node 2>/dev/null) requested_version=$(cat $build_dir/package.json | node $bp_dir/vendor/json engines.node 2>/dev/null)
# echo "requested_version: $requested_version"
# Give a warning if no node engine is specified
if ! test $requested_version; then
echo
echo "WARNING: No version of Node.js specified in package.json, see:" | indent
echo "https://devcenter.heroku.com/articles/nodejs-support#versions" | indent
echo
fi
if test $requested_version; then if test $requested_version; then
default_satisfies=$(node $lp_dir/vendor/semver/bin/semver $stable_version -r "$requested_version")
# Does the already-downloaded stable version of node satisfy the requested version?
default_satisfies=$(node $bp_dir/vendor/semver/bin/semver -v $stable_version -r "$requested_version" || echo "")
if ! test $default_satisfies; then if ! test $default_satisfies; then
download_node $requested_version status "Using node $stable_version"
else
# Find all available versions from nodejs.org/dist
available_versions=""
for version in query_all_versions; do
available_versions="${available_versions} -v \"${version}\""
done
# Determine which available versions satisfy the requested version
# https://github.com/isaacs/node-semver/blob/master/bin/semver
evaluated_versions=$(node $bp_dir/vendor/semver/bin/semver $available_versions -r "$requested_version" || echo "")
newest_matching_version=$("$evaluated_versions" | tail -n 1)
if test $newest_matching_version; then
status "Using node $newest_matching_version"
download_and_install_node $newest_matching_version
else
error "Requested node version ${requested_version} not found among available versions: ${available_versions}"
fi fi
fi
fi
# Configure cache directories
cache_store_dir="$cache_dir/node_modules/$NODE_VERSION/$NPM_VERSION"
cache_target_dir="$build_dir/node_modules"
# Restore node_modules cache
if [ -d $cache_store_dir ]; then
status "Restoring node_modules cache"
if [ -d $cache_target_dir ]; then
cp -r $cache_store_dir/* $cache_target_dir/
else
cp -r $cache_store_dir $cache_target_dir
fi
status "Pruning unused dependencies"
npm prune
fi fi
status "Installing dependencies with npm" # Install dependencies
npm prune status "Installing dependencies"
npm install --production npm install --production
npm rebuild npm rebuild
echo "Dependencies installed" | indent echo "Dependencies installed" | indent
# Cache node_modules for future builds
if [ -d $cache_target_dir ]; then
status "Caching node_modules for future builds"
rm -rf $cache_store_dir
mkdir -p $(dirname $cache_store_dir)
cp -r $cache_target_dir $cache_store_dir
fi
# Update the PATH
status "Building runtime environment" status "Building runtime environment"
mkdir -p $build_dir/.profile.d mkdir -p $build_dir/.profile.d
echo "export PATH=\"\$HOME/node/bin:$HOME/bin:\$HOME/node_modules/.bin:\$PATH\"" > $build_dir/.profile.d/nodejs.sh echo "export PATH=\"\$HOME/node/bin:$HOME/bin:\$HOME/node_modules/.bin:\$PATH\"" > $build_dir/.profile.d/nodejs.sh
\ No newline at end of file
...@@ -22,8 +22,7 @@ testDetectWithoutPackageJson() { ...@@ -22,8 +22,7 @@ testDetectWithoutPackageJson() {
testPackageJsonWithVersion() { testPackageJsonWithVersion() {
compile "package-json-version" compile "package-json-version"
assertNotCaptured "WARNING: No version of Node.js specified" assertNotCaptured "WARNING: No version of Node.js specified"
assertCaptured "Using Node.js version: 0.6.11" assertCaptured "Using node 0.10.17"
assertCaptured "Using npm version: 1.1.9"
assertCapturedSuccess assertCapturedSuccess
} }
...@@ -31,29 +30,27 @@ testPackageJsonWithoutVersion() { ...@@ -31,29 +30,27 @@ testPackageJsonWithoutVersion() {
compile "package-json-noversion" compile "package-json-noversion"
assertCaptured "WARNING: No version of Node.js specified" assertCaptured "WARNING: No version of Node.js specified"
assertCaptured "Using Node.js version: 0.10" assertCaptured "Using Node.js version: 0.10"
assertCaptured "Using npm version: 1.3"
assertCapturedSuccess assertCapturedSuccess
} }
testPackageJsonWithInvalidVersion() { testPackageJsonWithInvalidVersion() {
compile "package-json-invalidversion" compile "package-json-invalidversion"
assertCapturedError 1 "Requested engine npm version 1.1.5 does not" assertCapturedError 1 "Requested node version 5.0 not found"
}
testNothingCached() {
cache=$(mktmpdir)
compile "package-json-version" $cache
assertCapturedSuccess
assertEquals "0" "$(ls -1 $cache | wc -l)"
} }
testProfileCreated() { testProfileCreated() {
compile "package-json-version" compile "package-json-version"
assertCaptured "Building runtime environment" assertCaptured "Building runtime environment"
assertFile "export PATH=\"\$HOME/bin:\$HOME/node_modules/.bin:\$PATH\"" ".profile.d/nodejs.sh" assertFile "export PATH=\"\$HOME/node/bin:$HOME/bin:\$HOME/node_modules/.bin:\$PATH\"" ".profile.d/nodejs.sh"
assertCapturedSuccess assertCapturedSuccess
} }
testNodeModulesCached() {
cache=$(mktmpdir)
compile "node-modules-caching" $cache
assertEquals "1" "$(ls -1 $cache/node_modules/0.10.15/1.3.5 | wc -l)"
}
## utils ######################################## ## utils ########################################
pushd $(dirname 0) >/dev/null pushd $(dirname 0) >/dev/null
...@@ -78,7 +75,7 @@ COMPILE_DIR="" ...@@ -78,7 +75,7 @@ COMPILE_DIR=""
compile() { compile() {
COMPILE_DIR=$(mktmpdir) COMPILE_DIR=$(mktmpdir)
cp -r ${BASE}/test/$1/* ${COMPILE_DIR}/ cp -r ${BASE}/test/$1/* ${COMPILE_DIR}/
capture ${BASE}/bin/compile ${COMPILE_DIR} $2 capture ${BASE}/bin/compile ${COMPILE_DIR} ${2:-$(mktmpdir)}
} }
assertFile() { assertFile() {
......
{
"name": "myapp",
"version": "0.0.1",
"engines": {
"node": "0.10.15",
"npm": "1.3.5"
},
"dependencies": {
"express": "latest"
}
}
\ No newline at end of file
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
"version": "0.0.1", "version": "0.0.1",
"engines": { "engines": {
"node": "0.6.11", "node": "5.0"
"npm": "1.1.5"
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment