-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·66 lines (56 loc) · 1.46 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
62
63
64
65
66
#!/usr/bin/env node
// writen in es5, for better env compatability
// todo: maybe JacksonTian/bufferhelper?
// todo: minimist for friendly cli handling?
// vm: https://nodejs.org/api/vm.html
'use strict'
var through2 = require('through2')
var _ = require('lodash')
var cp = require('child_process')
var vm = require('vm')
var stdin = process.stdin
var stdout = process.stdout
var args = process.argv.slice(2)
var forEach = false
var execCmd = false
var textIn = false
var textOut = false
var exprs = []
args.forEach(function (arg) {
if (arg === '-e') { // for each
forEach = true
} else if (arg === '-c') { // exec cmd
execCmd = true
} else if (arg === '-ti') { // text input
textIn = true
} else if (arg === '-to') { // text output
textOut = true
} else if (arg === '-t') { // text i/o
textIn = textOut = true
} else {
exprs.push(arg)
}
})
var stream = stdin
exprs.forEach(function (expr) {
if (execCmd) {
expr = 'exec(`' + expr + '`)'
}
if (forEach) {
expr = 'x.forEach(function (x, i) {' + expr + '}), x'
}
stream = stream.pipe(through2(function (chunk, enc, callback) {
var injson = chunk.toString()
var inobj = textIn ? injson : JSON.parse(injson)
var sandbox = {
exec: cp.execSync,
_: _,
x: inobj
}
vm.createContext(sandbox)
var outobj = vm.runInContext(expr, sandbox)
var outjson = textOut ? outobj : JSON.stringify(outobj)
this.push(outjson)
}))
})
stream.pipe(stdout)