@@ -194,6 +194,30 @@ namespace ts {
194
194
} ;
195
195
}
196
196
197
+ export const enum WatchType {
198
+ ConfigFile = "Config file" ,
199
+ SourceFile = "Source file" ,
200
+ MissingFile = "Missing file" ,
201
+ WildcardDirectory = "Wild card directory" ,
202
+ FailedLookupLocations = "Failed Lookup Locations" ,
203
+ TypeRoots = "Type roots"
204
+ }
205
+
206
+ interface WatchFactory < X , Y = undefined > extends ts . WatchFactory < X , Y > {
207
+ watchLogLevel : WatchLogLevel ;
208
+ writeLog : ( s : string ) => void ;
209
+ }
210
+
211
+ export function createWatchFactory < Y = undefined > ( host : { trace ?( s : string ) : void ; } , options : { extendedDiagnostics ?: boolean ; diagnostics ?: boolean ; } ) {
212
+ const watchLogLevel = host . trace ? options . extendedDiagnostics ? WatchLogLevel . Verbose :
213
+ options . diagnostics ? WatchLogLevel . TriggerOnly : WatchLogLevel . None : WatchLogLevel . None ;
214
+ const writeLog : ( s : string ) => void = watchLogLevel !== WatchLogLevel . None ? ( s => host . trace ! ( s ) ) : noop ;
215
+ const result = getWatchFactory < WatchType , Y > ( watchLogLevel , writeLog ) as WatchFactory < WatchType , Y > ;
216
+ result . watchLogLevel = watchLogLevel ;
217
+ result . writeLog = writeLog ;
218
+ return result ;
219
+ }
220
+
197
221
/**
198
222
* Creates the watch compiler host that can be extended with config file or root file names and options host
199
223
*/
@@ -224,7 +248,7 @@ namespace ts {
224
248
watchDirectory,
225
249
setTimeout,
226
250
clearTimeout,
227
- trace : s => system . write ( s ) ,
251
+ trace : s => system . write ( s + system . newLine ) ,
228
252
onWatchStatusChange,
229
253
createDirectory : path => system . createDirectory ( path ) ,
230
254
writeFile : ( path , data , writeByteOrderMark ) => system . writeFile ( path , data , writeByteOrderMark ) ,
@@ -517,17 +541,12 @@ namespace ts {
517
541
newLine = updateNewLine ( ) ;
518
542
}
519
543
520
- const trace = host . trace && ( ( s : string ) => { host . trace ! ( s + newLine ) ; } ) ;
521
- const watchLogLevel = trace ? compilerOptions . extendedDiagnostics ? WatchLogLevel . Verbose :
522
- compilerOptions . diagnostics ? WatchLogLevel . TriggerOnly : WatchLogLevel . None : WatchLogLevel . None ;
523
- const writeLog : ( s : string ) => void = watchLogLevel !== WatchLogLevel . None ? trace ! : noop ; // TODO: GH#18217
524
- const { watchFile, watchFilePath, watchDirectory } = getWatchFactory < string > ( watchLogLevel , writeLog ) ;
525
-
544
+ const { watchFile, watchFilePath, watchDirectory, watchLogLevel, writeLog } = createWatchFactory < string > ( host , compilerOptions ) ;
526
545
const getCanonicalFileName = createGetCanonicalFileName ( useCaseSensitiveFileNames ) ;
527
546
528
547
writeLog ( `Current directory: ${ currentDirectory } CaseSensitiveFileNames: ${ useCaseSensitiveFileNames } ` ) ;
529
548
if ( configFileName ) {
530
- watchFile ( host , configFileName , scheduleProgramReload , PollingInterval . High , "Config file" ) ;
549
+ watchFile ( host , configFileName , scheduleProgramReload , PollingInterval . High , WatchType . ConfigFile ) ;
531
550
}
532
551
533
552
const compilerHost : CompilerHost & ResolutionCacheHost = {
@@ -543,7 +562,7 @@ namespace ts {
543
562
getNewLine : ( ) => newLine ,
544
563
fileExists,
545
564
readFile,
546
- trace,
565
+ trace : host . trace && ( s => host . trace ! ( s ) ) ,
547
566
directoryExists : directoryStructureHost . directoryExists && ( path => directoryStructureHost . directoryExists ! ( path ) ) ,
548
567
getDirectories : ( directoryStructureHost . getDirectories && ( ( path : string ) => directoryStructureHost . getDirectories ! ( path ) ) ) ! , // TODO: GH#18217
549
568
realpath : host . realpath && ( s => host . realpath ! ( s ) ) ,
@@ -553,8 +572,8 @@ namespace ts {
553
572
// Members for ResolutionCacheHost
554
573
toPath,
555
574
getCompilationSettings : ( ) => compilerOptions ,
556
- watchDirectoryOfFailedLookupLocation : ( dir , cb , flags ) => watchDirectory ( host , dir , cb , flags , "Failed Lookup Locations" ) ,
557
- watchTypeRootsDirectory : ( dir , cb , flags ) => watchDirectory ( host , dir , cb , flags , "Type roots" ) ,
575
+ watchDirectoryOfFailedLookupLocation : ( dir , cb , flags ) => watchDirectory ( host , dir , cb , flags , WatchType . FailedLookupLocations ) ,
576
+ watchTypeRootsDirectory : ( dir , cb , flags ) => watchDirectory ( host , dir , cb , flags , WatchType . TypeRoots ) ,
558
577
getCachedDirectoryStructureHost : ( ) => cachedDirectoryStructureHost ,
559
578
onInvalidatedResolution : scheduleProgramUpdate ,
560
579
onChangedAutomaticTypeDirectiveNames : ( ) => {
@@ -719,7 +738,7 @@ namespace ts {
719
738
( hostSourceFile as FilePresentOnHost ) . sourceFile = sourceFile ;
720
739
sourceFile . version = hostSourceFile . version . toString ( ) ;
721
740
if ( ! ( hostSourceFile as FilePresentOnHost ) . fileWatcher ) {
722
- ( hostSourceFile as FilePresentOnHost ) . fileWatcher = watchFilePath ( host , fileName , onSourceFileChange , PollingInterval . Low , path , "Source file" ) ;
741
+ ( hostSourceFile as FilePresentOnHost ) . fileWatcher = watchFilePath ( host , fileName , onSourceFileChange , PollingInterval . Low , path , WatchType . SourceFile ) ;
723
742
}
724
743
}
725
744
else {
@@ -733,7 +752,7 @@ namespace ts {
733
752
else {
734
753
if ( sourceFile ) {
735
754
sourceFile . version = initialVersion . toString ( ) ;
736
- const fileWatcher = watchFilePath ( host , fileName , onSourceFileChange , PollingInterval . Low , path , "Source file" ) ;
755
+ const fileWatcher = watchFilePath ( host , fileName , onSourceFileChange , PollingInterval . Low , path , WatchType . SourceFile ) ;
737
756
sourceFilesCache . set ( path , { sourceFile, version : initialVersion , fileWatcher } ) ;
738
757
}
739
758
else {
@@ -907,7 +926,7 @@ namespace ts {
907
926
}
908
927
909
928
function watchMissingFilePath ( missingFilePath : Path ) {
910
- return watchFilePath ( host , missingFilePath , onMissingFileChange , PollingInterval . Medium , missingFilePath , "Missing file" ) ;
929
+ return watchFilePath ( host , missingFilePath , onMissingFileChange , PollingInterval . Medium , missingFilePath , WatchType . MissingFile ) ;
911
930
}
912
931
913
932
function onMissingFileChange ( fileName : string , eventKind : FileWatcherEventKind , missingFilePath : Path ) {
@@ -971,7 +990,7 @@ namespace ts {
971
990
}
972
991
} ,
973
992
flags ,
974
- "Wild card directories"
993
+ WatchType . WildcardDirectory
975
994
) ;
976
995
}
977
996
0 commit comments