Permalink
Browse files

use semver.io for great success

  • Loading branch information...
zeke committed Oct 10, 2013
1 parent cdedbf0 commit 12020b35691fd91ecae09cb20b3a7d11b2654119
View
@@ -1,45 +1,3 @@
-#!/usr/bin/env bash
-
-# fail fast
-set -e
-
-# Uncomment the line below to enable debugging
-# set -x
-
-download_and_install_node() {
- version="$1"
- node_url="http://s3pository.heroku.com/node/v$version/node-v$version-linux-x64.tar.gz"
- curl $node_url -s -o - | tar xzf - -C $build_dir
- mkdir -p $build_dir/vendor
-
- # Remove node in case we're overwriting a previously-downloaded version
- rm -rf $build_dir/vendor/node
-
- mv $build_dir/node-v$version-linux-x64 $build_dir/vendor/node
- chmod +x $build_dir/vendor/node/bin/*
- PATH=$PATH:$build_dir/vendor/node/bin
-}
-
-query_stable_version() {
- curl -s http://nodejs.org/dist/ \
- | egrep -o '[0-9]+\.[0-9]*[02468]\.[0-9]+' \
- | sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
- | tail -n1
-}
-
-query_latest_version() {
- curl -s http://nodejs.org/dist/ \
- | egrep -o '[0-9]+\.[0-9]+\.[0-9]+' \
- | sort -u -k 1,1n -k 2,2n -k 3,3n -t . \
- | tail -n1
-}
-
-query_all_versions() {
- curl -s http://nodejs.org/dist/ \
- | egrep -o '[0-9]+\.[0-9]+\.[0-9]+' \
- | sort -u -k 1,1n -k 2,2n -k 3,3n -t .
-}
-
error() {
echo " ! $*" >&2
exit 1
@@ -49,6 +7,13 @@ status() {
echo "-----> $*"
}
+protip() {
+ echo
+ echo "PRO TIP: $*" | indent
+ echo "See https://devcenter.heroku.com/articles/nodejs-support"
+ echo
+}
+
# sed -l basically makes sed replace and buffer through stdin to stdout
# so you get updates while the command runs and dont wait for the end
# e.g. npm install | indent
@@ -60,6 +25,6 @@ indent() {
esac
}
-function cat_npm_debug_log() {
+cat_npm_debug_log() {
test -f $build_dir/npm-debug.log && cat $build_dir/npm-debug.log
}
View
@@ -1,34 +1,55 @@
#!/usr/bin/env bash
+# fail fast
+set -e
+
+# enable debugging
+# set -x
+
+# Configure directories
build_dir=$1
cache_basedir=$2
bp_dir=$(cd $(dirname $0); cd ..; pwd)
+# Load some convenience functions like status() echo(), indent
source $bp_dir/bin/common.sh
# Output npm debug info on error
trap cat_npm_debug_log ERR
-# Is a node version specified in package.json?
-semver_range=$(cat $build_dir/package.json | $bp_dir/vendor/json engines.node)
+# Look in package.json's engines.node field for a semver range
+semver_range=$(cat $build_dir/package.json | $bp_dir/vendor/jq -r .engines.node)
+
+# Recommend using semver ranges in a safe manner
+if [ "$semver_range" == "null" ]; then
+ protip "Specify a node version in package.json"
+ semver_range=""
+elif [ "$semver_range" == "*" ]; then
+ protip "Don't use semver ranges like *"
+elif [ ${semver_range:0:1} == ">" ]; then
+ protip "Don't use semver ranges starting with >"
+fi
-# Resolve node version using a service
-node_version=$(curl --silent https://node-semver-service.heroku.com/${semver_range})
+# Resolve node version using semver.io
+semver_url=http://semver.io/node/$semver_range
+node_version=$(curl --silent $semver_url)
-# Warn if engines.node is unspecified
-if ! test $semver_range; then
- echo
- echo "WARNING: No node version specified in package.json, see:" | indent
- echo "https://devcenter.heroku.com/articles/nodejs-support#versions" | indent
- echo
- status "Defaulting to latest stable node, v$node_version"
+if [ "$semver_range" == "" ]; then
+ status "Defaulting to latest stable node: $node_version"
+else
+ status "Resolved node version for $semver_range is $node_version"
fi
-# TODO: Warn if engines.node is "*" or ">foo"
-
-# Download and install node
+# Download node from Heroku's S3 mirror of nodejs.org/dist
status "Downloading and installing node v$node_version"
-download_and_install_node $node_version
+node_url="http://s3pository.heroku.com/node/v$node_version/node-v$node_version-linux-x64.tar.gz"
+curl $node_url -s -o - | tar xzf - -C $build_dir
+
+# Move node into ./vendor and make it executable
+mkdir -p $build_dir/vendor
+mv $build_dir/node-v$node_version-linux-x64 $build_dir/vendor/node
+chmod +x $build_dir/vendor/node/bin/*
+PATH=$PATH:$build_dir/vendor/node/bin
# Run subsequent node/npm commands from the build path
cd $build_dir
@@ -37,7 +58,7 @@ cd $build_dir
package_checksum=$(cat $build_dir/package.json | md5sum | awk '{print $1}')
cache_dir="$cache_basedir/$package_checksum"
-# Restore from cache package.json hasn't changed
+# Restore from cache if package.json hasn't changed
if test -d $cache_dir; then
status "package.json unchanged since last build"
status "Restoring node_modules from cache"
@@ -54,7 +75,7 @@ else
status "Caching node_modules for future builds"
rm -rf $cache_dir
mkdir -p $cache_dir
- mkdir -p $cache_dir/vendor
+ # mkdir -p $cache_dir/vendor
test -d $build_dir/node_modules && cp -r $build_dir/node_modules $cache_dir/
# cp -r $build_dir/vendor/node $cache_dir/vendor/
fi
View
@@ -2,7 +2,7 @@
# See CONTRIBUTING.md for info on running these tests.
testDetectWithPackageJson() {
- detect "package-json-stable-version"
+ detect "stable-node"
assertCaptured "Node.js"
assertCapturedSuccess
}
@@ -12,44 +12,59 @@ testDetectWithoutPackageJson() {
assertCapturedError 1 ""
}
-testPackageJsonWithoutVersion() {
- compile "package-json-noversion"
- assertCaptured "WARNING: No node version specified"
+testNoVersion() {
+ compile "no-version"
+ assertCaptured "PRO TIP: Specify a node version in package.json"
assertCaptured "Defaulting to latest stable node"
assertCapturedSuccess
}
-testPackageJsonWithStableVersion() {
- compile "package-json-stable-version"
- assertNotCaptured "WARNING: No node version specified"
- assertCaptured "satisfies engines.node"
- assertCapturedSuccess
-}
+testDangerousRangeStar() {
+ compile "dangerous-range-star"
+ assertCaptured "PRO TIP: Don't use semver ranges like *"
-testPackageJsonWithUnstableVersion() {
- compile "package-json-unstable-version"
- assertCaptured "Downloading and installing node v0.11"
assertCapturedSuccess
}
-testPackageJsonWithInvalidVersion() {
- compile "package-json-invalidversion"
- assertCapturedError 1 "not found among available versions"
+testDangerousRangeGreaterThan() {
+ compile "dangerous-range-greater-than"
+ assertCaptured "PRO TIP: Don't use semver ranges starting with >"
+ assertCaptured "Resolved node version for >"
+ assertCapturedSuccess
}
-testProfileCreated() {
- compile "package-json-stable-version"
- assertCaptured "Building runtime environment"
- assertFile "export PATH=\"\$HOME/vendor/node/bin:$HOME/bin:\$HOME/node_modules/.bin:\$PATH\"" ".profile.d/nodejs.sh"
+testStableVersion() {
+ compile "stable-node"
+ assertNotCaptured "PRO TIP"
+ assertCaptured "Resolved node version"
assertCapturedSuccess
}
-testNodeModulesCached() {
- cache=$(mktmpdir)
- compile "node-modules-caching" $cache
- assertCaptured "Caching node"
- assertEquals "1" "$(ls -1 $cache/0.10.18 | wc -l)"
-}
+# testUnstableVersion() {
+# compile "unstable-version"
+# assertCaptured "Resolved node version for >0.11.0 is 0.11"
+
+# assertCapturedSuccess
+# }
+
+# testInvalidVersion() {
+# compile "invalid-node-version"
+# assertCapturedError 1 "not found among available versions"
+# }
+
+# testProfileCreated() {
+# compile "stable-node"
+# assertCaptured "Building runtime environment"
+# assertFile "export PATH=\"\$HOME/vendor/node/bin:$HOME/bin:\$HOME/node_modules/.bin:\$PATH\"" ".profile.d/nodejs.sh"
+# assertCapturedSuccess
+# }
+
+# testNodeModulesCached() {
+# cache=$(mktmpdir)
+# compile "caching" $cache
+# assertCaptured "Caching node"
+# assertEquals "1" "$(ls -1 $cache/0.10.18 | wc -l)"
+# }
# Pending
File renamed without changes.
File renamed without changes.
@@ -7,6 +7,6 @@
"url" : "http://github.com/example/example.git"
},
"engines": {
- "node": ">0.10.0"
+ "node": ">0.4"
}
}
@@ -0,0 +1,12 @@
+{
+ "name": "node-buildpack-test-app",
+ "version": "0.0.1",
+ "description": "node buildpack integration test app",
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/example/example.git"
+ },
+ "engines": {
+ "node": "*"
+ }
+}
@@ -0,0 +1 @@
+A fake README, to keep npm from polluting stderr.
@@ -0,0 +1,12 @@
+{
+ "name": "node-buildpack-test-app",
+ "version": "0.0.1",
+ "description": "node buildpack integration test app",
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/example/example.git"
+ },
+ "engines": {
+ "node": "~0.10.0"
+ }
+}
@@ -0,0 +1 @@
+A fake README, to keep npm from polluting stderr.
View
BIN vendor/jq
Binary file not shown.
Oops, something went wrong.

0 comments on commit 12020b3

Please sign in to comment.