forked from commaai/log_reader_js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog-to-json.js
executable file
·66 lines (57 loc) · 1.73 KB
/
log-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
#!/usr/bin/env node
const fs = require('fs');
const Reader = require('./');
const EventWrapper = require('./src/event');
const cli = require('commander');
const JSONStream = require('JSONStream');
const package = require('./package');
cli
.version(package.version)
.usage('[source]')
.description('Take an rlog file and convert it to json. If no output file is specified, prints to stdout')
.option('-a, --array', 'Output json as a massive array instead of newline deliminated json objects')
.option('-o, --output <filename>', 'Output file to write the JSON values to')
.option('-m, --message <index>', 'Print a specific message')
.option('-b, --binary', 'Print the raw binary as opposed to the pretty JSON')
.parse(process.argv);
let source = cli.args[0];
let sourceStream = process.stdin;
let destination = cli.output;
let destinationStream = process.stdout;
if (cli.message) {
cli.message = Number(cli.message);
}
if (destination) {
if (fs.existsSync(destination)) {
fs.truncateSync(destination);
}
destinationStream = fs.createWriteStream(destination);
}
if (source) {
sourceStream = fs.createReadStream(source);
}
var jsonStream = null;
if (cli.array) {
// default is '[', '\n,\n', ']'
// massive array
jsonStream = JSONStream.stringify();
} else {
// false is shorthand for '', '\n', ''
// that means no start/end symbol and newline deliminated
jsonStream = JSONStream.stringify(false);
}
jsonStream.pipe(destinationStream);
var reader = Reader(sourceStream, {
binary: !!cli.binary
});
var i = 0;
reader(function (jsonData) {
++i;
if (!cli.message) {
jsonStream.write(jsonData);
} else if (cli.message === i) {
jsonStream.write(jsonData);
jsonStream.end(null);
process.exit(0);
}
});