@@ -21,26 +21,34 @@ module.exports = function(grunt) {
2121 // have changed incorrectly.
2222 var mtimes = { } ;
2323
24- grunt . registerTask ( 'watch' , 'Run predefined tasks whenever watched files change.' , function ( prop ) {
25-
26- var props = [ 'watch' ] ;
27- // If a prop was passed as the argument, use that sub-property of watch.
28- if ( prop ) { props . push ( prop ) ; }
29- // Get the files and tasks sub-properties.
30- var filesProp = props . concat ( 'files' ) ;
31- var tasksProp = props . concat ( 'tasks' ) ;
32-
33- // Fail if any required config properties have been omitted.
34- this . requiresConfig ( filesProp , tasksProp ) ;
24+ grunt . registerTask ( 'watch' , 'Run predefined tasks whenever watched files change.' , function ( target ) {
25+ this . requiresConfig ( 'watch' ) ;
26+ // Build an array of files/tasks objects.
27+ var watch = grunt . config ( 'watch' ) ;
28+ var targets = target ? [ target ] : Object . keys ( watch ) . filter ( function ( key ) {
29+ return typeof watch [ key ] !== 'string' && ! Array . isArray ( watch [ key ] ) ;
30+ } ) ;
31+ targets = targets . map ( function ( target ) {
32+ // Fail if any required config properties have been omitted.
33+ target = [ 'watch' , target ] ;
34+ this . requiresConfig ( target . concat ( 'files' ) , target . concat ( 'tasks' ) ) ;
35+ return grunt . config ( target ) ;
36+ } , this ) ;
37+
38+ // Allow "basic" non-target format.
39+ if ( typeof watch . files === 'string' || Array . isArray ( watch . files ) ) {
40+ targets . push ( { files : watch . files , tasks : watch . tasks } ) ;
41+ }
3542
3643 grunt . log . write ( 'Waiting...' ) ;
3744
3845 // This task is asynchronous.
3946 var taskDone = this . async ( ) ;
40- // Get a list of ffles to be watched.
41- var getFiles = grunt . file . expandFiles . bind ( grunt . file , grunt . config ( filesProp ) ) ;
47+ // Get a list of files to be watched.
48+ var patterns = grunt . utils . _ . chain ( targets ) . pluck ( 'files' ) . flatten ( ) . uniq ( ) . value ( ) ;
49+ var getFiles = grunt . file . expandFiles . bind ( grunt . file , patterns ) ;
4250 // The tasks to be run.
43- var tasks = grunt . config ( tasksProp ) ;
51+ var tasks = [ ] ; // grunt.config(tasksProp);
4452 // This task's name + optional args, in string format.
4553 var nameArgs = this . nameArgs ;
4654 // An ID by which the setInterval can be canceled.
@@ -52,7 +60,7 @@ module.exports = function(grunt) {
5260
5361 // Define an alternate fail "warn" behavior.
5462 grunt . fail . warnAlternate = function ( ) {
55- grunt . task . clearQueue ( ) . run ( nameArgs ) ;
63+ grunt . task . clearQueue ( { untilMarker : true } ) . run ( nameArgs ) ;
5664 } ;
5765
5866 // Cleanup when files have changed. This is debounced to handle situations
@@ -63,17 +71,26 @@ module.exports = function(grunt) {
6371 clearInterval ( intervalId ) ;
6472 // Ok!
6573 grunt . log . ok ( ) ;
66- Object . keys ( changedFiles ) . forEach ( function ( filepath ) {
74+ var fileArray = Object . keys ( changedFiles ) ;
75+ fileArray . forEach ( function ( filepath ) {
6776 // Log which file has changed, and how.
6877 grunt . log . ok ( 'File "' + filepath + '" ' + changedFiles [ filepath ] + '.' ) ;
6978 // Clear the modified file's cached require data.
7079 grunt . file . clearRequireCache ( filepath ) ;
7180 } ) ;
7281 // Unwatch all watched files.
7382 Object . keys ( watchedFiles ) . forEach ( unWatchFile ) ;
74- // Enqueue all specified tasks (if specified)...
75- if ( tasks ) { grunt . task . run ( tasks ) ; }
76- // ...followed by the watch task, so that it loops.
83+ // For each specified target, test to see if any files matching that
84+ // target's file patterns were modified.
85+ targets . forEach ( function ( target ) {
86+ var files = grunt . file . expandFiles ( target . files ) ;
87+ var intersection = grunt . utils . _ . intersection ( fileArray , files ) ;
88+ // Enqueue specified tasks if a matching file was found.
89+ if ( intersection . length > 0 && target . tasks ) {
90+ grunt . task . run ( target . tasks ) . mark ( ) ;
91+ }
92+ } ) ;
93+ // Enqueue the watch task, so that it loops.
7794 grunt . task . run ( nameArgs ) ;
7895 // Continue task queue.
7996 taskDone ( ) ;
0 commit comments