-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.js
More file actions
47 lines (42 loc) · 1.23 KB
/
parser.js
File metadata and controls
47 lines (42 loc) · 1.23 KB
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
"use strict";
const fs = require("fs");
const xml2js = require("xml2js");
const SynergyImport = require("./model");
function readFile(src) {
return new Promise((resolve, reject) => {
if (!fs.existsSync(src)) {
reject(`File '${src}' not found`);
} else {
fs.readFile(src, (err, data) => {
if (err) {
reject(err);
} else {
resolve(data);
}
});
}
});
}
function parseXML(xmlString) {
return new Promise((resolve, reject) => {
new xml2js.Parser().parseString(xmlString, function (err, result) {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
}
function createModel(parsedData) {
const p = new SynergyImport();
const attr = parsedData.specification["$"];
p.setSpecification(attr.author, attr.project, attr.version, parsedData.specification.title[0], parsedData.specification.description[0])
.setSuites(parsedData.specification.suites);
return Promise.resolve(p);
}
exports.parse = filePath => {
return readFile(filePath)
.then(parseXML)
.then(createModel);
};