forked from fountainhead-cash/bigjq
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
32 lines (31 loc) · 830 Bytes
/
index.js
File metadata and controls
32 lines (31 loc) · 830 Bytes
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
const spawn = require('child_process').spawn;
var getPath = function() {
return process.cwd() + '/node_modules/node-jq/bin/jq'
}
var run = function(filter, data, log_result=true) {
const child = spawn(getPath(), [filter]);
return new Promise(function(resolve, reject) {
let chunks = [];
child.stdout.on('data', (chunk) => {
chunks.push(chunk)
});
child.stdout.on('end', () => {
let str = chunks.join("");
if(log_result)
console.log("STR = ", str)
try {
let parsed = JSON.parse(str);
resolve(parsed)
} catch (e) {
reject(e)
}
})
child.stdout.on('error', (err) => {
reject(err)
})
child.stdin.setEncoding('utf-8');
child.stdin.write(JSON.stringify(data));
child.stdin.end()
})
}
module.exports = { run: run }