@@ -90,75 +90,89 @@ namespace ts {
9090 return program . isEmittedFile ( file ) ;
9191 }
9292
93- export function addFileWatcher ( host : System , file : string , cb : FileWatcherCallback ) : FileWatcher {
94- return host . watchFile ( file , cb ) ;
93+ export enum WatchLogLevel {
94+ None ,
95+ TriggerOnly ,
96+ Verbose
9597 }
9698
97- export function addFileWatcherWithLogging ( host : System , file : string , cb : FileWatcherCallback , log : ( s : string ) => void ) : FileWatcher {
98- const watcherCaption = `FileWatcher:: ` ;
99- return createWatcherWithLogging ( addFileWatcher , watcherCaption , log , /*logOnlyTrigger*/ false , host , file , cb ) ;
100- }
99+ export type WatchFile < X , Y > = ( host : System , file : string , callback : FileWatcherCallback , pollingInterval ?: number , detailInfo1 ?: X , detailInfo2 ?: Y ) => FileWatcher ;
100+ export type FilePathWatcherCallback = ( fileName : string , eventKind : FileWatcherEventKind , filePath : Path ) => void ;
101+ export type WatchFilePath < X , Y > = ( host : System , file : string , callback : FilePathWatcherCallback , pollingInterval : number | undefined , path : Path , detailInfo1 ?: X , detailInfo2 ?: Y ) => FileWatcher ;
102+ export type WatchDirectory < X , Y > = ( host : System , directory : string , callback : DirectoryWatcherCallback , flags : WatchDirectoryFlags , detailInfo1 ?: X , detailInfo2 ?: Y ) => FileWatcher ;
101103
102- export function addFileWatcherWithOnlyTriggerLogging ( host : System , file : string , cb : FileWatcherCallback , log : ( s : string ) => void ) : FileWatcher {
103- const watcherCaption = `FileWatcher:: ` ;
104- return createWatcherWithLogging ( addFileWatcher , watcherCaption , log , /*logOnlyTrigger*/ true , host , file , cb ) ;
104+ export function createWatchFile < X = undefined , Y = undefined > ( watchLogLevel : WatchLogLevel , log : ( s : string ) => void , getDetailWatchInfo ?: GetDetailWatchInfo < X , Y > ) : WatchFile < X , Y > {
105+ const createFileWatcher : CreateFileWatcher < number | undefined , FileWatcherEventKind , undefined , X , Y > = getCreateFileWatcher ( watchLogLevel , watchFile ) ;
106+ return ( host , file , callback , pollingInterval , detailInfo1 , detailInfo2 ) =>
107+ createFileWatcher ( host , file , callback , pollingInterval , /*passThrough*/ undefined , detailInfo1 , detailInfo2 , watchFile , log , "FileWatcher" , getDetailWatchInfo ) ;
105108 }
106109
107- export type FilePathWatcherCallback = ( fileName : string , eventKind : FileWatcherEventKind , filePath : Path ) => void ;
108- export function addFilePathWatcher ( host : System , file : string , cb : FilePathWatcherCallback , path : Path ) : FileWatcher {
109- return host . watchFile ( file , ( fileName , eventKind ) => cb ( fileName , eventKind , path ) ) ;
110+ export function createWatchFilePath < X = undefined , Y = undefined > ( watchLogLevel : WatchLogLevel , log : ( s : string ) => void , getDetailWatchInfo ?: GetDetailWatchInfo < X , Y > ) : WatchFilePath < X , Y > {
111+ const createFileWatcher : CreateFileWatcher < number | undefined , FileWatcherEventKind , Path , X , Y > = getCreateFileWatcher ( watchLogLevel , watchFilePath ) ;
112+ return ( host , file , callback , pollingInterval , path , detailInfo1 , detailInfo2 ) =>
113+ createFileWatcher ( host , file , callback , pollingInterval , path , detailInfo1 , detailInfo2 , watchFile , log , "FileWatcher" , getDetailWatchInfo ) ;
110114 }
111115
112- export function addFilePathWatcherWithLogging ( host : System , file : string , cb : FilePathWatcherCallback , path : Path , log : ( s : string ) => void ) : FileWatcher {
113- const watcherCaption = `FileWatcher:: ` ;
114- return createWatcherWithLogging ( addFileWatcher , watcherCaption , log , /*logOnlyTrigger*/ false , host , file , cb , path ) ;
116+ export function createWatchDirectory < X = undefined , Y = undefined > ( watchLogLevel : WatchLogLevel , log : ( s : string ) => void , getDetailWatchInfo ?: GetDetailWatchInfo < X , Y > ) : WatchDirectory < X , Y > {
117+ const createFileWatcher : CreateFileWatcher < WatchDirectoryFlags , undefined , undefined , X , Y > = getCreateFileWatcher ( watchLogLevel , watchDirectory ) ;
118+ return ( host , directory , callback , flags , detailInfo1 , detailInfo2 ) =>
119+ createFileWatcher ( host , directory , callback , flags , /*passThrough*/ undefined , detailInfo1 , detailInfo2 , watchDirectory , log , "DirectoryWatcher" , getDetailWatchInfo ) ;
115120 }
116121
117- export function addFilePathWatcherWithOnlyTriggerLogging ( host : System , file : string , cb : FilePathWatcherCallback , path : Path , log : ( s : string ) => void ) : FileWatcher {
118- const watcherCaption = `FileWatcher:: ` ;
119- return createWatcherWithLogging ( addFileWatcher , watcherCaption , log , /*logOnlyTrigger*/ true , host , file , cb , path ) ;
122+ function watchFile ( host : System , file : string , callback : FileWatcherCallback , pollingInterval ?: number ) : FileWatcher {
123+ return host . watchFile ( file , callback , pollingInterval ) ;
120124 }
121125
122- export function addDirectoryWatcher ( host : System , directory : string , cb : DirectoryWatcherCallback , flags : WatchDirectoryFlags ) : FileWatcher {
123- const recursive = ( flags & WatchDirectoryFlags . Recursive ) !== 0 ;
124- return host . watchDirectory ( directory , cb , recursive ) ;
126+ function watchFilePath ( host : System , file : string , callback : FilePathWatcherCallback , pollingInterval : number | undefined , path : Path ) : FileWatcher {
127+ return host . watchFile ( file , ( fileName , eventKind ) => callback ( fileName , eventKind , path ) , pollingInterval ) ;
125128 }
126129
127- export function addDirectoryWatcherWithLogging ( host : System , directory : string , cb : DirectoryWatcherCallback , flags : WatchDirectoryFlags , log : ( s : string ) => void ) : FileWatcher {
128- const watcherCaption = `DirectoryWatcher ${ ( flags & WatchDirectoryFlags . Recursive ) !== 0 ? "recursive" : "" } :: ` ;
129- return createWatcherWithLogging ( addDirectoryWatcher , watcherCaption , log , /*logOnlyTrigger*/ false , host , directory , cb , flags ) ;
130+ function watchDirectory ( host : System , directory : string , callback : DirectoryWatcherCallback , flags : WatchDirectoryFlags ) : FileWatcher {
131+ return host . watchDirectory ( directory , callback , ( flags & WatchDirectoryFlags . Recursive ) !== 0 ) ;
130132 }
131133
132- export function addDirectoryWatcherWithOnlyTriggerLogging ( host : System , directory : string , cb : DirectoryWatcherCallback , flags : WatchDirectoryFlags , log : ( s : string ) => void ) : FileWatcher {
133- const watcherCaption = `DirectoryWatcher ${ ( flags & WatchDirectoryFlags . Recursive ) !== 0 ? "recursive" : "" } :: ` ;
134- return createWatcherWithLogging ( addDirectoryWatcher , watcherCaption , log , /*logOnlyTrigger*/ true , host , directory , cb , flags ) ;
135- }
134+ export type WatchCallback < T , U > = ( fileName : string , cbOptional ?: T , passThrough ?: U ) => void ;
135+ export type AddWatch < T , U , V > = ( host : System , file : string , cb : WatchCallback < U , V > , flags : T , passThrough ?: V , detailInfo1 ?: undefined , detailInfo2 ?: undefined ) => FileWatcher ;
136+ export type GetDetailWatchInfo < X , Y > = ( detailInfo1 : X , detailInfo2 : Y ) => string ;
136137
137- type WatchCallback < T , U > = ( fileName : string , cbOptional1 ?: T , optional ?: U ) => void ;
138- type AddWatch < T , U > = ( host : System , file : string , cb : WatchCallback < T , U > , optional ?: U ) => FileWatcher ;
139- function createWatcherWithLogging < T , U > ( addWatch : AddWatch < T , U > , watcherCaption : string , log : ( s : string ) => void , logOnlyTrigger : boolean , host : System , file : string , cb : WatchCallback < T , U > , optional ?: U ) : FileWatcher {
140- const info = `PathInfo: ${ file } ` ;
141- if ( ! logOnlyTrigger ) {
142- log ( `${ watcherCaption } Added: ${ info } ` ) ;
138+ type CreateFileWatcher < T , U , V , X , Y > = ( host : System , file : string , cb : WatchCallback < U , V > , flags : T , passThrough : V | undefined , detailInfo1 : X | undefined , detailInfo2 : Y | undefined , addWatch : AddWatch < T , U , V > , log : ( s : string ) => void , watchCaption : string , getDetailWatchInfo : GetDetailWatchInfo < X , Y > | undefined ) => FileWatcher ;
139+ function getCreateFileWatcher < T , U , V , X , Y > ( watchLogLevel : WatchLogLevel , addWatch : AddWatch < T , U , V > ) : CreateFileWatcher < T , U , V , X , Y > {
140+ switch ( watchLogLevel ) {
141+ case WatchLogLevel . None :
142+ return addWatch ;
143+ case WatchLogLevel . TriggerOnly :
144+ return createFileWatcherWithLogging ;
145+ case WatchLogLevel . Verbose :
146+ return createFileWatcherWithTriggerLogging ;
143147 }
144- const watcher = addWatch ( host , file , ( fileName , cbOptional1 ?) => {
145- const optionalInfo = cbOptional1 !== undefined ? ` ${ cbOptional1 } ` : "" ;
146- log ( `${ watcherCaption } Trigger: ${ fileName } ${ optionalInfo } ${ info } ` ) ;
147- const start = timestamp ( ) ;
148- cb ( fileName , cbOptional1 , optional ) ;
149- const elapsed = timestamp ( ) - start ;
150- log ( `${ watcherCaption } Elapsed: ${ elapsed } ms Trigger: ${ fileName } ${ optionalInfo } ${ info } ` ) ;
151- } , optional ) ;
148+ }
149+
150+ function createFileWatcherWithLogging < T , U , V , X , Y > ( host : System , file : string , cb : WatchCallback < U , V > , flags : T , passThrough : V | undefined , detailInfo1 : X | undefined , detailInfo2 : Y | undefined , addWatch : AddWatch < T , U , undefined > , log : ( s : string ) => void , watchCaption : string , getDetailWatchInfo : GetDetailWatchInfo < X , Y > | undefined ) : FileWatcher {
151+ log ( `${ watchCaption } :: Added:: ${ getWatchInfo ( file , flags , detailInfo1 , detailInfo2 , getDetailWatchInfo ) } ` ) ;
152+ const watcher = createFileWatcherWithTriggerLogging ( host , file , cb , flags , passThrough , detailInfo1 , detailInfo2 , addWatch , log , watchCaption , getDetailWatchInfo ) ;
152153 return {
153154 close : ( ) => {
154- if ( ! logOnlyTrigger ) {
155- log ( `${ watcherCaption } Close: ${ info } ` ) ;
156- }
155+ log ( `${ watchCaption } :: Close:: ${ getWatchInfo ( file , flags , detailInfo1 , detailInfo2 , getDetailWatchInfo ) } ` ) ;
157156 watcher . close ( ) ;
158157 }
159158 } ;
160159 }
161160
161+ function createFileWatcherWithTriggerLogging < T , U , V , X , Y > ( host : System , file : string , cb : WatchCallback < U , V > , flags : T , passThrough : V | undefined , detailInfo1 : X | undefined , detailInfo2 : Y | undefined , addWatch : AddWatch < T , U , undefined > , log : ( s : string ) => void , watchCaption : string , getDetailWatchInfo : GetDetailWatchInfo < X , Y > | undefined ) : FileWatcher {
162+ return addWatch ( host , file , ( fileName , cbOptional ) => {
163+ const triggerredInfo = `${ watchCaption } :: Triggered with ${ fileName } ${ cbOptional !== undefined ? cbOptional : "" } :: ${ getWatchInfo ( file , flags , detailInfo1 , detailInfo2 , getDetailWatchInfo ) } ` ;
164+ log ( triggerredInfo ) ;
165+ const start = timestamp ( ) ;
166+ cb ( fileName , cbOptional , passThrough ) ;
167+ const elapsed = timestamp ( ) - start ;
168+ log ( `Elapsed:: ${ elapsed } ms ${ triggerredInfo } ` ) ;
169+ } , flags ) ;
170+ }
171+
172+ function getWatchInfo < T , X , Y > ( file : string , flags : T , detailInfo1 : X | undefined , detailInfo2 : Y | undefined , getDetailWatchInfo : GetDetailWatchInfo < X , Y > | undefined ) {
173+ return `WatchInfo: ${ file } ${ flags } ${ getDetailWatchInfo ? getDetailWatchInfo ( detailInfo1 , detailInfo2 ) : "" } `
174+ }
175+
162176 export function closeFileWatcher ( watcher : FileWatcher ) {
163177 watcher . close ( ) ;
164178 }
0 commit comments