@@ -174,6 +174,17 @@ namespace ts {
174174
175175 const noopFileWatcher : FileWatcher = { close : noop } ;
176176
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+
177188 /**
178189 * Creates the watch compiler host that can be extended with config file or root file names and options host
179190 */
@@ -186,7 +197,7 @@ namespace ts {
186197 host ; // tslint:disable-line no-unused-expression (TODO: `host` is unused!)
187198 const useCaseSensitiveFileNames = ( ) => system . useCaseSensitiveFileNames ;
188199 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 ) ;
190201 return {
191202 useCaseSensitiveFileNames,
192203 getNewLine : ( ) => system . newLine ,
@@ -200,10 +211,10 @@ namespace ts {
200211 readDirectory : ( path , extensions , exclude , include , depth ) => system . readDirectory ( path , extensions , exclude , include , depth ) ,
201212 realpath : system . realpath && ( path => system . realpath ! ( path ) ) ,
202213 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,
207218 trace : s => system . write ( s ) ,
208219 onWatchStatusChange,
209220 createDirectory : path => system . createDirectory ( path ) ,
@@ -224,10 +235,10 @@ namespace ts {
224235
225236 const reportSummary = ( errorCount : number ) => {
226237 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 ) ;
228239 }
229240 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 ) ;
231242 }
232243 } ;
233244
@@ -270,7 +281,21 @@ namespace ts {
270281 export type WatchStatusReporter = ( diagnostic : Diagnostic , newLine : string , options : CompilerOptions ) => void ;
271282 /** Create the program with rootNames and options, if they are undefined, oldProgram and new configFile diagnostics create new program */
272283 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 {
274299 // TODO: GH#18217 Optional methods are frequently asserted
275300
276301 /**
@@ -279,8 +304,6 @@ namespace ts {
279304 createProgram : CreateProgram < T > ;
280305 /** If provided, callback to invoke after every new program creation */
281306 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 ;
284307
285308 // Only for testing
286309 /*@internal */
@@ -323,15 +346,6 @@ namespace ts {
323346 resolveModuleNames ?( moduleNames : string [ ] , containingFile : string , reusedNames ?: string [ ] ) : ResolvedModule [ ] ;
324347 /** If provided, used to resolve type reference directives, otherwise typescript's default resolution */
325348 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 ;
335349 }
336350
337351 /** Internal interface used to wire emit through same host */
0 commit comments