import { readdir } from 'fs' ; import { join } from 'path' ; import { spawn } from 'child_process' ; import { promisify } from 'util' ; const readdirp = promisify ( readdir ) ; function spawnp ( command , parameters ) { return new Promise < void > ( ( resolve , reject ) => { const proc = spawn ( command , parameters , { stdio : [ 'inherit' , 'inherit' , 'inherit' ] , } ) ; proc . on ( 'exit' , code => { if ( code === 0 ) { resolve ( ) ; } reject ( ) ; } ) ; } ) ; } const testDir = join ( '.' , 'build' , 'test' ) ; const runCommand = 'node' ; const runParameters = [ './node_modules/mocha/bin/mocha' ] ; const batchSize = 2 ; async function runBatch ( batch ) { const parameters = [ ... runParameters , ... batch ] ; console . log ( `${ runCommand } ${ parameters . join ( ' ' ) }` ) ; await spawnp ( runCommand , parameters ) ; } async function main ( ) { const files = await readdirp ( testDir ) ; const jsFiles = files . filter ( fn => fn . match ( /^gapic_.*\.js$/ ) ) ; const batch = [ ] ; for ( const file of jsFiles ) { batch . push ( join ( testDir , file ) ) ; if ( batch . length >= batchSize ) { await runBatch ( batch ) ; batch . splice ( 0 ) ; } } if ( batch . length > 0 ) { await runBatch ( batch ) ; batch . splice ( 0 ) ; } } main ( ) . catch ( ( ) => { process . exitCode = 1 ; } ) ;