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 ' ;
15+ import RecrawlWarning from './RecrawlWarning ' ;
1516
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 ;
17+ const CHANGE_EVENT = 'change' ;
18+ const DELETE_EVENT = 'delete' ;
19+ const ADD_EVENT = 'add' ;
20+ const ALL_EVENT = 'all' ;
2021const SUB_NAME = 'sane-sub' ;
2122
2223/**
@@ -29,7 +30,7 @@ const SUB_NAME = 'sane-sub';
2930 */
3031
3132export default function WatchmanWatcher ( dir , opts ) {
32- common . assignOptions ( this , opts ) ;
33+ assignOptions ( this , opts ) ;
3334 this . root = path . resolve ( dir ) ;
3435 this . init ( ) ;
3536}
@@ -226,7 +227,7 @@ WatchmanWatcher.prototype.handleFileChange = function (changeDescriptor) {
226227
227228 if (
228229 ! ( self . capabilities . wildmatch && ! this . hasIgnore ) &&
229- ! common . isFileIncluded ( this . globs , this . dot , this . doIgnore , relativePath )
230+ ! isFileIncluded ( this . globs , this . dot , this . doIgnore , relativePath )
230231 ) {
231232 return ;
232233 }
@@ -321,3 +322,33 @@ function handleWarning(resp) {
321322 return false ;
322323 }
323324}
325+
326+ function assignOptions ( watcher , opts ) {
327+ opts = opts || { } ;
328+ watcher . globs = opts . glob || [ ] ;
329+ watcher . dot = opts . dot || false ;
330+ watcher . ignored = opts . ignored || false ;
331+
332+ if ( ! Array . isArray ( watcher . globs ) ) {
333+ watcher . globs = [ watcher . globs ] ;
334+ }
335+ watcher . hasIgnore =
336+ Boolean ( opts . ignored ) && ! ( Array . isArray ( opts ) && opts . length > 0 ) ;
337+ watcher . doIgnore = opts . ignored ? anymatch ( opts . ignored ) : ( ) => false ;
338+
339+ if ( opts . watchman && opts . watchmanPath ) {
340+ watcher . watchmanPath = opts . watchmanPath ;
341+ }
342+
343+ return opts ;
344+ }
345+
346+ function isFileIncluded ( globs , dot , doIgnore , relativePath ) {
347+ if ( doIgnore ( relativePath ) ) {
348+ return false ;
349+ }
350+ return globs . length
351+ ? micromatch . some ( relativePath , globs , { dot} )
352+ : dot || micromatch . some ( relativePath , '**/*' ) ;
353+ }
354+
0 commit comments