import * as ts from "typescript" ; import * as path from "path" ; import { TsCore } from "../Utils/TsCore" ; import { Utils } from "../Utils/Utilities" ; export class CachingCompilerHost implements ts . CompilerHost { protected system : ts . System = ts . sys ; private output : ts . MapLike < string > = { } ; private dirExistsCache : ts . MapLike < boolean > = { } ; private fileExistsCache : ts . MapLike < boolean > = { } ; private fileReadCache : ts . MapLike < string > = { } ; protected compilerOptions : ts . CompilerOptions ; protected baseHost : ts . CompilerHost ; constructor ( compilerOptions ) { this . compilerOptions = compilerOptions ; this . baseHost = ts . createIncrementalCompilerHost ( this . compilerOptions , this . system ) ; } public getOutput = ( ) => { return this . output ; } ; public readFile = ( fileName ) : string => { if ( this . isBuildInfoFile ( fileName ) ) { return this . baseHost . readFile ( fileName ) ; } if ( Utils . hasProperty ( this . fileReadCache , fileName ) ) { return this . fileReadCache [ fileName ] ; } return this . fileReadCache [ fileName ] = this . baseHost . readFile ( fileName ) ; } ; public writeFile = ( fileName , data , writeByteOrderMark , onError ? ) => { if ( this . isBuildInfoFile ( fileName ) ) { return this . baseHost . writeFile ( fileName , data , writeByteOrderMark ) ; } this . baseHost . writeFile ( fileName , data , writeByteOrderMark ) ; this . output [ fileName ] = data ; } ; public directoryExists = ( directoryPath ) : boolean => { if ( Utils . hasProperty ( this . dirExistsCache , directoryPath ) ) { return this . dirExistsCache [ directoryPath ] ; } return this . dirExistsCache [ directoryPath ] = ts . sys . directoryExists ( directoryPath ) ; } ; public fileExists = ( fileName ) : boolean => { fileName = this . getCanonicalFileName ( fileName ) ; if ( ! this . directoryExists ( path . dirname ( fileName ) ) ) { return false ; } if ( Utils . hasProperty ( this . fileExistsCache , fileName ) ) { return this . fileExistsCache [ fileName ] ; } return this . fileExistsCache [ fileName ] = this . baseHost . fileExists ( fileName ) ; } ; public getSourceFile = ( fileName , languageVersion , onError ? ) : ts . SourceFile => { return this . baseHost . getSourceFile ( fileName , languageVersion , onError ) ; } ; public getDefaultLibFileName = ( options ) => { return this . baseHost . getDefaultLibFileName ( options ) ; } ; public getCurrentDirectory = ( ) => { return this . baseHost . getCurrentDirectory ( ) ; } ; public getDirectories = ( path ) : string [ ] => { return this . baseHost . getDirectories ( path ) ; } ; public getCanonicalFileName = ( fileName ) => { return this . baseHost . getCanonicalFileName ( fileName ) ; } ; public useCaseSensitiveFileNames = ( ) => { return this . baseHost . useCaseSensitiveFileNames ( ) ; } ; public getNewLine = ( ) => { return this . baseHost . getNewLine ( ) ; } ; public readDirectory ( rootDir , extensions ? , exclude ? , include ? , depth ? ) { return this . baseHost . readDirectory ( rootDir , extensions , exclude , include , depth ) ; } public trace ( s ) { console . log ( s ) ; } protected isBuildInfoFile ( file ) { return TsCore . fileExtensionIs ( file , ts . Extension . TsBuildInfo . toString ( ) ) ; } }