From bfe7a2a1fbdc1e1f0d832e9230f077fe563abc7e Mon Sep 17 00:00:00 2001 From: "John K. Paul" Date: Mon, 1 Jul 2013 18:36:15 -0400 Subject: [PATCH] Add atBegin option to run tasks when watcher starts. Closes GH-147. --- README.md | 8 +++++++- docs/watch-options.md | 6 ++++++ tasks/lib/taskrunner.js | 16 +++++++++++++++- tasks/watch.js | 1 + test/fixtures/atBegin/Gruntfile.js | 22 ++++++++++++++++++++++ test/fixtures/atBegin/lib/one.js | 1 + test/tasks/watch_test.js | 14 ++++++++++++++ 7 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/atBegin/Gruntfile.js create mode 100644 test/fixtures/atBegin/lib/one.js diff --git a/README.md b/README.md index f6b5dec..1e8d36b 100644 --- a/README.md +++ b/README.md @@ -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.* diff --git a/docs/watch-options.md b/docs/watch-options.md index ec1c725..5ac86df 100644 --- a/docs/watch-options.md +++ b/docs/watch-options.md @@ -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 diff --git a/tasks/lib/taskrunner.js b/tasks/lib/taskrunner.js index bfa26b3..fbbc8a7 100644 --- a/tasks/lib/taskrunner.js +++ b/tasks/lib/taskrunner.js @@ -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 diff --git a/tasks/watch.js b/tasks/watch.js index 2351f43..aa58c31 100644 --- a/tasks/watch.js +++ b/tasks/watch.js @@ -73,6 +73,7 @@ module.exports = function(grunt) { var targets = taskrun.init(name, { interrupt: false, nospawn: false, + atBegin: false, event: ['all'], target: target, }); diff --git a/test/fixtures/atBegin/Gruntfile.js b/test/fixtures/atBegin/Gruntfile.js new file mode 100644 index 0000000..9171d74 --- /dev/null +++ b/test/fixtures/atBegin/Gruntfile.js @@ -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']); +}; diff --git a/test/fixtures/atBegin/lib/one.js b/test/fixtures/atBegin/lib/one.js new file mode 100644 index 0000000..517f09f --- /dev/null +++ b/test/fixtures/atBegin/lib/one.js @@ -0,0 +1 @@ +var test = true; \ No newline at end of file diff --git a/test/tasks/watch_test.js b/test/tasks/watch_test.js index 85eb580..51a41e3 100644 --- a/test/tasks/watch_test.js +++ b/test/tasks/watch_test.js @@ -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');