-
Notifications
You must be signed in to change notification settings - Fork 727
/
Copy pathexec.js
84 lines (67 loc) · 2.08 KB
/
exec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const spawn = require('child_process').spawn;
const TEST_RETRIES = 2; // Will retry 2 times on top of the regular one
const delay = (ms) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
/**
* Execute the `cmd` in a new process.
*/
const exec = (cmd, options = {}) => {
return new Promise((resolve, reject) => {
let stderr = '';
let stdout = '';
const command = spawn(cmd, [], {
shell: true,
stdio: 'inherit',
...options
});
const stderrClosed = !command.stderr ? Promise.resolve() : new Promise((resolve) => {
command.stderr.on('close', resolve);
});
const stdoutClosed = !command.stdout ? Promise.resolve() : new Promise((resolve) => {
command.stdout.on('close', resolve);
});
command.stderr?.on('data', (data) => {
stderr += data;
});
command.stdout?.on('data', (data) => {
stdout += data;
});
command.on('error', (err) => {
reject(err);
});
command.on('exit', async (code) => {
await Promise.all([stderrClosed, stdoutClosed]);
if (code) {
reject(new Error(`Exit Code: ${code}\n${stderr}`));
} else {
resolve({ stderr: stderr.trimEnd(), stdout: stdout.trimEnd() });
}
});
});
};
/**
* Execute a `command` retrying if `exitCode` is different than 0.
*/
const execWithRetry = async (command, options = {}, allowedRetries = TEST_RETRIES) => {
let retriesLeft = allowedRetries;
while (retriesLeft >= 0) {
try {
return await exec(command, options);
} catch (e) {
console.error(`Failed executing "${command}". Retries left: ${retriesLeft}.`);
if (retriesLeft === 0) {
throw e;
}
retriesLeft--;
await delay(1000 * Math.pow(2, allowedRetries - retriesLeft));
}
}
return Promise.resolve();
};
module.exports = {
exec,
execWithRetry
};