88import assert from 'assert' ;
99import { EventEmitter } from 'events' ;
1010import path from 'path' ;
11+ import anymatch from 'anymatch' ;
1112import watchman from 'fb-watchman' ;
1213import * as fs from 'graceful-fs' ;
13- import common from 'sane/src/common' ;
14- import RecrawlWarning from 'sane/src/utils/recrawl-warning-dedupe' ;
14+ import micromatch from 'micromatch' ;
1515
16- const CHANGE_EVENT = common . CHANGE_EVENT ;
17- const DELETE_EVENT = common . DELETE_EVENT ;
18- const ADD_EVENT = common . ADD_EVENT ;
19- const ALL_EVENT = common . ALL_EVENT ;
16+ const CHANGE_EVENT = 'change' ;
17+ const DELETE_EVENT = 'delete' ;
18+ const ADD_EVENT = 'add' ;
19+ const ALL_EVENT = 'all' ;
2020const SUB_NAME = 'sane-sub' ;
2121
2222/**
@@ -29,7 +29,7 @@ const SUB_NAME = 'sane-sub';
2929 */
3030
3131export default function WatchmanWatcher ( dir , opts ) {
32- common . assignOptions ( this , opts ) ;
32+ assignOptions ( this , opts ) ;
3333 this . root = path . resolve ( dir ) ;
3434 this . init ( ) ;
3535}
@@ -226,7 +226,7 @@ WatchmanWatcher.prototype.handleFileChange = function (changeDescriptor) {
226226
227227 if (
228228 ! ( self . capabilities . wildmatch && ! this . hasIgnore ) &&
229- ! common . isFileIncluded ( this . globs , this . dot , this . doIgnore , relativePath )
229+ ! isFileIncluded ( this . globs , this . dot , this . doIgnore , relativePath )
230230 ) {
231231 return ;
232232 }
@@ -321,3 +321,80 @@ function handleWarning(resp) {
321321 return false ;
322322 }
323323}
324+
325+ function assignOptions ( watcher , opts ) {
326+ opts = opts || { } ;
327+ watcher . globs = opts . glob || [ ] ;
328+ watcher . dot = opts . dot || false ;
329+ watcher . ignored = opts . ignored || false ;
330+
331+ if ( ! Array . isArray ( watcher . globs ) ) {
332+ watcher . globs = [ watcher . globs ] ;
333+ }
334+ watcher . hasIgnore =
335+ Boolean ( opts . ignored ) && ! ( Array . isArray ( opts ) && opts . length > 0 ) ;
336+ watcher . doIgnore = opts . ignored ? anymatch ( opts . ignored ) : ( ) => false ;
337+
338+ if ( opts . watchman && opts . watchmanPath ) {
339+ watcher . watchmanPath = opts . watchmanPath ;
340+ }
341+
342+ return opts ;
343+ }
344+
345+ function isFileIncluded ( globs , dot , doIgnore , relativePath ) {
346+ if ( doIgnore ( relativePath ) ) {
347+ return false ;
348+ }
349+ return globs . length
350+ ? micromatch . some ( relativePath , globs , { dot} )
351+ : dot || micromatch . some ( relativePath , '**/*' ) ;
352+ }
353+
354+ class RecrawlWarning {
355+ constructor ( root , count ) {
356+ this . root = root ;
357+ this . count = count ;
358+ }
359+
360+ static findByRoot ( root ) {
361+ for ( let i = 0 ; i < this . RECRAWL_WARNINGS . length ; i ++ ) {
362+ const warning = this . RECRAWL_WARNINGS [ i ] ;
363+ if ( warning . root === root ) {
364+ return warning ;
365+ }
366+ }
367+ }
368+
369+ static isRecrawlWarningDupe ( warningMessage ) {
370+ if ( typeof warningMessage !== 'string' ) {
371+ return false ;
372+ }
373+ const match = warningMessage . match ( this . REGEXP ) ;
374+ if ( ! match ) {
375+ return false ;
376+ }
377+ const count = Number ( match [ 1 ] ) ;
378+ const root = match [ 2 ] ;
379+
380+ const warning = this . findByRoot ( root ) ;
381+
382+ if ( warning ) {
383+ // only keep the highest count, assume count to either stay the same or
384+ // increase.
385+ if ( warning . count >= count ) {
386+ return true ;
387+ } else {
388+ // update the existing warning to the latest (highest) count
389+ warning . count = count ;
390+ return false ;
391+ }
392+ } else {
393+ this . RECRAWL_WARNINGS . push ( new RecrawlWarning ( root , count ) ) ;
394+ return false ;
395+ }
396+ }
397+ }
398+
399+ RecrawlWarning . RECRAWL_WARNINGS = [ ] ;
400+ RecrawlWarning . REGEXP = / R e c r a w l e d t h i s w a t c h ( \d + ) t i m e s , m o s t r e c e n t l y b e c a u s e : \n ( [ ^ : ] + ) / ;
0 commit comments