Permalink
Browse files

add npm-shrinkwrap.json cachebusting

  • Loading branch information...
1 parent 83104b4 commit eae9a9df54872d22b7efbad28f540f40999abc66 @zeke zeke committed Oct 23, 2013
View
@@ -1,15 +1,15 @@
#!/usr/bin/env bash
set -e # fail fast
-set -o pipefail # don't ignore piped exit codes
+set -o pipefail # don't ignore exit codes when piping output
# set -x # enable debugging
# Configure directories
build_dir=$1
cache_basedir=$2
bp_dir=$(cd $(dirname $0); cd ..; pwd)
-# Load some convenience functions like status() echo(), indent
+# Load some convenience functions like status() echo(), indent()
source $bp_dir/bin/common.sh
# Output npm debug info on error
@@ -54,37 +54,35 @@ PATH=$PATH:$build_dir/vendor/node/bin
# Run subsequent node/npm commands from the build path
cd $build_dir
-# Configure cache directory
-package_checksum=$(cat $build_dir/package.json | md5sum | awk '{print $1}')
-cache_dir="$cache_basedir/$package_checksum"
+if test -f $build_dir/npm-shrinkwrap.json; then
+ # Use npm-shrinkwrap.json's checksum as the cachebuster
+ status "Found npm-shrinkwrap.json"
+ shrinkwrap_checksum=$(cat $build_dir/npm-shrinkwrap.json | md5sum | awk '{print $1}')
+ cache_dir="$cache_basedir/$shrinkwrap_checksum"
+ test -d $cache_dir && status "npm-shrinkwrap.json unchanged since last build"
+else
+ # Fall back to package.json as the cachebuster.
+ protip "Use npm shrinkwrap to lock down dependency versions"
+ package_json_checksum=$(cat $build_dir/package.json | md5sum | awk '{print $1}')
+ cache_dir="$cache_basedir/$package_json_checksum"
+ test -d $cache_dir && status "package.json unchanged since last build"
+fi
-# 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"
test -d $cache_dir/node_modules && cp -r $cache_dir/node_modules $build_dir/
+fi
- # If any scripts are defined in package.json, trigger them.
- # https://npmjs.org/doc/misc/npm-scripts.html
- hook_scripts=$(cat $build_dir/package.json | $bp_dir/vendor/jq -r .scripts)
- if [ "$hook_scripts" != "null" ]; then
- status "Running npm install to trigger script hooks"
- npm install --production | indent
- fi
-
-else
-
- status "Rebuilding dependencies"
- npm rebuild | indent
+status "Installing dependencies"
+npm install --production | indent
- status "Installing dependencies"
- npm install --production | indent
+status "Pruning unused dependencies"
+npm prune | indent
- status "Caching node_modules for future builds"
- rm -rf $cache_dir
- mkdir -p $cache_dir
- test -d $build_dir/node_modules && cp -r $build_dir/node_modules $cache_dir/
-fi
+status "Caching node_modules for future builds"
+rm -rf $cache_dir
+mkdir -p $cache_dir
+test -d $build_dir/node_modules && cp -r $build_dir/node_modules $cache_dir/
# Update the PATH
status "Building runtime environment"
View
@@ -37,7 +37,8 @@ testDangerousRangeGreaterThan() {
testStableVersion() {
compile "stable-node"
- assertNotCaptured "PRO TIP"
+ assertNotCaptured "PRO TIP: Avoid using semver"
+ assertNotCaptured "PRO TIP: Specify"
assertCaptured "Resolved node version"
assertCapturedSuccess
}
@@ -68,6 +69,13 @@ testNodeModulesCached() {
assertEquals "1" "$(ls -1 $cache/ | wc -l)"
}
+testShrinkwrap() {
+ compile "shrinkwrap"
+ assertCaptured "Found npm-shrinkwrap.json"
+ assertNotCaptured "PRO TIP: Use npm shrinkwrap"
+ assertCapturedSuccess
+}
+
# Pending Tests
# testNodeBinariesAddedToPath() {
@@ -96,8 +104,6 @@ testNodeModulesCached() {
# assertCapturedError 1 "not found among available versions"
# }
-
-
# Utils
pushd $(dirname 0) >/dev/null
@@ -0,0 +1 @@
+A fake README, to keep npm from polluting stderr.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
@@ -0,0 +1,15 @@
+{
+ "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"
+ },
+ "dependencies": {
+ "euclidean-distance": "*"
+ },
+ "engines": {
+ "node": "~0.10.0"
+ }
+}
@@ -6,6 +6,9 @@
"type" : "git",
"url" : "http://github.com/example/example.git"
},
+ "dependencies": {
+ "hashish": "*"
+ },
"engines": {
"node": "~0.10.0"
}

0 comments on commit eae9a9d

Please sign in to comment.