2013-02-14 01:47:48 +08:00
|
|
|
var grunt = require('grunt');
|
|
|
|
|
|
|
|
var taskrun = module.exports = {
|
|
|
|
waiting: 'Waiting...',
|
|
|
|
cliArgs: null,
|
|
|
|
nameArgs: null,
|
2013-02-20 03:53:15 +08:00
|
|
|
changedFiles: Object.create(null),
|
|
|
|
startedAt: false,
|
2013-02-14 01:47:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Do this when watch has completed
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.completed = function completed() {
|
2013-02-14 01:47:48 +08:00
|
|
|
grunt.log.writeln('').write(String(
|
|
|
|
'Completed in ' +
|
2013-02-20 03:53:15 +08:00
|
|
|
Number((Date.now() - taskrun.startedAt) / 1000).toFixed(2) +
|
2013-02-14 01:47:48 +08:00
|
|
|
's at ' +
|
|
|
|
(new Date()).toString()
|
|
|
|
).cyan + ' - ' + taskrun.waiting);
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.startedAt = false;
|
|
|
|
};
|
2013-02-14 01:47:48 +08:00
|
|
|
|
|
|
|
// Do this when watch has been triggered
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.triggered = function triggered() {
|
2013-02-14 01:47:48 +08:00
|
|
|
grunt.log.ok();
|
|
|
|
Object.keys(taskrun.changedFiles).forEach(function(filepath) {
|
|
|
|
// Log which file has changed, and how.
|
|
|
|
grunt.log.ok('File "' + filepath + '" ' + taskrun.changedFiles[filepath] + '.');
|
|
|
|
});
|
|
|
|
// Reset changedFiles
|
|
|
|
taskrun.changedFiles = Object.create(null);
|
2013-02-20 03:53:15 +08:00
|
|
|
};
|
2013-02-14 01:47:48 +08:00
|
|
|
|
|
|
|
// Keep track of spawned processes
|
|
|
|
var spawned = Object.create(null);
|
|
|
|
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.spawn = grunt.util._.debounce(function spawn(id, tasks, options, done) {
|
2013-02-14 01:47:48 +08:00
|
|
|
// If interrupted, reset the spawned for a target
|
|
|
|
if (options.interrupt && typeof spawned[id] === 'object') {
|
|
|
|
grunt.log.writeln('').write('Previously spawned task has been interrupted...'.yellow);
|
|
|
|
spawned[id].kill('SIGINT');
|
|
|
|
delete spawned[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only spawn one at a time unless interrupt is specified
|
|
|
|
if (!spawned[id]) {
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.triggered();
|
2013-02-14 01:47:48 +08:00
|
|
|
|
|
|
|
// Spawn the tasks as a child process
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.startedAt = Date.now();
|
2013-02-14 01:47:48 +08:00
|
|
|
spawned[id] = grunt.util.spawn({
|
|
|
|
// Spawn with the grunt bin
|
|
|
|
grunt: true,
|
|
|
|
// Run from current working dir and inherit stdio from process
|
|
|
|
opts: {cwd: process.cwd(), stdio: 'inherit'},
|
|
|
|
// Run grunt this process uses, append the task to be run and any cli options
|
|
|
|
args: grunt.util._.union(tasks, taskrun.cliArgs)
|
|
|
|
}, function(err, res, code) {
|
|
|
|
// Spawn is done
|
|
|
|
delete spawned[id];
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.completed();
|
2013-02-14 01:47:48 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 250);
|
|
|
|
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.nospawn = grunt.util._.debounce(function nospawn(id, tasks, options, done) {
|
2013-02-14 01:47:48 +08:00
|
|
|
// todo: add interrupt
|
|
|
|
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.triggered();
|
2013-02-14 01:47:48 +08:00
|
|
|
|
|
|
|
// Mark tasks to run and enqueue this task afterward
|
2013-02-20 03:53:15 +08:00
|
|
|
taskrun.startedAt = Date.now();
|
2013-02-14 01:47:48 +08:00
|
|
|
grunt.task.run(tasks).mark().run(taskrun.nameArgs);
|
|
|
|
|
|
|
|
// Finish the task
|
|
|
|
done();
|
|
|
|
}, 250);
|