-
Notifications
You must be signed in to change notification settings - Fork 21
/
ndjson-cat
executable file
·38 lines (33 loc) · 1.11 KB
/
ndjson-cat
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
#!/usr/bin/env node
var fs = require("fs"),
commander = require("commander"),
output = require("./output");
commander
.version(require("./package.json").version)
.usage("[options] [files…]")
.description("Concatenate JSON values to form a newline-delimited JSON stream.")
.parse(process.argv);
if (commander.args.length < 1) {
commander.args[0] = "-";
}
var i = -1, n = commander.args.length, task = Promise.resolve();
while ( i < n) task = task.then(cat(commander.args[i]));
task.catch(function(error) { console.error(error); });
function cat(input) {
return new Promise(function(resolve, reject) {
var data = [], stream = input === "-" ? process.stdin : fs.createReadStream(input);
stream
.on("data", function(d) { data.push(d); })
.on("error", reject)
.on("end", function() {
try {
var d = JSON.parse(Buffer.concat(data));
} catch (e) {
console.error((input === "-" ? "stdin" : input) ": SyntaxError: " e.message);
process.exit(1);
};
output(d);
resolve();
});
});
}