Skip to content

Commit

Permalink
Updated --watch to be very smart
Browse files Browse the repository at this point in the history
  • Loading branch information
davglass committed Dec 12, 2012
1 parent 75a0c3e commit 2bd142b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 21 deletions.
88 changes: 68 additions & 20 deletions lib/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var log = require('./log'),
shifter = require('./'),
util = require('./util'),
timer = require('timethat'),
findit = require('findit'),
dirs = [
'assets',
'css',
Expand All @@ -26,20 +27,21 @@ var build = function (cwd, callback) {
return false;
}
buildArgs.unshift(util.shifter);
var child = spawn(process.execPath, buildArgs, {
cwd: cwd
}), start = new Date();

buildRunning = true;

if (!quiet) {
child.stdout.on('data', function (data) {
console.log(data.toString().trim());
});
var stds = [process.stdin, process.stdout, process.stderr], child, start = new Date();

if (quiet) {
buildArgs.push('--quiet');
}
child.stderr.on('data', function (data) {
log.err(data.toString());

child = spawn(process.execPath, buildArgs, {
cwd: cwd,
stdio: stds
});

buildRunning = true;

child.on('exit', function (code) {
buildRunning = false;
var end = new Date();
Expand All @@ -64,6 +66,15 @@ var changed = function (curr, prev) {
if ((curr && prev) && curr.size !== prev.size) {
ret = true;
}

if ((curr && !prev) || (!curr && prev)) { //new file or removed file
ret = true;
}

if (curr && prev && (curr.mtime > prev.mtime)) {
ret = true;
}

return ret;
};

Expand All @@ -73,26 +84,52 @@ var shorten = function (file) {

var handler = function (file, curr, prev) {
var ext = path.extname(file),
dir = path.dirname(file),
name = shorten(file);

if (usableExts[ext]) {
if (changed(curr, prev)) {
log.info(name + ' changed, shifting');
if (name.indexOf('./meta') === 0) {
log.info('meta changed, shifting yui');
build(path.join(shifter.cwd(), '../yui'));
} else {
log.info('shifting here');
build(shifter.cwd());
}
util.find(dir, 'build.json', function(err, json) {
var dir = path.dirname(json);
if (name.indexOf('./meta') === 0) {
log.info('meta changed, shifting yui');
build(path.join(dir, '../yui'));
} else {
log.info('shifting here: ' + dir);
build(dir);
}
});
}
}
};

var scan = function(options) {
log.info('no build.json file found, racing to see if we can find one');
var finder = findit(shifter.cwd()),
mods = [];

finder.on('path', function(file) {
if (path.basename(file) === 'build.json') {
mods.push(path.basename(path.dirname(file)));
}
});

finder.on('end', function() {
if (mods.length) {
log.info('racing complete, found ' + mods.length + ' modules to watch! you may code now, i\'ll build them when they change');
//log.info('modules: ' + mods.join(', '));
options.modules = mods;
exports.start(options);
} else {
log.bail('failed to locate any build.json files under this directory');
}
});
};

exports.start = function (options) {
log.info('shifting the first time for you..');
quiet = true;
build(shifter.cwd(), function () {
var start = function() {
log.info('watching for shifts in ' + dirs.join(', '));

quiet = options.quiet;
Expand Down Expand Up @@ -124,6 +161,17 @@ exports.start = function (options) {
monitor.on(event, handler);
});
});
});
};

if (!options.modules) {
if (!util.existsSync(path.join(shifter.cwd(), 'build.json'))) {
return scan(options);
}
log.info('shifting the first time for you..');
build(shifter.cwd(), start);
} else {
start();
}


};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"istanbul": "~0.1.8",
"which": "*",
"yuglify": "~0.1.0",
"timethat": "~0.0.1"
"timethat": "~0.0.1",
"findit": "~0.1.2"
},
"devDependencies": {
"selleck": "*",
Expand Down

0 comments on commit 2bd142b

Please sign in to comment.