|
1 | | -import { execSync } from 'node:child_process'; |
| 1 | +import { spawnSync } from 'node:child_process'; |
2 | 2 |
|
3 | 3 | const mode = process.argv[2] || 'all'; |
4 | 4 | const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('npm_'))); |
5 | 5 |
|
6 | 6 | function run(cmd, allowRetry = false) { |
7 | | - try { |
8 | | - execSync(cmd, { |
9 | | - cwd: process.cwd(), |
10 | | - stdio: 'inherit', |
11 | | - env, |
12 | | - shell: '/bin/bash', |
13 | | - }); |
14 | | - } catch (err) { |
15 | | - const msg = String(err && err.message || err || ''); |
16 | | - if (allowRetry && /ECONNRESET/.test(msg)) { |
17 | | - execSync(cmd, { |
18 | | - cwd: process.cwd(), |
19 | | - stdio: 'inherit', |
20 | | - env, |
21 | | - shell: '/bin/bash', |
22 | | - }); |
23 | | - return; |
24 | | - } |
25 | | - throw err; |
| 7 | + const first = runOnce(cmd); |
| 8 | + if (first.status === 0) return; |
| 9 | + if (allowRetry && /\bECONNRESET\b/.test(resultText(first))) { |
| 10 | + process.stderr.write('\nRetrying after transient ECONNRESET from browser/relay test transport...\n'); |
| 11 | + const second = runOnce(cmd); |
| 12 | + if (second.status === 0) return; |
| 13 | + throw commandError(cmd, second); |
26 | 14 | } |
| 15 | + throw commandError(cmd, first); |
| 16 | +} |
| 17 | + |
| 18 | +function runOnce(cmd) { |
| 19 | + const result = spawnSync(cmd, { |
| 20 | + cwd: process.cwd(), |
| 21 | + env, |
| 22 | + shell: '/bin/bash', |
| 23 | + encoding: 'utf8', |
| 24 | + maxBuffer: 64 * 1024 * 1024, |
| 25 | + }); |
| 26 | + if (result.stdout) process.stdout.write(result.stdout); |
| 27 | + if (result.stderr) process.stderr.write(result.stderr); |
| 28 | + if (result.error) throw result.error; |
| 29 | + return result; |
| 30 | +} |
| 31 | + |
| 32 | +function resultText(result) { |
| 33 | + return `${result.stdout || ''}\n${result.stderr || ''}`; |
| 34 | +} |
| 35 | + |
| 36 | +function commandError(cmd, result) { |
| 37 | + const err = new Error(`Command failed: ${cmd}`); |
| 38 | + err.status = result.status; |
| 39 | + err.signal = result.signal; |
| 40 | + err.output = [null, result.stdout, result.stderr]; |
| 41 | + err.stdout = result.stdout; |
| 42 | + err.stderr = result.stderr; |
| 43 | + return err; |
27 | 44 | } |
28 | 45 |
|
29 | 46 | if (mode === 'js') { |
|
0 commit comments