Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add npm-shrinkwrap.json cachebusting
  • Loading branch information
zeke committed Oct 23, 2013
1 parent 83104b4 commit eae9a9d
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 29 deletions.
50 changes: 24 additions & 26 deletions bin/compile
@@ -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
Expand Down Expand Up @@ -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"
Expand Down
12 changes: 9 additions & 3 deletions bin/test
Expand Up @@ -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
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -96,8 +104,6 @@ testNodeModulesCached() {
# assertCapturedError 1 "not found among available versions"
# }



# Utils

pushd $(dirname 0) >/dev/null
Expand Down
1 change: 1 addition & 0 deletions test/shrinkwrap/README.md
@@ -0,0 +1 @@
A fake README, to keep npm from polluting stderr.
1 change: 1 addition & 0 deletions test/shrinkwrap/node_modules/euclidean-distance/.npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 38 additions & 0 deletions test/shrinkwrap/node_modules/euclidean-distance/README.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions test/shrinkwrap/node_modules/euclidean-distance/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions test/shrinkwrap/node_modules/euclidean-distance/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions test/shrinkwrap/node_modules/euclidean-distance/test/indexTest.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/shrinkwrap/npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test/shrinkwrap/package.json
@@ -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"
}
}
3 changes: 3 additions & 0 deletions test/stable-node/package.json
Expand Up @@ -6,6 +6,9 @@
"type" : "git",
"url" : "http://github.com/example/example.git"
},
"dependencies": {
"hashish": "*"
},
"engines": {
"node": "~0.10.0"
}
Expand Down

0 comments on commit eae9a9d

Please sign in to comment.