import * as path from 'path' ; import { spawn } from 'child_process' ; let electron ; try { electron = require ( 'electron' ) as unknown as string ; } catch ( err ) { } interface FlossOptions { path : string ; debug ? : boolean ; electron ? : string ; reporter ? : string ; reporterOptions ? : any ; require ? : string ; args ? : string [ ] ; } module param { Object | String } options param { String } options . path param { Boolean } options . debug param { String } options . electron param { String } options . require param { String } options . reporter param { String | Object } options . reporterOptions param { String [ ] } options . args param { Function } done function floss ( options ) { if ( typeof options === 'string' ) { options = { path : options } ; } const opts = Object . assign ( { path : '' , debug : false , quiet : false , args : [ ] , electron : process . env . ELECTRON_PATH || electron } , options ) ; if ( ! opts . path ) { throw new Error ( 'No path specified for Floss.' ) ; } else if ( typeof opts . path !== 'string' ) { throw new Error ( 'Path type is not a string' ) ; } if ( ! opts . electron ) { throw new Error ( 'Unable to find Electron. Install \'electron\' alongside Floss.' ) ; } const app = path . join ( __dirname , 'main' ) ; const args = JSON . stringify ( opts ) ; const envCopy = Object . assign ( { } , process . env ) ; delete envCopy . ELECTRON_RUN_AS_NODE ; delete envCopy . ELECTRON_NO_ATTACH_CONSOLE ; return new Promise ( ( resolve , reject ) => { let execPath = opts . electron as string ; const isWindows = ( /^win/ ) . test ( process . platform ) ; if ( isWindows && ! path . extname ( execPath ) ) { execPath += '.cmd' ; } const childProcess = spawn ( execPath , [ app , args , ... opts . args as string [ ] ] , { stdio : 'pipe' , env : envCopy } ) ; childProcess . stdout . on ( 'data' , ( data ) => { process . stdout . write ( data ) ; } ) ; childProcess . on ( 'close' , ( code ) => { if ( code !== 0 ) { reject ( new Error ( 'Mocha tests failed.' ) ) ; } else { resolve ( ) ; } } ) ; } ) ; } export { floss } ; export type { FlossOptions } ;