Add event option to only trigger watch on certain events. Closes GH-63.

This commit is contained in:
Jaime Pillora 2013-03-15 12:32:35 +11:00 committed by Kyle Robinson Young
parent 82d0df4bbd
commit cf415ad229
3 changed files with 67 additions and 12 deletions

View File

@ -103,6 +103,25 @@ Default: 100
The `interval` is passed to `fs.watchFile`. Since `interval` is only used by `fs.watchFile` and this watcher also uses `fs.watch`; it is recommended to ignore this option. *Default is 100ms*. The `interval` is passed to `fs.watchFile`. Since `interval` is only used by `fs.watchFile` and this watcher also uses `fs.watch`; it is recommended to ignore this option. *Default is 100ms*.
###### options.event
Type: `String|Array`
Default: `'all'`
Specify the type watch event that trigger the specified task. This option can be one or many of: `'all'`, `'changed'`, `'added'` and `'deleted'`.
Example:
```js
watch: {
scripts: {
files: '**/*.js',
tasks: ['generateFileManifest'],
options: {
event: ['added', 'deleted']
}
}
}
```
### Examples ### Examples
```js ```js
@ -177,17 +196,17 @@ Spawning does cause a performance hit (usually 500ms for most environments). It
## Release History ## Release History
* 2013-02-27v0.3.1Fix for top level options. * 2013-02-28v0.3.1Fix for top level options.
* 2013-02-26v0.3.0nospawn option added to run tasks without spawning as child processes. Watch emits 'watch' events upon files being triggered with grunt.event. Completion time in seconds and date/time shown after tasks ran. Negate file patterns fixed. Tasks debounced individually to handle simultaneous triggering for multiple targets. Errors handled better and viewable with --stack cli option. Code complexity reduced making the watch task code easier to read. * 2013-02-27v0.3.0nospawn option added to run tasks without spawning as child processes. Watch emits 'watch' events upon files being triggered with grunt.event. Completion time in seconds and date/time shown after tasks ran. Negate file patterns fixed. Tasks debounced individually to handle simultaneous triggering for multiple targets. Errors handled better and viewable with --stack cli option. Code complexity reduced making the watch task code easier to read.
* 2013-02-14v0.2.0First official release for Grunt 0.4.0. * 2013-02-15v0.2.0First official release for Grunt 0.4.0.
* 2013-01-17v0.2.0rc7Updating grunt/gruntplugin dependencies to rc6. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions. * 2013-01-18v0.2.0rc7Updating grunt/gruntplugin dependencies to rc6. Changing in-development grunt/gruntplugin dependency versions from tilde version ranges to specific versions.
* 2013-01-08v0.2.0rc5Updating to work with grunt v0.4.0rc5. * 2013-01-09v0.2.0rc5Updating to work with grunt v0.4.0rc5.
* 2012-12-14v0.2.0aConversion to grunt v0.4 conventions. Remove node v0.6 and grunt v0.3 support. Allow watch task to be renamed. Use grunt.util.spawn "grunt" option. Updated to gaze@0.3.0, forceWatchMethod option removed. * 2012-12-15v0.2.0aConversion to grunt v0.4 conventions. Remove node v0.6 and grunt v0.3 support. Allow watch task to be renamed. Use grunt.util.spawn "grunt" option. Updated to gaze@0.3.0, forceWatchMethod option removed.
* 2012-10-31v0.1.4Prevent watch from spawning duplicate watch tasks * 2012-11-01v0.1.4Prevent watch from spawning duplicate watch tasks
* 2012-10-27v0.1.3Better method to spawn the grunt bin Bump gaze to v0.2.0. Better handles some events and new option forceWatchMethod Only support Node.js >= v0.8 * 2012-10-28v0.1.3Better method to spawn the grunt bin Bump gaze to v0.2.0. Better handles some events and new option forceWatchMethod Only support Node.js >= v0.8
* 2012-10-16v0.1.2Only spawn a process per task one at a time Add interrupt option to cancel previous spawned process Grunt v0.3 compatibility changes * 2012-10-17v0.1.2Only spawn a process per task one at a time Add interrupt option to cancel previous spawned process Grunt v0.3 compatibility changes
* 2012-10-15v0.1.1Fallback to global grunt bin if local doesnt exist. Fatal if bin cannot be found Update to gaze 0.1.6 * 2012-10-16v0.1.1Fallback to global grunt bin if local doesnt exist. Fatal if bin cannot be found Update to gaze 0.1.6
* 2012-10-07v0.1.0Release watch task Remove spawn from helper Run on Grunt v0.4 * 2012-10-08v0.1.0Release watch task Remove spawn from helper Run on Grunt v0.4
--- ---

View File

@ -74,3 +74,22 @@ Type: `Integer`
Default: 100 Default: 100
The `interval` is passed to `fs.watchFile`. Since `interval` is only used by `fs.watchFile` and this watcher also uses `fs.watch`; it is recommended to ignore this option. *Default is 100ms*. The `interval` is passed to `fs.watchFile`. Since `interval` is only used by `fs.watchFile` and this watcher also uses `fs.watch`; it is recommended to ignore this option. *Default is 100ms*.
#### options.event
Type: `String|Array`
Default: `'all'`
Specify the type watch event that trigger the specified task. This option can be one or many of: `'all'`, `'changed'`, `'added'` and `'deleted'`.
Example:
```js
watch: {
scripts: {
files: '**/*.js',
tasks: ['generateFileManifest'],
options: {
event: ['added', 'deleted']
}
}
}
```

View File

@ -20,7 +20,8 @@ module.exports = function(grunt) {
// Default options for the watch task // Default options for the watch task
var defaults = this.options({ var defaults = this.options({
interrupt: false, interrupt: false,
nospawn: false nospawn: false,
event: 'all'
}); });
// Build an array of files/tasks objects // Build an array of files/tasks objects
@ -69,6 +70,15 @@ module.exports = function(grunt) {
// Default options per target // Default options per target
var options = grunt.util._.defaults(target.options || {}, defaults); var options = grunt.util._.defaults(target.options || {}, defaults);
// Validate the event option
if (typeof options.event === 'string') {
options.event = [options.event];
} else if (!Array.isArray(options.event)) {
grunt.log.writeln('ERROR'.red);
grunt.fatal('Invalid event option type');
return done();
}
// Create watcher per target // Create watcher per target
new Gaze(patterns, options, function(err) { new Gaze(patterns, options, function(err) {
if (err) { if (err) {
@ -83,6 +93,13 @@ module.exports = function(grunt) {
// On changed/added/deleted // On changed/added/deleted
this.on('all', function(status, filepath) { this.on('all', function(status, filepath) {
// Skip events not specified
if(!grunt.util._.contains(options.event, 'all') &&
!grunt.util._.contains(options.event, status)) {
return;
}
filepath = path.relative(process.cwd(), filepath); filepath = path.relative(process.cwd(), filepath);
// Emit watch events if anyone is listening // Emit watch events if anyone is listening