Skip to content

Commit

Permalink
Watch more files
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Aug 10, 2012
1 parent 2a5d05a commit ca04075
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin/square
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ program.on('--watch', function watching (live) {
});

// start watching for file changes in the given directory
watch(files, extensions, function update (err, files) {
watch.call(square, files, extensions, function update (err, files) {
if (err) return square.logger.error(
'Watcher error %s, canceling watch operations on %s'
, err.message, files
Expand Down
67 changes: 66 additions & 1 deletion lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ var canihaz = require('canihaz')('square')

function watching(files, extensions, fn) {
var processors = require('./pre-process')
, square = this
, changes = []
, limited;

Expand Down Expand Up @@ -81,6 +82,70 @@ function watching(files, extensions, fn) {

notifier.on('change', filter);
notifier.on('error', fn);

// now that we have started watching our default files we can start searching
// for other files that might trigger a change for these build
var finder = require('findit').find(square.package.path)
, extras = [];

/**
* We have found a new file in the directory that matches our extension, watch
* it for changes.
*
* @param {String} file
* @api private
*/

finder.on('file', function found(file) {
var trimmed = file.slice(1)
, extension = path.extname(trimmed).slice(1);

// make sure we are not watching file already
if (~files.indexOf(file) || ~files.indexOf(trimmed)) return;

// don't watch node_module folders.. ever.. we might want to look at the
// possible gitignore or .npmignore files to see what other files need to be
// black listen, but that is more a @TODO item
if (~file.indexOf('node_modules/')) return;

// make sure the file isn't in a dot directory as well..
var paths = trimmed.split('/');
if (paths.some(function some(path) { return path[0] === '.'; })) return;

// in addition to that, it shouldn't really be one of our output files.. or
// we will be creating a watching loop as we will be changing that file
// during the write
var distributions = []
, possibles = extensions.length ? extensions : [extension];

possibles.forEach(function forEach(ext) {
var dummy = { content: '', group: 'squared', extension: ext }
, dist = square.package.configuration.dist
, dev = square.template(dist.dev, square.tag(dummy, 'dev'))
, min = square.template(dist.min, square.tag(dummy, 'min'));

distributions.push(dev, min);
});

if (~distributions.indexOf(file)) return;

// now the last check is that we actually support this file ;D
if (!extensions.length || ~extensions.indexOf(path.extname(file).slice(1))) {
extras.push(file);
}
});

/**
* We are done scanning the directory, see if we need to push out more files.
*
* @api private
*/

finder.on('end', function end() {
if (!extras.length) return;

notifier.add(extras);
});
}

/**
Expand All @@ -105,7 +170,7 @@ watching.live = function live (port) {
io = io.listen(port, {
'log level': 0 // socket.io spams like whore, silence it
, 'browser client etag': true // cache, but with etags for quick refresh
, 'browser client gzip': true // minimal overhead for requests
, 'browser client gzip': false // minimal overhead for requests
, 'resource': '/live' // fancy pancy resources
});

Expand Down

0 comments on commit ca04075

Please sign in to comment.