import { spawn , SpawnOptions } from 'child_process' ; export async function waitForPortOpen ( host , port , seconds ) { const deadline = Date . now ( ) + seconds * 1000 ; do { if ( await isPortOpen ( host , port ) ) { return true ; } } while ( Date . now ( ) < deadline ) ; return false ; } export async function isPortOpen ( host , port ) { return ( await execCmd ( 'nc' , [ '-z' , host , port . toString ( ) ] , { silent : true } ) ) === 0 ; } function execCmd ( 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 as number ) ; } catch ( error ) { reject ( error ) ; } } ) ; } ) ; }