-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.js
executable file
·55 lines (46 loc) · 1.27 KB
/
cli.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
#!/usr/bin/env node
// package deps
const program = require('commander'),
readline = require('readline'),
debug = require('debug')('b3');
// library deps
const parser = require('./lib/parser.js'),
errors = require('./lib/errors.js'),
packageDetails = require('./package.json');
program
.version(packageDetails.version)
.option('-s, --stop-on-fail', 'Stop on parser failure', false)
.parse(process.argv);
parser.initialize({
trace: process.env.TRACE == 'true'
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
rl.on('line', parseLine);
function parseLine(line) {
try {
debug('Received line: ' + line);
let parsed = parser.parseLine(line, {debug:true});
console.log(outputLine(parsed));
} catch(e) {
switch(true) {
case e instanceof errors.UnfinishedSyscallException:
debug('Encountered partial syscall, skipping: ' + line);
// suppress
return;
}
debug('[PARSE ERROR] ' + e);
if(program.stopOnFail) {
console.error('[PARSE ERROR] ' + e);
debug('Exiting due to stopOnFail argument');
process.exit(1);
}
}
}
// TODO: some kind of buffering solution to boost performance?
function outputLine(line) {
return JSON.stringify(line);
}