@@ -174,6 +174,17 @@ namespace ts {
174
174
175
175
const noopFileWatcher : FileWatcher = { close : noop } ;
176
176
177
+ export function createWatchHost ( system = sys , reportWatchStatus ?: WatchStatusReporter ) : WatchHost {
178
+ const onWatchStatusChange = reportWatchStatus || createWatchStatusReporter ( system ) ;
179
+ return {
180
+ onWatchStatusChange,
181
+ watchFile : system . watchFile ? ( ( path , callback , pollingInterval ) => system . watchFile ! ( path , callback , pollingInterval ) ) : ( ) => noopFileWatcher ,
182
+ watchDirectory : system . watchDirectory ? ( ( path , callback , recursive ) => system . watchDirectory ! ( path , callback , recursive ) ) : ( ) => noopFileWatcher ,
183
+ setTimeout : system . setTimeout ? ( ( callback , ms , ...args : any [ ] ) => system . setTimeout ! . call ( system , callback , ms , ...args ) ) : noop ,
184
+ clearTimeout : system . clearTimeout ? ( timeoutId => system . clearTimeout ! ( timeoutId ) ) : noop
185
+ } ;
186
+ }
187
+
177
188
/**
178
189
* Creates the watch compiler host that can be extended with config file or root file names and options host
179
190
*/
@@ -186,7 +197,7 @@ namespace ts {
186
197
host ; // tslint:disable-line no-unused-expression (TODO: `host` is unused!)
187
198
const useCaseSensitiveFileNames = ( ) => system . useCaseSensitiveFileNames ;
188
199
const writeFileName = ( s : string ) => system . write ( s + system . newLine ) ;
189
- const onWatchStatusChange = reportWatchStatus || createWatchStatusReporter ( system ) ;
200
+ const { onWatchStatusChange, watchFile , watchDirectory , setTimeout , clearTimeout } = createWatchHost ( system , reportWatchStatus ) ;
190
201
return {
191
202
useCaseSensitiveFileNames,
192
203
getNewLine : ( ) => system . newLine ,
@@ -200,10 +211,10 @@ namespace ts {
200
211
readDirectory : ( path , extensions , exclude , include , depth ) => system . readDirectory ( path , extensions , exclude , include , depth ) ,
201
212
realpath : system . realpath && ( path => system . realpath ! ( path ) ) ,
202
213
getEnvironmentVariable : system . getEnvironmentVariable && ( name => system . getEnvironmentVariable ( name ) ) ,
203
- watchFile : system . watchFile ? ( ( path , callback , pollingInterval ) => system . watchFile ! ( path , callback , pollingInterval ) ) : ( ) => noopFileWatcher ,
204
- watchDirectory : system . watchDirectory ? ( ( path , callback , recursive ) => system . watchDirectory ! ( path , callback , recursive ) ) : ( ) => noopFileWatcher ,
205
- setTimeout : system . setTimeout ? ( ( callback , ms , ... args : any [ ] ) => system . setTimeout ! . call ( system , callback , ms , ... args ) ) : noop ,
206
- clearTimeout : system . clearTimeout ? ( timeoutId => system . clearTimeout ! ( timeoutId ) ) : noop ,
214
+ watchFile,
215
+ watchDirectory,
216
+ setTimeout,
217
+ clearTimeout,
207
218
trace : s => system . write ( s ) ,
208
219
onWatchStatusChange,
209
220
createDirectory : path => system . createDirectory ( path ) ,
@@ -224,10 +235,10 @@ namespace ts {
224
235
225
236
const reportSummary = ( errorCount : number ) => {
226
237
if ( errorCount === 1 ) {
227
- onWatchStatusChange ( createCompilerDiagnostic ( Diagnostics . Found_1_error_Watching_for_file_changes , errorCount ) , newLine , compilerOptions ) ;
238
+ onWatchStatusChange ! ( createCompilerDiagnostic ( Diagnostics . Found_1_error_Watching_for_file_changes , errorCount ) , newLine , compilerOptions ) ;
228
239
}
229
240
else {
230
- onWatchStatusChange ( createCompilerDiagnostic ( Diagnostics . Found_0_errors_Watching_for_file_changes , errorCount , errorCount ) , newLine , compilerOptions ) ;
241
+ onWatchStatusChange ! ( createCompilerDiagnostic ( Diagnostics . Found_0_errors_Watching_for_file_changes , errorCount , errorCount ) , newLine , compilerOptions ) ;
231
242
}
232
243
} ;
233
244
@@ -270,7 +281,21 @@ namespace ts {
270
281
export type WatchStatusReporter = ( diagnostic : Diagnostic , newLine : string , options : CompilerOptions ) => void ;
271
282
/** Create the program with rootNames and options, if they are undefined, oldProgram and new configFile diagnostics create new program */
272
283
export type CreateProgram < T extends BuilderProgram > = ( rootNames : ReadonlyArray < string > | undefined , options : CompilerOptions | undefined , host ?: CompilerHost , oldProgram ?: T , configFileParsingDiagnostics ?: ReadonlyArray < Diagnostic > ) => T ;
273
- export interface WatchCompilerHost < T extends BuilderProgram > {
284
+ /** Host that has watch functionality used in --watch mode */
285
+ export interface WatchHost {
286
+ /** If provided, called with Diagnostic message that informs about change in watch status */
287
+ onWatchStatusChange ?( diagnostic : Diagnostic , newLine : string , options : CompilerOptions ) : void ;
288
+
289
+ /** Used to watch changes in source files, missing files needed to update the program or config file */
290
+ watchFile ( path : string , callback : FileWatcherCallback , pollingInterval ?: number ) : FileWatcher ;
291
+ /** Used to watch resolved module's failed lookup locations, config file specs, type roots where auto type reference directives are added */
292
+ watchDirectory ( path : string , callback : DirectoryWatcherCallback , recursive ?: boolean ) : FileWatcher ;
293
+ /** If provided, will be used to set delayed compilation, so that multiple changes in short span are compiled together */
294
+ setTimeout ?( callback : ( ...args : any [ ] ) => void , ms : number , ...args : any [ ] ) : any ;
295
+ /** If provided, will be used to reset existing delayed compilation */
296
+ clearTimeout ?( timeoutId : any ) : void ;
297
+ }
298
+ export interface WatchCompilerHost < T extends BuilderProgram > extends WatchHost {
274
299
// TODO: GH#18217 Optional methods are frequently asserted
275
300
276
301
/**
@@ -279,8 +304,6 @@ namespace ts {
279
304
createProgram : CreateProgram < T > ;
280
305
/** If provided, callback to invoke after every new program creation */
281
306
afterProgramCreate ?( program : T ) : void ;
282
- /** If provided, called with Diagnostic message that informs about change in watch status */
283
- onWatchStatusChange ?( diagnostic : Diagnostic , newLine : string , options : CompilerOptions ) : void ;
284
307
285
308
// Only for testing
286
309
/*@internal */
@@ -323,15 +346,6 @@ namespace ts {
323
346
resolveModuleNames ?( moduleNames : string [ ] , containingFile : string , reusedNames ?: string [ ] ) : ResolvedModule [ ] ;
324
347
/** If provided, used to resolve type reference directives, otherwise typescript's default resolution */
325
348
resolveTypeReferenceDirectives ?( typeReferenceDirectiveNames : string [ ] , containingFile : string ) : ResolvedTypeReferenceDirective [ ] ;
326
-
327
- /** Used to watch changes in source files, missing files needed to update the program or config file */
328
- watchFile ( path : string , callback : FileWatcherCallback , pollingInterval ?: number ) : FileWatcher ;
329
- /** Used to watch resolved module's failed lookup locations, config file specs, type roots where auto type reference directives are added */
330
- watchDirectory ( path : string , callback : DirectoryWatcherCallback , recursive ?: boolean ) : FileWatcher ;
331
- /** If provided, will be used to set delayed compilation, so that multiple changes in short span are compiled together */
332
- setTimeout ?( callback : ( ...args : any [ ] ) => void , ms : number , ...args : any [ ] ) : any ;
333
- /** If provided, will be used to reset existing delayed compilation */
334
- clearTimeout ?( timeoutId : any ) : void ;
335
349
}
336
350
337
351
/** Internal interface used to wire emit through same host */
0 commit comments