import fs from 'fs' ; import child_process = require ( 'child_process' ) ; import tmp from 'tmp' ; import commandExists from 'command-exists' ; export function strContains ( word , pattern ) { return word . indexOf ( pattern ) > - 1 ; } export function strEquals ( word , pattern ) { return word . toLowerCase ( ) == pattern . toLowerCase ( ) ; } export function parseCmdArgs ( text ) { const re = /^"[^"]*"$/ ; const re2 = /^([^"]|[^"].*?[^"])$/ ; const arr = [ ] ; let argPart = null ; text && text . split ( ' ' ) . forEach ( ( arg ) => { if ( ( re . test ( arg ) || re2 . test ( arg ) ) && ! argPart ) { arr . push ( arg ) ; } else { argPart = argPart ? argPart + ' ' + arg : arg ; if ( /"$/ . test ( argPart ) ) { arr . push ( argPart ) ; argPart = null ; } } } ) ; return arr ; } export async function fsCreateTmpfile ( ) { return new Promise ( ( resolve , reject ) => { tmp . file ( ( err , path ) => { if ( err ) reject ( new Error ( 'Failed to create a tmp file' ) ) ; resolve ( path ) ; } ) ; } ) ; } export async function fsWriteFile ( fullpath , content ) { return new Promise ( ( resolve , reject ) => { fs . writeFile ( fullpath , content , 'utf8' , ( err ) => { if ( err ) reject ( ) ; resolve ( ) ; } ) ; } ) ; } export async function runCommand ( command , args ? ) { return new Promise ( ( resolve , reject ) => { child_process . execFile ( command , args , ( error , stdout ) => { if ( error ) reject ( error ) ; resolve ( stdout ) ; } ) ; } ) ; } export async function checkCommand ( command ) { return new Promise ( ( resolve ) => { commandExists ( command , ( _err , exists ) => { resolve ( exists ) ; } ) ; } ) ; }