1111import type {
1212 Console ,
1313 CrawlerOptions ,
14- FileData ,
14+ CrawlResult ,
1515 Path ,
1616 PerfLogger ,
1717 WatcherBackend ,
1818 WatcherBackendChangeEvent ,
19- WatchmanClocks ,
2019} from './flow-types' ;
2120import type { WatcherOptions as WatcherBackendOptions } from './watchers/common' ;
2221
@@ -37,12 +36,6 @@ const debug = require('debug')('Metro:Watcher');
3736
3837const MAX_WAIT_TIME = 240000 ;
3938
40- type CrawlResult = {
41- changedFiles : FileData ,
42- clocks ?: WatchmanClocks ,
43- removedFiles : Set < Path > ,
44- } ;
45-
4639type WatcherOptions = {
4740 abortSignal : AbortSignal ,
4841 computeSha1 : boolean ,
@@ -113,50 +106,45 @@ export class Watcher extends EventEmitter {
113106 roots : options . roots ,
114107 } ;
115108
116- const retry = ( error : Error ) : Promise < CrawlResult > => {
117- if ( crawl === watchmanCrawl ) {
118- crawler = 'node' ;
119- options . console . warn (
120- 'metro-file-map: Watchman crawl failed. Retrying once with node ' +
121- 'crawler.\n' +
122- " Usually this happens when watchman isn't running. Create an " +
123- "empty `.watchmanconfig` file in your project's root folder or " +
124- 'initialize a git or hg repository in your project.\n' +
125- ' ' +
126- error . toString ( ) ,
127- ) ;
128- // $FlowFixMe[incompatible-type] Found when updating Promise type definition
129- return nodeCrawl ( crawlerOptions ) . catch < CrawlResult > ( e => {
130- throw new Error (
131- 'Crawler retry failed:\n' +
132- ` Original error: ${ error . message } \n` +
133- ` Retry error: ${ e . message } \n` ,
134- ) ;
135- } ) ;
136- }
137-
138- throw error ;
139- } ;
140-
141- const logEnd = ( delta : CrawlResult ) : CrawlResult => {
142- debug (
143- 'Crawler "%s" returned %d added/modified, %d removed, %d clock(s).' ,
144- crawler ,
145- delta . changedFiles . size ,
146- delta . removedFiles . size ,
147- delta . clocks ?. size ?? 0 ,
148- ) ;
149- this . #options. perfLogger ?. point ( 'crawl_end' ) ;
150- return delta ;
151- } ;
152-
153109 debug ( 'Beginning crawl with "%s".' , crawler ) ;
110+
111+ let delta : CrawlResult ;
154112 try {
155- // $FlowFixMe[incompatible-type] Found when updating Promise type definition
156- return crawl ( crawlerOptions ) . catch < CrawlResult > ( retry ) . then ( logEnd ) ;
157- } catch ( error ) {
158- return retry ( error ) . then ( logEnd ) ;
113+ delta = await crawl ( crawlerOptions ) ;
114+ } catch ( firstError ) {
115+ if ( crawl !== watchmanCrawl ) {
116+ throw firstError ;
117+ }
118+ crawler = 'node' ;
119+ options . console . warn (
120+ 'metro-file-map: Watchman crawl failed. Retrying once with node ' +
121+ 'crawler.\n' +
122+ " Usually this happens when watchman isn't running. Create an " +
123+ "empty `.watchmanconfig` file in your project's root folder or " +
124+ 'initialize a git or hg repository in your project.\n' +
125+ ' ' +
126+ firstError . toString ( ) ,
127+ ) ;
128+ try {
129+ delta = await nodeCrawl ( crawlerOptions ) ;
130+ } catch ( retryError ) {
131+ throw new Error (
132+ 'Crawler retry failed:\n' +
133+ ` Original error: ${ firstError . message } \n` +
134+ ` Retry error: ${ retryError . message } \n` ,
135+ ) ;
136+ }
159137 }
138+
139+ debug (
140+ 'Crawler "%s" returned %d added/modified, %d removed, %d clock(s).' ,
141+ crawler ,
142+ delta . changedFiles . size ,
143+ delta . removedFiles . size ,
144+ delta . clocks ?. size ?? 0 ,
145+ ) ;
146+ this . #options. perfLogger ?. point ( 'crawl_end' ) ;
147+ return delta ;
160148 }
161149
162150 async watch ( onChange : ( change : WatcherBackendChangeEvent ) => void ) {
0 commit comments