-
Notifications
You must be signed in to change notification settings - Fork 0
/
caprese.js
74 lines (62 loc) · 2.15 KB
/
caprese.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
const path = require('path');
const fs = require('fs');
const { ArgumentParser } = require('argparse');
const { version } = require('./package.json');
const { exec } = require('child_process');
const constants = require('./constants.js');
const { computeCapreseResult } = require("./computeCapreseResult");
const parser = new ArgumentParser({
add_help: true,
description: `Node.js dynamic dependency graph extractor version ${version}`,
});
parser.add_argument('-v', '--vaersion', { action: 'version', version: version})
let changeSet = []
if (!fs.existsSync(constants.DATA_PATH)) {
fs.mkdirSync(constants.DATA_PATH, {
recursive: true
});
}
if (process.argv[1].endsWith(path.basename(__filename))) {
if (process.argv[2] == "extract") {
daCommand(constants.SEED_COMMIT)
} else if (process.argv[2] == "detect") {
changeSet = process.argv[3].split(" ")
console.log(changeSet)
detectCommand(changeSet)
} else {
console.error("Invalid command.")
console.error("Please specify either 'da' or 'detect'")
}
}
async function daCommand(commit) {
await checkoutProject(commit)
await runDynamicAnalysis()
}
async function detectCommand(changeSet) {
console.log(" = = = Run Caprese = = = ")
computeCapreseResult(changeSet, "capreseResult.json")
}
async function checkoutProject(commit) {
console.log(" = = = Checkout Project = = = ")
const checkoutCommand = `cd ${constants.REPO_PATH}\ngit checkout ${commit}`
return runCommand(checkoutCommand)
}
async function runDynamicAnalysis() {
console.log(" = = = Run Dynamic Anlaysis = = = ")
console.log(constants.DA_COMMAND)
return runCommand(constants.DA_COMMAND, true)
}
function runCommand(command, logReport = false) {
return new Promise((resolve, reject) => {
exec(command, (err, stdout, stderr) => {
if (!err) {
resolve(stdout.trimEnd())
if (logReport) console.log(stdout.trimEnd())
} else {
reject(err)
if (logReport) console.log(stderr.trimEnd())
}
})
})
}
module.exports = { runDynamicAnalysis }