import { exec , spawn , SpawnOptions } from 'child_process' ; export async function execCmdAndParseJson ( cmd , execOptions = { } , rejectWithOutput = false , pipeOutput = false ) { const [ output ] = await execCmd ( cmd , execOptions , rejectWithOutput , pipeOutput ) ; return JSON . parse ( output ) ; } export function execCmd ( cmd , execOptions = { } , rejectWithOutput = false , pipeOutput = false ) { return new Promise ( ( resolve , reject ) => { if ( process . env . CELOTOOL_VERBOSE === 'true' ) { console . debug ( '$ ' + cmd ) ; pipeOutput = true ; } exec ( cmd , { maxBuffer : 1024 * 10000 , ... execOptions } , ( err , stdout , stderr ) => { if ( pipeOutput ) { console . debug ( stdout . toString ( ) ) ; } if ( err || pipeOutput ) { console . error ( stderr . toString ( ) ) ; } if ( err ) { if ( rejectWithOutput ) { reject ( [ err , stdout . toString ( ) , stderr . toString ( ) ] ) ; } else { reject ( err ) ; } } else { resolve ( [ stdout . toString ( ) , stderr . toString ( ) ] ) ; } } ) ; } ) ; } export function spawnCmd ( cmd , args , options ? ) { return new Promise < number > ( async ( resolve , reject ) => { const { silent , ... spawnOptions } = options || { silent : false } ; if ( ! silent ) { console . debug ( '$ ' + [ cmd ] . concat ( args ) . join ( ' ' ) ) ; } const process = spawn ( cmd , args , { ... spawnOptions , stdio : silent ? 'ignore' : 'inherit' } ) ; process . on ( 'close' , ( code ) => { try { resolve ( code ) ; } catch ( error ) { reject ( error ) ; } } ) ; } ) ; } export function execCmdWithExitOnFailure ( cmd , options = { } , pipeOutput = false ) { return new Promise ( ( resolve , reject ) => { try { resolve ( execCmd ( cmd , options , false , pipeOutput ) ) ; } catch ( error ) { console . error ( error ) ; process . exit ( 1 ) ; reject ( error ) ; } } ) ; } export async function spawnCmdWithExitOnFailure ( cmd , args , options ? ) { const code = await spawnCmd ( cmd , args , options ) ; if ( code !== 0 ) { console . error ( 'spawnCmd failed for: ' + [ cmd ] . concat ( args ) . join ( ' ' ) ) ; process . exit ( 1 ) ; } } export function execBackgroundCmd ( cmd ) { if ( process . env . CELOTOOL_VERBOSE === 'true' ) { console . debug ( '$ ' + cmd ) ; } return exec ( cmd , { maxBuffer : 1024 * 10000 } , ( err , stdout , stderr ) => { if ( process . env . CELOTOOL_VERBOSE === 'true' ) { console . debug ( stdout ) ; console . error ( stderr ) ; } if ( err ) { console . error ( err ) ; process . exit ( 1 ) ; } } ) ; } export async function outputIncludes ( cmd , matchString , matchMessage ? ) { const [ stdout ] = await execCmdWithExitOnFailure ( cmd ) ; if ( stdout . includes ( matchString ) ) { if ( matchMessage ) { console . info ( matchMessage ) ; } return true ; } return false ; }