-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
67 lines (58 loc) · 1.66 KB
/
index.ts
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
#!/usr/bin/env node
import * as meow from 'meow'
import * as stdin from 'get-stdin'
import {invoker, ramdaInvoker, vm} from './vm'
import {readFile, print} from './io'
import * as R from 'ramda'
const program = meow(`
HoseJS
Transform JSON with javascript.
Usage
$ cat some.json | j '[_].map(x => x.some_property)[0]'
$ cat some.json | j '_.map(x => new Date(x.timestamp).toISOString())'
$ cat some.json | j --file preload.js '.map(x => x.timestamp)'
Usage #ramda functions
$ cat some.json | j -r "pipe(pluck('timestamp'), head, Date)"
Advanced Usage
visit https://github.com/deptno/hosejs#usage
Alias
j, js
Options
--file, -f use javascript file first
--tab, -t JSON tab space (default: 2)
--ramda, -r automatic invoke with 'your_code(_)'
`, {
flags: {
file: {type: 'string', alias: 'f'},
tab : {type: 'string', alias: 't', default: '2'},
ramda : {type: 'boolean', alias: 'r', default: false},
}
})
async function main() {
const {input, flags} = program
if (flags.file) {
const buffer = await readFile(flags.file)
input.unshift(buffer.toString())
}
try {
const tryCatch = (_: any) => {
try {
return JSON.parse(_)
} catch(e) {
return _
}
}
const coder = R.ifElse(Boolean, R.always(ramdaInvoker), R.always(invoker))(flags.ramda)
const source = await stdin()
const sandbox: any = R.compose(
R.assoc('_', (R as any).__, {...R, R} as any),
tryCatch
)(source)
vm(sandbox, input.map(coder).join(';'))
print(sandbox._, parseInt(flags.tab))
} catch (e) {
console.error('🚫', e)
process.exit(1)
}
}
main()