forked from acordiner92/npm-audit-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.js
40 lines (37 loc) · 1.05 KB
/
executor.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
/**
*
* Handles the fetching of the npm audit data
*
*/
const Executor = ({ exec, jsonParser, logger }) => {
const exceededRetries = (retry, retries) => retries >= retry;
const callNpm = async (retry, retries) =>
new Promise((resolve, reject) => {
exec('npm audit --json | cat', (error, stdOut) => {
const parsedStdOut = jsonParser.parse(stdOut);
if (error || parsedStdOut.error) {
if (exceededRetries(retry, retries)) {
logger.error(`Failed to fetch NPM audit after ${retry} retries`);
return reject(parsedStdOut.error || error);
}
return resolve(callNpm(retry, retries + 1));
}
return resolve(parsedStdOut);
});
});
/**
* Runs the npm audit command and then parses
* the json response
*
* @param {object} config
* @returns {object} parsed json audit result
*/
const runNpmAuditCommand = config => {
const { retry } = config;
return callNpm(retry, 0);
};
return {
runNpmAuditCommand
};
};
module.exports = Executor;