-
Notifications
You must be signed in to change notification settings - Fork 61
/
xml2json.ts
57 lines (54 loc) · 1.69 KB
/
xml2json.ts
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
import fs from 'fs';
import * as _ from 'lodash';
import xml2js from 'xml2js';
const parser = new xml2js.Parser();
const pathToInfile = process.argv[2];
const pathToOutfile = process.argv[3];
// XML Structure after conversion
export interface ICCIList {
cci_list: {
cci_items: {
cci_item: {
$: Record<string, string>;
references: {
reference: {
$: Record<string, string>;
}[];
}[];
}[];
}[];
};
}
if (!pathToInfile || !pathToOutfile) {
console.error(`You must provide the path to both an input and ouput file.`);
} else {
fs.readFile(pathToInfile, function (readFileError, data) {
if (readFileError) {
console.error(`Failed to read ${pathToInfile}: ${readFileError}`);
} else {
// Parse XML to JS Object
parser.parseString(data, (parseFileError: any, converted: ICCIList) => {
if (parseFileError) {
console.error(`Failed to parse ${pathToInfile}: ${parseFileError}`);
} else {
// Stores our CCI->NIST mapping
const result: Record<string, string> = {};
// For all cci items
converted.cci_list.cci_items[0].cci_item.forEach((cciItem) => {
// Get the latest reference
const newestReference = _.maxBy(
cciItem.references[0].reference,
(item) => _.get(item, '$.version')
);
if (newestReference) {
result[cciItem.$.id] = newestReference.$.index;
} else {
console.error(`No NIST Controls found for ${cciItem.$.id}`);
}
});
fs.writeFileSync(pathToOutfile, JSON.stringify(result));
}
});
}
});
}