2013-03-19 01:39:59 +08:00
|
|
|
/*
|
|
|
|
* grunt-contrib-watch
|
|
|
|
* http://gruntjs.com/
|
|
|
|
*
|
2016-01-05 09:17:42 +08:00
|
|
|
* Copyright (c) 2016 "Cowboy" Ben Alman, contributors
|
2013-03-19 01:39:59 +08:00
|
|
|
* Licensed under the MIT license.
|
|
|
|
*/
|
2013-02-14 01:47:48 +08:00
|
|
|
|
2013-03-19 01:39:59 +08:00
|
|
|
'use strict';
|
2013-02-14 01:47:48 +08:00
|
|
|
|
2013-03-19 01:39:59 +08:00
|
|
|
module.exports = function(grunt) {
|
2013-02-14 01:47:48 +08:00
|
|
|
|
2013-03-19 01:39:59 +08:00
|
|
|
// Create a TaskRun on a target
|
2013-07-20 12:18:21 +08:00
|
|
|
function TaskRun(target) {
|
2013-03-19 01:39:59 +08:00
|
|
|
this.name = target.name || 0;
|
|
|
|
this.files = target.files || [];
|
2014-03-20 01:37:10 +08:00
|
|
|
this._getConfig = target._getConfig;
|
2013-04-20 01:46:22 +08:00
|
|
|
this.options = target.options;
|
2013-03-19 01:39:59 +08:00
|
|
|
this.startedAt = false;
|
|
|
|
this.spawned = null;
|
2013-05-01 04:08:10 +08:00
|
|
|
this.changedFiles = Object.create(null);
|
2014-02-24 23:54:59 +08:00
|
|
|
this.spawnTaskFailure = false;
|
|
|
|
this.livereloadOnError = true;
|
|
|
|
if (typeof this.options.livereloadOnError !== 'undefined') {
|
|
|
|
this.livereloadOnError = this.options.livereloadOnError;
|
2013-05-28 02:39:07 +08:00
|
|
|
}
|
2013-03-19 01:39:59 +08:00
|
|
|
}
|
2013-02-21 13:50:07 +08:00
|
|
|
|
2014-11-04 09:38:53 +08:00
|
|
|
var getErrorCount = function() {
|
2014-02-24 23:54:59 +08:00
|
|
|
if (typeof grunt.fail.forever_warncount !== 'undefined') {
|
|
|
|
return grunt.fail.forever_warncount + grunt.fail.forever_errorcount;
|
|
|
|
} else {
|
|
|
|
return grunt.fail.warncount + grunt.fail.errorcount;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-03-19 01:39:59 +08:00
|
|
|
// Run it
|
|
|
|
TaskRun.prototype.run = function(done) {
|
2013-07-19 11:06:27 +08:00
|
|
|
var self = this;
|
2013-02-14 01:47:48 +08:00
|
|
|
|
2013-03-19 01:39:59 +08:00
|
|
|
// Dont run if already running
|
|
|
|
if (self.startedAt !== false) { return; }
|
2013-02-14 01:47:48 +08:00
|
|
|
|
2013-05-08 03:17:21 +08:00
|
|
|
// Start this task run
|
2013-03-19 01:39:59 +08:00
|
|
|
self.startedAt = Date.now();
|
2013-05-08 03:17:21 +08:00
|
|
|
|
2014-03-20 01:37:10 +08:00
|
|
|
// reset before each run
|
2014-02-24 23:54:59 +08:00
|
|
|
self.spawnTaskFailure = false;
|
|
|
|
self.errorsAndWarningsCount = getErrorCount();
|
|
|
|
|
2014-03-20 01:37:10 +08:00
|
|
|
// pull the tasks here in case they were changed by a watch event listener
|
|
|
|
self.tasks = self._getConfig('tasks') || [];
|
|
|
|
if (typeof self.tasks === 'string') {
|
2014-02-24 23:54:59 +08:00
|
|
|
self.tasks = [self.tasks];
|
|
|
|
}
|
|
|
|
|
2013-05-08 03:17:21 +08:00
|
|
|
// If no tasks just call done to trigger potential livereload
|
|
|
|
if (self.tasks.length < 1) { return done(); }
|
|
|
|
|
2013-07-12 03:42:17 +08:00
|
|
|
if (self.options.spawn === false || self.options.nospawn === true) {
|
2013-03-19 01:39:59 +08:00
|
|
|
grunt.task.run(self.tasks);
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
self.spawned = grunt.util.spawn({
|
2013-02-27 08:24:51 +08:00
|
|
|
// Spawn with the grunt bin
|
|
|
|
grunt: true,
|
|
|
|
// Run from current working dir and inherit stdio from process
|
2013-03-19 01:39:59 +08:00
|
|
|
opts: {
|
2013-06-13 10:41:07 +08:00
|
|
|
cwd: self.options.cwd.spawn,
|
2013-03-19 01:39:59 +08:00
|
|
|
stdio: 'inherit',
|
|
|
|
},
|
2013-07-19 11:06:27 +08:00
|
|
|
// Run grunt this process uses, append the task to be run and any cli options
|
|
|
|
args: self.tasks.concat(self.options.cliArgs || []),
|
2013-02-27 08:24:51 +08:00
|
|
|
}, function(err, res, code) {
|
2014-02-24 23:54:59 +08:00
|
|
|
self.spawnTaskFailure = (code !== 0);
|
2013-10-16 03:55:24 +08:00
|
|
|
if (self.options.interrupt !== true || (code !== 130 && code !== 1)) {
|
2013-07-13 15:22:44 +08:00
|
|
|
// Spawn is done
|
|
|
|
self.spawned = null;
|
|
|
|
done();
|
|
|
|
}
|
2013-02-27 08:24:51 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2013-02-14 01:47:48 +08:00
|
|
|
|
2013-05-01 04:08:10 +08:00
|
|
|
// When the task run has completed
|
2013-03-19 01:39:59 +08:00
|
|
|
TaskRun.prototype.complete = function() {
|
2013-04-17 12:41:03 +08:00
|
|
|
var time = Date.now() - this.startedAt;
|
2013-03-19 01:39:59 +08:00
|
|
|
this.startedAt = false;
|
|
|
|
if (this.spawned) {
|
|
|
|
this.spawned.kill('SIGINT');
|
|
|
|
this.spawned = null;
|
2013-02-27 08:24:51 +08:00
|
|
|
}
|
2014-02-24 23:54:59 +08:00
|
|
|
|
|
|
|
var taskFailed = this.spawnTaskFailure || (getErrorCount() > this.errorsAndWarningsCount);
|
|
|
|
this.errorsAndWarningsCount = getErrorCount();
|
|
|
|
|
|
|
|
// Trigger livereload if necessary
|
|
|
|
if (this.livereload && (this.livereloadOnError || !taskFailed)) {
|
2013-05-01 04:08:10 +08:00
|
|
|
this.livereload.trigger(Object.keys(this.changedFiles));
|
2013-08-27 00:29:26 +08:00
|
|
|
this.changedFiles = Object.create(null);
|
2013-05-01 04:08:10 +08:00
|
|
|
}
|
2013-03-19 01:39:59 +08:00
|
|
|
return time;
|
2013-02-27 08:24:51 +08:00
|
|
|
};
|
2013-02-14 01:47:48 +08:00
|
|
|
|
2013-03-19 01:39:59 +08:00
|
|
|
return TaskRun;
|
2013-02-24 03:32:24 +08:00
|
|
|
};
|