Skip to content

Commit 6e406d4

Browse files
committed
refactor: replace execSync with spawnSync for improved command execution and error handling
1 parent 66c8ef5 commit 6e406d4

1 file changed

Lines changed: 37 additions & 20 deletions

File tree

scripts/test.mjs

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,46 @@
1-
import { execSync } from 'node:child_process';
1+
import { spawnSync } from 'node:child_process';
22

33
const mode = process.argv[2] || 'all';
44
const env = Object.fromEntries(Object.entries(process.env).filter(([key]) => !key.startsWith('npm_')));
55

66
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);
2614
}
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;
2744
}
2845

2946
if (mode === 'js') {

0 commit comments

Comments
 (0)