Add atBegin option to run tasks when watcher starts. Closes GH-147.

This commit is contained in:
John K. Paul 2013-07-01 18:36:15 -04:00 committed by Kyle Robinson Young
parent bbb41575d5
commit bfe7a2a1fb
7 changed files with 66 additions and 2 deletions

View File

@ -128,6 +128,12 @@ Default: true
This is *only a task level option* and cannot be configured per target. By default the watch task will duck punch `grunt.fatal` and `grunt.warn` to try and prevent them from exiting the watch process. If you don't want `grunt.fatal` and `grunt.warn` to be overridden set the `forever` option to `false`.
#### options.atBegin
Type: `Boolean`
Default: false
This option will trigger the run of each specified task at startup of the watcher.
#### options.livereload
Type: `Boolean|Number|Object`
Default: false
@ -365,4 +371,4 @@ Spawning does cause a performance hit (usually 500ms for most environments). It
Task submitted by [Kyle Robinson Young](http://dontkry.com)
*This file was generated on Fri Jul 05 2013 09:42:30.*
*This file was generated on Sun Jul 07 2013 14:50:23.*

View File

@ -100,6 +100,12 @@ Default: true
This is *only a task level option* and cannot be configured per target. By default the watch task will duck punch `grunt.fatal` and `grunt.warn` to try and prevent them from exiting the watch process. If you don't want `grunt.fatal` and `grunt.warn` to be overridden set the `forever` option to `false`.
## options.atBegin
Type: `Boolean`
Default: false
This option will trigger the run of each specified task at startup of the watcher.
## options.livereload
Type: `Boolean|Number|Object`
Default: false

View File

@ -87,7 +87,21 @@ module.exports = function(grunt) {
}
// Return the targets normalized
return self._getTargets(self.name);
var targets = self._getTargets(self.name);
// Check whether target's tasks should run at start w/ atBegin option
if (self.running === false) {
self.queue = targets.filter(function(tr) {
return tr.options.atBegin === true && tr.tasks.length > 0;
}).map(function(tr) {
return tr.name;
});
if (self.queue.length > 0) {
self.run();
}
}
return targets;
};
// Normalize targets from config

View File

@ -73,6 +73,7 @@ module.exports = function(grunt) {
var targets = taskrun.init(name, {
interrupt: false,
nospawn: false,
atBegin: false,
event: ['all'],
target: target,
});

22
test/fixtures/atBegin/Gruntfile.js vendored Normal file
View File

@ -0,0 +1,22 @@
module.exports = function(grunt) {
'use strict';
grunt.initConfig({
echo: {
one: { message: 'one has changed' }
},
watch: {
options:{
atBegin: true
},
one: {
files: ['lib/one.js', 'Gruntfile.js'],
tasks: 'echo:one',
}
}
});
// Load the echo task
grunt.loadTasks('../tasks');
// Load this watch task
grunt.loadTasks('../../../tasks');
grunt.registerTask('default', ['echo']);
};

1
test/fixtures/atBegin/lib/one.js vendored Normal file
View File

@ -0,0 +1 @@
var test = true;

View File

@ -26,6 +26,20 @@ exports.watchConfig = {
cleanUp();
done();
},
atBegin: function(test) {
test.expect(1);
var cwd = path.resolve(fixtures, 'atBegin');
var assertWatch = helper.assertTask(['watch', '--debug'], {cwd:cwd});
assertWatch(function() {
// noop. Does not modify any watched files.
}, function(result) {
helper.verboseLog(result);
var firstIndex = result.indexOf('one has changed');
test.ok(firstIndex !== -1, 'Watch should have fired even though no file was changed.');
test.done();
});
},
oneTarget: function(test) {
test.expect(2);
var cwd = path.resolve(fixtures, 'oneTarget');