generated from Kaholo/kaholo-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.js
62 lines (54 loc) · 1.3 KB
/
helpers.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
const util = require("util");
const childProcess = require("child_process");
async function asyncExec(params) {
const {
command,
onProgressFn,
options = {},
} = params;
let childProcessInstance;
try {
childProcessInstance = childProcess.exec(command, options);
} catch (error) {
throw new Error(error);
}
const dataChunks = [];
const errorChunks = [];
childProcessInstance.stdout.on("data", (data) => {
onProgressFn?.(data);
dataChunks.push(data);
});
childProcessInstance.stderr.on("data", (data) => {
onProgressFn?.(data);
const messagePrefix = "checkov: error: ";
errorChunks.push(data.split(messagePrefix)[1]);
});
try {
await util.promisify(childProcessInstance.on.bind(childProcessInstance))("close");
} catch (error) {
const resultObj = tryParseJson(dataChunks.join(''));
if (error === 1 && resultObj) {
throw resultObj;
}
const errorObj = {
exit_code: error,
error_message: errorChunks?.join('') || "See Activity Log",
};
throw errorObj;
}
const resultObj = tryParseJson(dataChunks.join(''));
if (resultObj) {
return resultObj;
}
return "";
}
function tryParseJson(value) {
try {
return JSON.parse(value);
} catch {
return undefined;
}
}
module.exports = {
asyncExec,
};