import * as path from 'path' ; import * as ts from 'typescript' ; export interface FileReflection { name : string ; text : string ; } export class Host implements ts . CompilerHost { private fileMap : Map < string , ts . SourceFile > = new Map ( ) ; private outputs : Map < string , string > = new Map ( ) ; private defaultLibFileName = 'node_modules/typescript/lib/lib.d.ts' ; private defaultLibLocation = 'node_modules/typescript/lib/' ; private currentDirectory = '' ; private caseSensitiveFileNames = false ; private newLine = '\n' ; constructor ( files , private compilerOptions , setParentNodes = true ) { for ( let file of files ) { this . fileMap . set ( file . name , ts . createSourceFile ( file . name , file . text , compilerOptions . target , setParentNodes ) ) ; } } public getResult ( ) { const result = [ ] ; const cwd = process . cwd ( ) ; this . outputs . forEach ( ( text , name ) => { const sepRegExp = new RegExp ( `^\\${ path . sep }+` ) ; name = name . split ( `${ cwd }${ path . sep }` ) . join ( '' ) ; name = name . replace ( sepRegExp , '' ) ; result . push ( { name , text } ) ; } ) ; return result ; } public setDefaultLibFileName ( defaultLibFileName ) { this . defaultLibFileName = defaultLibFileName ; } public setDefaultLibLocation ( defaultLibLocation ) { this . defaultLibLocation = defaultLibLocation ; } public setCurrentDirectory ( currentDirectory ) { this . currentDirectory = currentDirectory ; } public setUseCaseSensitiveFileNames ( useCaseSensitiveFileNames ) { this . caseSensitiveFileNames = useCaseSensitiveFileNames ; } public setNewLine ( newLine ) { this . newLine = newLine ; } public getSourceFile ( fileName ) { return this . fileMap . get ( fileName ) ; } public getDefaultLibFileName ( options ) { return path . join ( path . resolve ( path . dirname ( this . defaultLibFileName ) ) , path . basename ( this . defaultLibFileName ) ) ; } public getDefaultLibLocation ( ) { return path . resolve ( this . defaultLibLocation ) ; } public getCurrentDirectory ( ) { return path . resolve ( this . currentDirectory ) ; } public getDirectories ( path ) { return [ ] ; } public getCanonicalFileName ( fileName ) { return fileName ; } public useCaseSensitiveFileNames ( ) { return this . caseSensitiveFileNames ; } public getNewLine ( ) { return this . newLine ; } public fileExists ( fileName ) { return this . fileMap . has ( fileName ) ; } public readFile ( fileName ) { return this . fileMap . has ( fileName ) ? this . fileMap . get ( fileName ) . text : undefined ; } public writeFile ( fileName , data , writeByteOrderMark ? , onError ? , sourceFiles ? ) { this . outputs . set ( fileName , data ) ; } }