-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·61 lines (48 loc) · 1.43 KB
/
index.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
#!/usr/bin/env node
const argv = require('minimist')(process.argv.slice(2))
const style = require('ansi-styles')
if (argv.h || argv.help || !argv._.length) {
process.stdout.write(
`${style.bold.open}NAME${style.bold.close}
node-e - Helper for quick CLI scripting using node
${style.bold.open}SYNOPSIS${style.bold.close}
node-e <script>
${style.bold.open}DESCRIPTION${style.bold.close}
The purpose of node-e is to help write quick CLI commands.
And tries to avoid some of the boilerplate code that somes
with non-blocking IO node.
${style.bold.open}Global vars${style.bold.close}
$in
string - stdin as a string
${style.bold.open}Examples${style.bold.close}
node-e 1+1 => 2
Evaluates javascript expressions and passes the result to stdout
echo Hello | node-e '$in + " world!"' => "Hello world!"
Content from stdin is assigned to $in variable
cat data.json | node-e '$in.foo.bar'
Parses stdin as JSON by default
`)
process.exit(0)
}
const removeTrailingNewline = (x) => x.slice(0, -1)
const evaluate = () => {
$in = removeTrailingNewline($in)
try {
$in = JSON.parse($in)
} catch (e) {}
// console.error('foo', $in)
console.log(JSON.stringify(eval(argv._[0])))
}
process.stdin.setEncoding('utf8');
let $in = ''
process.stdin.on('readable', () => {
const chunk = process.stdin.read()
if (chunk === null) {
evaluate()
process.exit(0)
}
$in += chunk
})
// process.stdin.on('end', () => {
// evaluate()
// });