-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.mjs
42 lines (34 loc) · 980 Bytes
/
cli.mjs
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
import readline from 'node:readline/promises'
import { stdin } from 'node:process'
import {
gettype,
sum,
print
} from './lib.mjs'
async function main () {
const rl = readline.createInterface({ input: stdin })
let ltype = { __never: true }
const discriminantPaths = []
const literalPaths = []
const args = process.argv.slice(2)
while (args.length) {
const a = args.shift()
if (a === '--discriminant') discriminantPaths.push(args.shift())
if (a === '--literal') literalPaths.push(args.shift())
}
// any discriminants are literals too
literalPaths.push(...discriminantPaths)
for await (const line of rl) {
if (!line) continue // ignore empty lines
const json = JSON.parse(line)
const rtype = gettype(json, '', literalPaths, discriminantPaths)
if (ltype.__never) {
ltype = rtype
} else {
ltype = sum(ltype, rtype)
}
}
if (ltype.__never) return process.exit(1)
console.log(print(ltype))
}
main()