-
Notifications
You must be signed in to change notification settings - Fork 21
/
ndjson-map
executable file
·46 lines (40 loc) · 1.16 KB
/
ndjson-map
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
#!/usr/bin/env node
var readline = require("readline"),
vm = require("vm"),
commander = require("commander"),
requires = require("./requires"),
expression = require("./expression"),
output = require("./output");
commander
.version(require("./package.json").version)
.usage("[options] [expression]")
.description("Transform values in a newline-delimited JSON stream.")
.option("-r, --require <name=module>", "require a module", requires, {d: undefined, i: -1})
.parse(process.argv);
if (commander.args.length > 1) {
commander.outputHelp();
process.exit(1);
}
if (commander.args.length < 1) {
commander.args[0] = "d";
}
var i = -1,
sandbox = commander.require,
map = expression(commander.args[0]),
context = new vm.createContext(sandbox);
readline.createInterface({
input: process.stdin,
output: null
}).on("line", function(line) {
sandbox.i = ++i;
try {
sandbox.d = JSON.parse(line);
} catch (error) {
console.error("stdin:" + (i + 1));
console.error(line);
console.error("^");
console.error("SyntaxError: " + error.message);
process.exit(1);
}
output(map.runInContext(context));
});