import * as fs from 'fs' ; import * as path from 'path' ; import * as child_process from 'child_process' ; import { RenderError } from './interfaces' ; export function processWrapper ( process , pipeFilePath ? ) { return new Promise < Buffer > ( ( resolve , reject ) => { let buffOut = [ ] ; let buffOutLen = 0 ; let buffErr = [ ] ; let buffErrLen = 0 ; process . stdout . on ( 'data' , function ( chunck ) { buffOut . push ( chunck ) ; buffOutLen += chunck . length ; } ) ; process . stderr . on ( 'data' , function ( chunck ) { buffErr . push ( chunck ) ; buffErrLen += chunck . length ; } ) ; process . stdout . on ( 'close' , ( ) => { let stdout = Buffer . concat ( buffOut , buffOutLen ) ; if ( pipeFilePath && stdout . length ) { fs . writeFileSync ( pipeFilePath , stdout ) ; stdout = Buffer . from ( pipeFilePath ) ; } let stderr = Buffer . concat ( buffErr , buffErrLen ) . toString ( ) ; stderr = stderr . replace ( /^Picked up .*$/mg , "" ) . trim ( ) ; if ( stderr ) { reject ( < RenderError > { error : stderr , out : stdout } ) ; return ; } resolve ( stdout ) ; } ) ; } ) ; }