-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathindex.js
86 lines (73 loc) · 1.78 KB
/
index.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module.exports = xsd2json
const childProcess = require('child_process')
const concat = require('concat-stream')
const path = require('path')
const CLI = path.resolve(__dirname, 'lib-pl', 'cli.exe')
const CLIPL = path.resolve(__dirname, 'lib-pl', 'cli.pl')
const SWI = 'swipl'
const reservedKeys = [
'noExe',
'swi'
]
function xsd2json (filename, options, callback) {
if (arguments.length === 1) {
options = {}
callback = null
} else if (arguments.length === 2) {
if (typeof options === 'function') {
callback = options
options = {}
} else {
callback = null
}
}
let spawnArgs = []
for (const key in options) {
if (reservedKeys.indexOf(key) >= 0) {
continue
}
spawnArgs.push('--' + key + '=' + options[key])
}
spawnArgs.push(filename)
let outputStream
if (options.noExe) {
spawnArgs = [
'-g',
'main',
CLIPL,
'--'
].concat(spawnArgs)
outputStream = childProcess.spawn(options.swi || SWI, spawnArgs)
} else {
outputStream = childProcess.spawn(CLI, spawnArgs)
}
if (typeof callback !== 'function') {
// no callback given --> return stream
return outputStream
}
outputStream.stderr.on('data', function (err) {
if (options.trace) {
const lines = err.toString().split(/\n/)
lines.forEach(function (line) {
if (/^CHR:\s+\([0-9]+\)\s+Apply:.*$/.test(line)) {
console.log(line)
}
})
} else {
callback(err)
}
})
outputStream.stdout.pipe(reader(callback))
}
function reader (callback) {
return concat(function (jsonBuff) {
const jsonString = jsonBuff.toString()
let schema
try {
schema = JSON.parse(jsonString)
} catch (err) {
return callback(err)
}
callback(null, schema)
})
}