@@ -194,6 +194,30 @@ namespace ts {
194194 } ;
195195 }
196196
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+
197221 /**
198222 * Creates the watch compiler host that can be extended with config file or root file names and options host
199223 */
@@ -224,7 +248,7 @@ namespace ts {
224248 watchDirectory,
225249 setTimeout,
226250 clearTimeout,
227- trace : s => system . write ( s ) ,
251+ trace : s => system . write ( s + system . newLine ) ,
228252 onWatchStatusChange,
229253 createDirectory : path => system . createDirectory ( path ) ,
230254 writeFile : ( path , data , writeByteOrderMark ) => system . writeFile ( path , data , writeByteOrderMark ) ,
@@ -517,17 +541,12 @@ namespace ts {
517541 newLine = updateNewLine ( ) ;
518542 }
519543
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 ) ;
526545 const getCanonicalFileName = createGetCanonicalFileName ( useCaseSensitiveFileNames ) ;
527546
528547 writeLog ( `Current directory: ${ currentDirectory } CaseSensitiveFileNames: ${ useCaseSensitiveFileNames } ` ) ;
529548 if ( configFileName ) {
530- watchFile ( host , configFileName , scheduleProgramReload , PollingInterval . High , "Config file" ) ;
549+ watchFile ( host , configFileName , scheduleProgramReload , PollingInterval . High , WatchType . ConfigFile ) ;
531550 }
532551
533552 const compilerHost : CompilerHost & ResolutionCacheHost = {
@@ -543,7 +562,7 @@ namespace ts {
543562 getNewLine : ( ) => newLine ,
544563 fileExists,
545564 readFile,
546- trace,
565+ trace : host . trace && ( s => host . trace ! ( s ) ) ,
547566 directoryExists : directoryStructureHost . directoryExists && ( path => directoryStructureHost . directoryExists ! ( path ) ) ,
548567 getDirectories : ( directoryStructureHost . getDirectories && ( ( path : string ) => directoryStructureHost . getDirectories ! ( path ) ) ) ! , // TODO: GH#18217
549568 realpath : host . realpath && ( s => host . realpath ! ( s ) ) ,
@@ -553,8 +572,8 @@ namespace ts {
553572 // Members for ResolutionCacheHost
554573 toPath,
555574 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 ) ,
558577 getCachedDirectoryStructureHost : ( ) => cachedDirectoryStructureHost ,
559578 onInvalidatedResolution : scheduleProgramUpdate ,
560579 onChangedAutomaticTypeDirectiveNames : ( ) => {
@@ -719,7 +738,7 @@ namespace ts {
719738 ( hostSourceFile as FilePresentOnHost ) . sourceFile = sourceFile ;
720739 sourceFile . version = hostSourceFile . version . toString ( ) ;
721740 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 ) ;
723742 }
724743 }
725744 else {
@@ -733,7 +752,7 @@ namespace ts {
733752 else {
734753 if ( sourceFile ) {
735754 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 ) ;
737756 sourceFilesCache . set ( path , { sourceFile, version : initialVersion , fileWatcher } ) ;
738757 }
739758 else {
@@ -907,7 +926,7 @@ namespace ts {
907926 }
908927
909928 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 ) ;
911930 }
912931
913932 function onMissingFileChange ( fileName : string , eventKind : FileWatcherEventKind , missingFilePath : Path ) {
@@ -971,7 +990,7 @@ namespace ts {
971990 }
972991 } ,
973992 flags ,
974- "Wild card directories"
993+ WatchType . WildcardDirectory
975994 ) ;
976995 }
977996
0 commit comments