forked from unscrambl/browser-driver-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runNpmChildProcess.js
85 lines (74 loc) · 1.87 KB
/
runNpmChildProcess.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
85
'use strict';
/* eslint-disable no-console */
var spawn = require('child_process').spawn;
var MAX_RETRY_COUNT = 3;
module.exports = runNpmChildProcess;
function runNpmChildProcess(args, cachePath)
{
args.push('--cache-min=600000', '--no-optional', '--loglevel=error');
if (cachePath)
{
args.push('--cache=' + cachePath);
}
return retryNpmProcessIfItFails(args, MAX_RETRY_COUNT);
}
function retryNpmProcessIfItFails(processArgs, maxRetryCount)
{
return new Promise(function (resolve, reject)
{
var npmChildProcess = createNpmChildProcess(processArgs);
npmChildProcess.then(function ()
{
resolve();
}).catch(function (error)
{
maxRetryCount--;
if (maxRetryCount !== 0)
{
console.log('command failed, retrying...');
retryNpmProcessIfItFails(processArgs, maxRetryCount).then(function ()
{
resolve();
});
}
else
{
reject(error);
}
});
return npmChildProcess;
});
}
function createNpmChildProcess(processArgs)
{
var options = {
timeout: 600000
};
return new Promise(function (resolve, reject)
{
var task = spawn('npm', processArgs, options);
task.stdout.on('data', function (data)
{
console.log(data.toString());
});
task.stderr.on('data', function (data)
{
console.log(data.toString());
});
task.on('exit', function (code)
{
if (code !== 0)
{
reject(code);
}
else
{
resolve();
}
});
task.on('error', function (error)
{
reject(error);
});
});
}