import Emitter from "../platform/cross/src/emitter" ; import Spawn from "../platform/node/src/process.spawn" ; enum EState { working = 0 , waining = 1 } export default class TypeScriptCompiler extends Emitter { public static Events = { error : Symbol ( ) , logs : Symbol ( ) , success : Symbol ( ) , } ; private state : EState = EState . waining ; constructor ( ) { super ( ) ; } public compiler ( folder , tsConfigFile = '' ) { return new Promise ( ( resolve , reject ) => { if ( this . state === EState . working ) { return reject ( new Error ( `Previous process isn't finished yet.` ) ) ; } const spawn = new Spawn ( ) ; spawn . subscribe ( Spawn . Events . error , ( error ) => { this . emit ( TypeScriptCompiler . Events . error , error ) ; if ( this . state === EState . working ) { spawn . kill ( ) ; this . state = EState . waining ; reject ( error ) ; } } ) ; spawn . subscribe ( Spawn . Events . stream , ( str ) => { this . emit ( TypeScriptCompiler . Events . logs , str ) ; } ) ; tsConfigFile = tsConfigFile === '' ? './tsconfig.json' : tsConfigFile ; spawn . execute ( { command : `tsc -p ${ tsConfigFile }` , getOriginalEnvVars : [ 'PATH' ] , options : { cwd : folder , } , } ) . then ( ( ) => { resolve ( ) ; } ) . catch ( ( error ) => { this . emit ( TypeScriptCompiler . Events . error , error ) ; if ( this . state === EState . working ) { this . state = EState . waining ; reject ( error ) ; } } ) ; } ) ; } }