-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.js
54 lines (49 loc) · 1.39 KB
/
extract.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
const fs = require('fs');
const writeJsonFile = require('write-json-file');
const util = require('util');
const commandLineArgs = require('command-line-args');
const options = commandLineArgs([
{
name: 'interesting-property',
alias: 'p',
type: String,
defaultOption: true,
defaultValue: '*',
},
{ name: 'conditional-property', alias: 'k', type: String },
{ name: 'conditional-value', alias: 'v', type: String },
]);
extract(
options['interesting-property'],
options['conditional-property'],
options['conditional-value'],
);
function extract(interestingProperty, conditionalProperty, conditionalValue) {
const result = [];
loadLogs().forEach(log => {
if (
log.hasOwnProperty(conditionalProperty) &&
log[conditionalProperty] === conditionalValue
) {
if (log.hasOwnProperty(interestingProperty)) {
result.push(log[interestingProperty]);
} else if (interestingProperty === '*') {
result.push(log);
}
}
});
writeResult(
result,
`res/${interestingProperty}_with_${conditionalProperty}_eq_${conditionalValue}.json`,
);
}
function loadLogs() {
return JSON.parse(fs.readFileSync('log/log.json'));
}
function writeResult(result, fileName) {
if (typeof fileName === 'undefined') {
console.log(util.inspect(result, false, null, true));
} else {
writeJsonFile.sync(fileName, result, { indent: 2 });
}
}