import ts from 'typescript' ; import Logger from './logger' ; const logger = new Logger ( { debug : false } ) ; export type TranspileResult = { failure : boolean ; diags : Array < ts . Diagnostic > ; js : string ; sourceMap : string ; } ; export interface SourceFile extends ts . SourceFile { output ? : TranspileResult ; } export class CompilerHost implements ts . CompilerHost { private _files : { [ s ] : SourceFile ; } ; constructor ( ) { this . _files = { } ; } public getDefaultLibFileName ( options ) { return this . getDefaultLibFilePaths ( options ) [ 0 ] ; } public getDefaultLibFilePaths ( options ) { return options . lib ? options . lib . map ( libName => `typescript/lib/lib.${ libName }.d.ts` ) : [ 'typescript/lib/lib.d.ts' ] ; } public useCaseSensitiveFileNames ( ) { return false ; } public getCanonicalFileName ( fileName ) { return ( ts as any ) . normalizePath ( fileName ) ; } public getCurrentDirectory ( ) { return "" ; } public getNewLine ( ) { return "\n" ; } public readFile ( fileName ) { throw new Error ( "Not implemented" ) ; } public writeFile ( name , text , writeByteOrderMark ) { throw new Error ( "Not implemented" ) ; } public getSourceFile ( fileName ) { fileName = this . getCanonicalFileName ( fileName ) ; return this . _files [ fileName ] ; } public getAllFiles ( ) { return Object . keys ( this . _files ) . map ( key => this . _files [ key ] ) ; } public fileExists ( fileName ) { return ! ! this . getSourceFile ( fileName ) ; } public getDirectories ( ) { throw new Error ( "Not implemented" ) ; } public addFile ( fileName , text , target ) { fileName = this . getCanonicalFileName ( fileName ) ; const file = this . _files [ fileName ] ; if ( ! file ) { this . _files [ fileName ] = ts . createSourceFile ( fileName , text , target ) ; logger . debug ( `added ${ fileName }` ) ; } else if ( file . text != text ) { this . _files [ fileName ] = ts . createSourceFile ( fileName , text , target ) ; logger . debug ( `updated ${ fileName }` ) ; } return this . _files [ fileName ] ; } }