( command: string, args: string[], predicate: (stdout: string) => boolean )
| 51 | } |
| 52 | |
| 53 | function spawnAndWaitForStdout( |
| 54 | command: string, |
| 55 | args: string[], |
| 56 | predicate: (stdout: string) => boolean |
| 57 | ): DeferredPromise<ChildProcess> { |
| 58 | const promise = new DeferredPromise<ChildProcess>(); |
| 59 | const io = spawn(command, args, { |
| 60 | env: { |
| 61 | ...process.env, |
| 62 | NODE_ENV: 'test', |
| 63 | }, |
| 64 | }); |
| 65 | let stdout = Buffer.from(''); |
| 66 | |
| 67 | io.stdout.on('data', (raw: Buffer) => { |
| 68 | console.log(raw.toString('utf8')); |
| 69 | stdout = Buffer.concat([stdout, raw]); |
| 70 | |
| 71 | if (predicate(raw.toString('utf8'))) { |
| 72 | promise.resolve(io); |
| 73 | } |
| 74 | }); |
| 75 | |
| 76 | io.stderr.on('data', (raw: Buffer) => { |
| 77 | console.error(stdout.toString('utf8')); |
| 78 | promise.reject(new Error(raw.toString('utf8'))); |
| 79 | io.kill(1); |
| 80 | }); |
| 81 | |
| 82 | io.once('error', (error) => promise.reject(error)); |
| 83 | io.once('exit', (code) => { |
| 84 | if (code !== 0) { |
| 85 | promise.reject(new Error(format('Process "%s %j" exited with code %d', command, args, code))); |
| 86 | } |
| 87 | }); |
| 88 | |
| 89 | return promise; |
| 90 | } |
no test coverage detected