Skip to content

Commit

Permalink
Merge pull request #246 from wrangr/pack_hoodie
Browse files Browse the repository at this point in the history
Polish up pack_hoodie... fixes a few teething problems
  • Loading branch information
svnlto committed Mar 1, 2014
2 parents 1e140ee + 830d06b commit d0eeee1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
35 changes: 30 additions & 5 deletions lib/helpers/pack_hoodie.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var fs = require('fs');
var path = require('path');
var browserify = require('browserify');
var watch = require('watch');

var hoodieUtils = require('../utils/hconsole');

Expand All @@ -18,15 +19,34 @@ var bundleConcat = path.join(__dirname, 'hoodie_bundle_concat.js');
// We use `cache` to keep in memory copy of the hoodie.js bundle.
var cache;

// Keep track of watched files to make sure we don't watch the same path more
// than once.
var watchedPaths = [];

// When file system watcher emits an update event we invalidate the cache, so
// the bundle will be recreated on the next request.
function update() {
function update(f, curr, prev) {
if (typeof f === 'object' && prev === null && curr === null) {
// Finished walking the tree!
return;
}
cache = null;
}

// Watch file/directory and invoke `update` when changes happen.
function watch(file) {
fs.watchFile(file, update);
function watchPath(root) {
// If file is already being watched we don't need to do anything.
if (watchedPaths.indexOf(root) >= 0) { return; }
watchedPaths.push(root);
fs.stat(root, function (err, stats) {
if (err) { return console.error(err); }
if (stats.isDirectory()) {
return watch.watchTree(root, { ignoreDotFiles: true }, update);
}
fs.watchFile(root, function (curr, prev) {
update(root, curr, prev);
});
});
}

// Get absolute paths to the files with the frontend javascript for all
Expand Down Expand Up @@ -55,7 +75,7 @@ function concatPlugins(plugins) {
}

// Watch `hoodie.js` files for changes.
watch(hoodiePath);
watchPath(hoodiePath);


module.exports = function (config) {
Expand All @@ -66,7 +86,7 @@ module.exports = function (config) {
var plugins = getPlugins(config);

// Watch plugins for changes...
plugins.forEach(watch);
plugins.forEach(watchPath);

// Create the `bundleConcat` that we'll use to create the `browserify` bundle.
fs.writeFileSync(bundleConcat, bundleTmpl + concatPlugins(plugins));
Expand All @@ -80,6 +100,11 @@ module.exports = function (config) {
if (err) { return console.error(err); }
// Store successful bundle in cache.
cache = bundle;
// Clean up! Remove dynamically generated file with concatenated scripts
// once we are done building the bundle.
fs.unlink(bundleConcat, function (err) {
if (err) { return console.log(err); }
});
});

};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"http-proxy": "1.0.2",
"lodash": "^2.4.1",
"browserify": "^3.30.2",
"combine-streams": "0.0.4"
"watch": "^0.9.0"
},
"scripts": {
"start": "bin/start",
Expand Down

0 comments on commit d0eeee1

Please sign in to comment.