@@ -137,6 +137,40 @@ exports.watchRun = (mocha, {watchFiles, watchIgnore}, fileCollectParams) => {
137137 } ) ;
138138} ;
139139
140+ class GlobFilesTracker {
141+ constructor ( watchFiles , watchIgnore ) {
142+ this . watchFilesSet = new Set ( ) ;
143+ this . watchFiles = watchFiles ;
144+ this . watchIgnore = watchIgnore ;
145+ }
146+
147+ regenerate ( ) {
148+ let watchIgnoreFlat = [ ] ;
149+ for ( const pattern of this . watchIgnore ) {
150+ watchIgnoreFlat = watchIgnoreFlat . concat ( glob . sync ( pattern , { dot : true } ) ) ;
151+ }
152+
153+ this . watchFilesSet . clear ( ) ;
154+ for ( const pattern of this . watchFiles ) {
155+ glob . sync ( pattern , { dot : true } ) . forEach ( pathToCheck => {
156+ for ( const watchIgnore of watchIgnoreFlat ) {
157+ if ( pathToCheck === watchIgnore ) {
158+ return ;
159+ }
160+ if ( pathToCheck . startsWith ( watchIgnore + path . sep ) ) {
161+ return ;
162+ }
163+ }
164+ this . watchFilesSet . add ( pathToCheck ) ;
165+ } ) ;
166+ }
167+ }
168+
169+ has ( filePath ) {
170+ return this . watchFilesSet . has ( filePath )
171+ }
172+ }
173+
140174/**
141175 * Bootstraps a chokidar watcher. Handles keyboard input & signals
142176 * @param {Mocha } mocha - Mocha instance
@@ -168,34 +202,8 @@ const createWatcher = (
168202 // we handle global fixtures manually
169203 mocha . enableGlobalSetup ( false ) . enableGlobalTeardown ( false ) ;
170204
171- const watchFilesFlat = [ ] ;
172-
173- const regenerateWatchFilesFlat = ( ) => {
174- let watchIgnoreFlat = [ ] ;
175- for ( const pattern of watchIgnore ) {
176- watchIgnoreFlat = watchIgnoreFlat . concat ( glob . sync ( pattern , { dot : true } ) ) ;
177- }
178-
179- let newWatchFilesFlat = [ ] ;
180- for ( const pattern of watchFiles ) {
181- newWatchFilesFlat = newWatchFilesFlat . concat (
182- glob . sync ( pattern , { dot : true } ) . filter ( pathToCheck => {
183- for ( const watchIgnore of watchIgnoreFlat ) {
184- if ( pathToCheck === watchIgnore ) {
185- return false ;
186- }
187- if ( pathToCheck . startsWith ( watchIgnore + path . sep ) ) {
188- return false ;
189- }
190- }
191- return true ;
192- } ) ) ;
193- }
194- watchFilesFlat . splice ( 0 , watchFiles . length ) ;
195- watchFilesFlat . push ( ...newWatchFilesFlat ) ;
196- }
197-
198- regenerateWatchFilesFlat ( ) ;
205+ const tracker = new GlobFilesTracker ( watchFiles , watchIgnore ) ;
206+ tracker . regenerate ( ) ;
199207
200208 const watcher = chokidar . watch ( '.' , {
201209 ignoreInitial : true
@@ -214,15 +222,11 @@ const createWatcher = (
214222 } ) ;
215223
216224 watcher . on ( 'all' , ( event , filePath ) => {
217- if ( watchFilesFlat . includes ( filePath ) ) {
225+ if ( event === 'add' ) {
226+ tracker . regenerate ( ) ;
227+ }
228+ if ( tracker . has ( filePath ) ) {
218229 rerunner . scheduleRun ( ) ;
219- } else {
220- if ( event === 'add' ) {
221- regenerateWatchFilesFlat ( ) ;
222- if ( watchFilesFlat . includes ( filePath ) ) {
223- rerunner . scheduleRun ( ) ;
224- }
225- }
226230 }
227231 } ) ;
228232
0 commit comments