/
xml-to-json.js
71 lines (71 loc) · 2.67 KB
/
xml-to-json.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.XmlToJson = exports.XmlToJsonOptions = void 0;
var fhir_1 = require("fhir/fhir");
var fs = require("fs");
var path_1 = require("path");
var Prompt = require("prompt-sync");
var XmlToJsonOptions = (function () {
function XmlToJsonOptions() {
this.overwrite = false;
}
return XmlToJsonOptions;
}());
exports.XmlToJsonOptions = XmlToJsonOptions;
var XmlToJson = exports.XmlToJson = (function () {
function XmlToJson(options) {
this.fhir = new fhir_1.Fhir();
this.prompt = Prompt({ sigint: true });
this.options = options;
}
XmlToJson.args = function (args) {
return args
.option('path', {
type: 'string',
description: 'The directories or files to convert to JSON',
array: true
})
.option('overwrite', {
type: 'boolean',
description: 'Always overwrite destination, and do not prompt if the output path for each JSON file already exists',
default: false
});
};
XmlToJson.handler = function (args) {
new XmlToJson(args).execute();
};
XmlToJson.prototype.convert = function (filePath) {
if (!filePath || !filePath.toLowerCase().endsWith('.xml')) {
return;
}
var xml = fs.readFileSync(filePath).toString();
var json = this.fhir.xmlToJson(xml);
var newFilePath = filePath.substring(0, filePath.lastIndexOf('.') + 1) + 'json';
if (!fs.existsSync(newFilePath) || this.options.overwrite || this.prompt("Overwrite ".concat(newFilePath, "? (y|N)")).trim().toLowerCase() === 'y') {
fs.writeFileSync(newFilePath, json);
console.log("Saved ".concat(newFilePath));
}
};
XmlToJson.prototype.execute = function () {
var _this = this;
if (!this.options || !this.options.path || this.options.path.length <= 0) {
return;
}
this.options.path.forEach(function (path) {
if (!fs.existsSync(path)) {
return;
}
if (fs.lstatSync(path).isDirectory()) {
fs.readdirSync(path)
.forEach(function (next) { return _this.convert(path + path_1.sep + next); });
}
else {
_this.convert(path);
}
});
};
XmlToJson.command = 'xml-to-json';
XmlToJson.description = 'Converts a XML file (or all XML files in a directory) to JSON. Stores the JSON file as the same file name as the XML file, but with a JSON extension, instead.';
return XmlToJson;
}());
//# sourceMappingURL=xml-to-json.js.map