forked from fnogatz/xsd2json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (25 loc) · 790 Bytes
/
Copy pathindex.js
File metadata and controls
35 lines (25 loc) · 790 Bytes
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
module.exports = xsd2json;
var childProcess = require('duplex-child-process');
var concat = require('concat-stream');
var path = require('path');
var PROLOG = process.env.SWIPL || 'swipl';
var XSD2JSON = process.env.XSD2JSON || path.resolve(__dirname, 'lib-pl', 'cli');
function xsd2json(filename, callback) {
var outputStream = childProcess.spawn(PROLOG, ['-q', '-f', XSD2JSON, filename]);
if (arguments.length === 1) {
// no callback given --> return stream
return outputStream;
}
outputStream.pipe(reader(callback));
}
function reader(callback) {
return concat(function(jsonBuff) {
var jsonString = jsonBuff.toString();
try {
var schema = JSON.parse(jsonString);
} catch(err) {
callback(err);
}
callback(null, schema);
});
}