-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
66 lines (51 loc) · 1.82 KB
/
app.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
var prompt = require("prompt");
var parseString = require("xml2js").parseString;
const path = require("path");
const fs = require("fs");
start();
function start() {
prompt.start();
prompt.get(["xmlUrl", "valueName"], function (err, result) {
var xmlUrl = result.xmlUrl.replace(/"/g, "");
if (xmlUrl == "0") process.exit(-1);
var savePath = xmlUrl.split("\\");
const valueName = result.valueName ? result.valueName : false;
var fileName = savePath[savePath.length - 1].replace(".xml", ".json");
// removing last element
savePath.splice(savePath.length - 1, 1);
savePath = savePath.join("\\");
var xmlData = fs.readFileSync(xmlUrl, "utf-8");
// covert xml to json
parseString(xmlData, function (err, result) {
// extract list of data
var data = result.codeList.code;
data.forEach((d) => {
// remove description
delete d.description;
d["codeListVersion"] = typeof d.name == "object" ? d.name[0] : d.name;
delete d.name;
d["value"] = typeof d.value == "object" ? d.value[0] : d.value;
if (valueName) {
d[valueName] = typeof d.value == "object" ? d.value[0] : d.value;
delete d.value;
}
});
// save it as json
fs.writeFileSync(path.join(savePath, fileName), JSON.stringify(data));
console.info(
"\nData extraction completed",
path.join(savePath, fileName),
"\n"
);
// List converted XML's
const completedModelsFileName = "completed-models.json";
const content = JSON.parse(
fs.readFileSync(completedModelsFileName, "utf-8")
);
if (!content.some((c) => c == fileName)) content.push(fileName);
fs.writeFileSync(completedModelsFileName, JSON.stringify(content));
// restart
start();
});
});
}