forked from artemv/yarn-retry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
31 lines (27 loc) · 1019 Bytes
/
index.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
require('colors');
var exec = require('child_process').exec;
module.exports = function (command, args, options, callback) {
var times = 1;
function run () {
var runCmd = command + ' ' + (args || []).join(' ');
console.log(('attempt ' + times).bold.green + ': ' + runCmd);
process.env.npm_config_color = 0;
var attempt = exec(runCmd, function (err, stdout, stderr) {
matchers = [/unexpected end of file/ig, /the registry may be down/ig, /Request failed/ig];
var match = matchers.some(function (matcher) {
return stdout.match(matcher) || stderr.match(matcher);
});
if (match) {
if (times >= options.attempts) {
return callback(new Error('too many attempts'));
}
times++;
return setTimeout(run, options.wait);
}
return callback(null, {times: times, stdout: stdout, exitCode: (err && err.code) || 0});
});
attempt.stdout.pipe(process.stdout);
attempt.stderr.pipe(process.stderr);
}
run();
};