import * as ts from 'typescript' ; import { HostBase } from './hosts-base' ; import { defaultCompilerOptions } from './configuration' ; import { RawSourceMap } from 'source-map' ; function fileExtensionIs ( path , extension ) { let pathLen = path . length ; let extLen = extension . length ; return pathLen > extLen && path . substr ( pathLen - extLen , extLen ) === extension ; } export class MultipleFilesHost extends HostBase implements ts . CompilerHost { private syntacticErrors : ts . Diagnostic [ ] = [ ] ; constructor ( private _resolutionHosts , private _compilerOptions = defaultCompilerOptions ) { super ( ) ; } getCurrentDirectory ( ) { let currentDir = '' ; this . _resolutionHosts . forEach ( ( host ) => { if ( host . getCurrentDirectory ) { const hostCurrentDir = host . getCurrentDirectory ( ) ; if ( hostCurrentDir ) { currentDir = hostCurrentDir ; } } } ) ; return currentDir ; } directoryExists ( directoryName ) { if ( directoryName === '' ) { return true ; } return this . _resolutionHosts . some ( host => host . directoryExists && host . directoryExists ( directoryName ) ) ; } getDirectories ( path ) { return this . _resolutionHosts . reduce ( ( directories , host ) => { return host . getDirectories ? directories . concat ( host . getDirectories ( path ) ) : directories ; } , [ ] as string [ ] ) ; } fileExists ( fileName ) { return this . _resolutionHosts . some ( host => host . fileExists ( fileName ) ) ; } readFile ( fileName ) { return this . _resolutionHosts . reduce < string > ( ( acc , host ) => ( ! acc && host . fileExists ( fileName ) ) ? host . readFile ( fileName ) : acc , null ) ; } getSourceFile ( fileName ) { const source = this . readFile ( fileName ) ; if ( source ) { const ast = ts . createSourceFile ( fileName , source , this . _compilerOptions . target , true ) ; const syntacticErors = this . getParserErrors ( ast ) ; if ( syntacticErors . length > 0 ) { this . syntacticErrors . push ( ... syntacticErors ) ; return null ; } else { return ast ; } } else { return null ; } } getSyntacticErrors ( ) { return this . syntacticErrors ; } private getParserErrors ( sourceFile ) { return sourceFile [ 'parseDiagnostics' ] ; } } export class SingleFileHost extends HostBase implements ts . CompilerHost { private _output : string = '' ; private _map : string = null ; private _declaration : string = '' ; constructor ( private _ast ) { super ( ) ; } public get output ( ) : string { return this . _output ; } public get declaration ( ) : string { return this . _declaration ; } public get sourceMap ( ) : RawSourceMap { return JSON . parse ( this . _map ) ; } fileExists ( fileName ) { return fileName === this . _ast . fileName ; } readFile ( fileName ) { if ( fileName === this . _ast . fileName ) { return this . _ast . text ; } } getSourceFile ( fileName ) { if ( fileName === this . _ast . fileName ) { return this . _ast ; } } writeFile ( name , text , writeByteOrderMark ) { if ( fileExtensionIs ( name , 'map' ) ) { this . _map = text ; } else if ( fileExtensionIs ( name , 'd.ts' ) ) { this . _declaration = text ; } else { this . _output = text ; } } }