@@ -4,6 +4,8 @@ var chokidar = require('chokidar');
44var debounce = require ( 'just-debounce' ) ;
55var asyncDone = require ( 'async-done' ) ;
66var defaults = require ( 'object.defaults/immutable' ) ;
7+ var isNegatedGlob = require ( 'is-negated-glob' ) ;
8+ var anymatch = require ( 'anymatch' ) ;
79
810var defaultOpts = {
911 delay : 200 ,
@@ -24,6 +26,10 @@ function hasErrorListener(ee) {
2426 return listenerCount ( ee , 'error' ) !== 0 ;
2527}
2628
29+ function exists ( val ) {
30+ return val != null ;
31+ }
32+
2733function watch ( glob , options , cb ) {
2834 if ( typeof options === 'function' ) {
2935 cb = options ;
@@ -36,10 +42,51 @@ function watch(glob, options, cb) {
3642 opt . events = [ opt . events ] ;
3743 }
3844
45+ if ( Array . isArray ( glob ) ) {
46+ // We slice so we don't mutate the passed globs array
47+ glob = glob . slice ( ) ;
48+ } else {
49+ glob = [ glob ] ;
50+ }
51+
3952 var queued = false ;
4053 var running = false ;
4154
42- var watcher = chokidar . watch ( glob , opt ) ;
55+ // These use sparse arrays to keep track of the index in the
56+ // original globs array
57+ var positives = new Array ( glob . length ) ;
58+ var negatives = new Array ( glob . length ) ;
59+
60+ // Reverse the glob here so we don't end up with a positive
61+ // and negative glob in position 0 after a reverse
62+ glob . reverse ( ) . forEach ( sortGlobs ) ;
63+
64+ function sortGlobs ( globString , index ) {
65+ var result = isNegatedGlob ( globString ) ;
66+ if ( result . negated ) {
67+ negatives [ index ] = result . pattern ;
68+ } else {
69+ positives [ index ] = result . pattern ;
70+ }
71+ }
72+
73+ function shouldBeIgnored ( path ) {
74+ var positiveMatch = anymatch ( positives , path , true ) ;
75+ var negativeMatch = anymatch ( negatives , path , true ) ;
76+ // If negativeMatch is -1, that means it was never negated
77+ if ( negativeMatch === - 1 ) {
78+ return false ;
79+ }
80+
81+ // If the negative is "less than" the positive, that means
82+ // it came later in the glob array before we reversed them
83+ return negativeMatch < positiveMatch ;
84+ }
85+
86+ var toWatch = positives . filter ( exists ) ;
87+
88+ opt . ignored = shouldBeIgnored ;
89+ var watcher = chokidar . watch ( toWatch , opt ) ;
4390
4491 function runComplete ( err ) {
4592 running = false ;
0 commit comments