-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·78 lines (67 loc) · 1.91 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const pkg = require('./package.json')
const argv = mri(process.argv.slice(2), {
boolean: [
'help', 'h',
'version', 'v',
// todo: tick fit, tick count, timeseries?
// see https://c3js.org/reference.html
]
})
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
cat data.ndjson | ndjson-to-chart >chart.html
Options:
--x Set the X axis of all fields to another field.
--x:{field} Set the X axis of {field} to another field.
--type Show all fields as a specific type of chart.
see https://c3js.org/reference.html#data-type
--type:{field} Show {field} as a type of chart.
see https://c3js.org/reference.html#data-type
Examples:
ndjson-to-chart --x t <node_modules/ndjson-chart-cli/example.ndjson
ndjson-to-chart --x:temperature time1 --type:temperature area-spline <measurements.ndjson
\n`)
process.exit(0)
}
if (argv.version || argv.v) {
process.stdout.write(`ndjson-to-chart v${pkg.version}\n`)
process.exit(0)
}
const showError = (err) => {
if (!err) return;
if (process.env.NODE_ENV === 'dev') console.error(err)
else console.error(err && err.message || (err + ''))
process.exit(1)
}
const {readFileSync} = require('fs')
const {join} = require('path')
const {Transform} = require('stream')
const pump = require('pump')
const {parse: ndjsonParser} = require('ndjson')
const {c3ConfigGenerator} = require('.')
const head = readFileSync(join(__dirname, 'lib', 'head.html'))
const tail = readFileSync(join(__dirname, 'lib', 'tail.html'))
const {ingest, generate} = c3ConfigGenerator(argv)
const transform = (row, _, cb) => {
ingest(row)
cb()
}
const flush = (cb) => {
const html = Buffer.concat([
head,
Buffer.from(generate(), 'utf-8'),
tail
])
cb(null, html)
}
pump(
process.stdin,
ndjsonParser(),
new Transform({objectMode: true, transform, flush}),
process.stdout,
showError
)