@brimdata/zealot
Version:
The Javascript Client for Zed Lakes
46 lines (45 loc) • 1.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "zq", {
enumerable: true,
get: ()=>zq
});
const _childProcess = require("child_process");
const _paths = require("./paths");
function execute(bin, opts, input) {
return new Promise((resolve, reject)=>{
const p = (0, _childProcess.spawn)(bin, opts).on("error", (e)=>reject(e)).on("close", ()=>resolve(out));
let out = "";
p.stdout.on("data", (data)=>out += data);
p.stderr.on("data", (data)=>out += data);
if (input) {
p.stdin.write(input);
p.stdin.end();
}
});
}
function parseNDJSON(input) {
return input.trim().split("\n").map((s)=>{
try {
return JSON.parse(s);
} catch (_) {
throw new Error(s);
}
});
}
async function zq(opts) {
const bin = opts.bin || (0, _paths.getPath)("zq");
const args = [];
if (opts.format) args.push("-f", opts.format);
if (opts.query) args.push(opts.query);
if (opts.file) args.push(opts.file);
else if (opts.input) args.push("-");
const result = await execute(bin, args, opts.input);
if (opts.format === "zjson") {
return parseNDJSON(result);
} else {
return result;
}
}