@@ -36,6 +36,7 @@ import {DiskCacheManager} from './cache/DiskCacheManager';
36
36
import H from './constants' ;
37
37
import getMockName from './getMockName' ;
38
38
import HasteFS from './HasteFS' ;
39
+ import canUseWatchman from './lib/canUseWatchman' ;
39
40
import deepCloneInternalData from './lib/deepCloneInternalData' ;
40
41
import * as fastPath from './lib/fast_path' ;
41
42
import getPlatformExtension from './lib/getPlatformExtension' ;
@@ -48,7 +49,6 @@ import NodeWatcher from './watchers/NodeWatcher';
48
49
// $FlowFixMe[untyped-import] - WatchmanWatcher
49
50
import WatchmanWatcher from './watchers/WatchmanWatcher' ;
50
51
import { getSha1 , worker } from './worker' ;
51
- import { execSync } from 'child_process' ;
52
52
import EventEmitter from 'events' ;
53
53
import invariant from 'invariant' ;
54
54
// $FlowFixMe[untyped-import] - jest-regex-util
@@ -145,14 +145,6 @@ const VCS_DIRECTORIES = ['.git', '.hg']
145
145
. map ( vcs => escapePathForRegex ( path . sep + vcs + path . sep ) )
146
146
. join ( '|' ) ;
147
147
148
- const canUseWatchman = ( ( ) : boolean => {
149
- try {
150
- execSync ( 'watchman --version' , { stdio : [ 'ignore' ] } ) ;
151
- return true ;
152
- } catch { }
153
- return false ;
154
- } ) ( ) ;
155
-
156
148
/**
157
149
* HasteMap is a JavaScript implementation of Facebook's haste module system.
158
150
*
@@ -233,6 +225,7 @@ const canUseWatchman = ((): boolean => {
233
225
export default class HasteMap extends EventEmitter {
234
226
_buildPromise : ?Promise < InternalDataObject > ;
235
227
_cachePath: Path;
228
+ _canUseWatchmanPromise: Promise< boolean > ;
236
229
_changeInterval: ?IntervalID;
237
230
_console: Console;
238
231
_options: InternalOptions;
@@ -787,7 +780,7 @@ export default class HasteMap extends EventEmitter {
787
780
return this . _worker ;
788
781
}
789
782
790
- _crawl ( hasteMap : InternalData ) : Promise < ?(
783
+ async _crawl ( hasteMap : InternalData ) : Promise < ?(
791
784
| Promise < {
792
785
changedFiles ? : FileData ,
793
786
hasteMap : InternalData ,
@@ -798,8 +791,7 @@ export default class HasteMap extends EventEmitter {
798
791
this . _options . perfLogger ?. point ( 'crawl_start' ) ;
799
792
const options = this . _options ;
800
793
const ignore = ( filePath : string ) => this . _ignore ( filePath ) ;
801
- const crawl =
802
- canUseWatchman && this . _options . useWatchman ? watchmanCrawl : nodeCrawl ;
794
+ const crawl = ( await this . _shouldUseWatchman ( ) ) ? watchmanCrawl : nodeCrawl ;
803
795
const crawlerOptions : CrawlerOptions = {
804
796
abortSignal : this . _crawlerAbortController . signal ,
805
797
computeSha1 : options . computeSha1 ,
@@ -851,11 +843,11 @@ export default class HasteMap extends EventEmitter {
851
843
/**
852
844
* Watch mode
853
845
*/
854
- _watch ( hasteMap : InternalData ) : Promise < void > {
846
+ async _watch ( hasteMap : InternalData ) : Promise < void > {
855
847
this . _options . perfLogger ?. point ( 'watch_start' ) ;
856
848
if ( ! this . _options . watch ) {
857
849
this . _options . perfLogger ?. point ( 'watch_end' ) ;
858
- return Promise . resolve ( ) ;
850
+ return ;
859
851
}
860
852
861
853
// In watch mode, we'll only warn about module collisions and we'll retain
@@ -864,12 +856,11 @@ export default class HasteMap extends EventEmitter {
864
856
this._options.retainAllFiles = true;
865
857
866
858
// WatchmanWatcher > FSEventsWatcher > sane . NodeWatcher
867
- const WatcherImpl =
868
- canUseWatchman && this . _options . useWatchman
869
- ? WatchmanWatcher
870
- : FSEventsWatcher . isSupported ( )
871
- ? FSEventsWatcher
872
- : NodeWatcher ;
859
+ const WatcherImpl = ( await this . _shouldUseWatchman ( ) )
860
+ ? WatchmanWatcher
861
+ : FSEventsWatcher . isSupported ( )
862
+ ? FSEventsWatcher
863
+ : NodeWatcher ;
873
864
874
865
const extensions = this . _options . extensions ;
875
866
const ignorePattern = this . _options . ignorePattern ;
@@ -1067,12 +1058,10 @@ export default class HasteMap extends EventEmitter {
1067
1058
1068
1059
this . _changeInterval = setInterval ( emitChange , CHANGE_INTERVAL ) ;
1069
1060
1070
- return Promise . all ( this . _options . roots . map ( createWatcher ) ) . then (
1071
- watchers => {
1072
- this . _watchers = watchers ;
1073
- this . _options . perfLogger ?. point ( 'watch_end' ) ;
1074
- } ,
1075
- );
1061
+ await Promise . all ( this . _options . roots . map ( createWatcher ) ) . then ( watchers => {
1062
+ this . _watchers = watchers ;
1063
+ this . _options . perfLogger ?. point ( 'watch_end' ) ;
1064
+ } );
1076
1065
}
1077
1066
1078
1067
/**
@@ -1163,6 +1152,16 @@ export default class HasteMap extends EventEmitter {
1163
1152
) ;
1164
1153
}
1165
1154
1155
+ async _shouldUseWatchman(): Promise< boolean > {
1156
+ if ( ! this . _options . useWatchman ) {
1157
+ return false ;
1158
+ }
1159
+ if (!this._canUseWatchmanPromise) {
1160
+ this . _canUseWatchmanPromise = canUseWatchman ( ) ;
1161
+ }
1162
+ return this._canUseWatchmanPromise;
1163
+ }
1164
+
1166
1165
_createEmptyMap ( ) : InternalData {
1167
1166
return {
1168
1167
clocks : new Map ( ) ,
0 commit comments