diff options
Diffstat (limited to 'app/lib/pipe.js')
-rw-r--r-- | app/lib/pipe.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/app/lib/pipe.js b/app/lib/pipe.js new file mode 100644 index 0000000..e329b2b --- /dev/null +++ b/app/lib/pipe.js @@ -0,0 +1,27 @@ +"use strict"; + +const { spawn } = require("child_process"); + +module.exports = function pipe({ command, flags, stdin="", buffer } = {}) { + return new Promise((resolve, reject) => { + const child = spawn(command, flags); + let stdout = (buffer?[]:""); + let stderr = ""; + + child.stderr.on("data", d => stderr += d); + child.stdout.on("data", d => (buffer? + stdout.push(d):stdout+=d)); + + child.on("close", code => { + const res = { code, stderr, stdout: + (buffer?Buffer.concat(stdout):stdout) }; + + if (code) reject(res); + else resolve(res); + }); + + //child.stdin.setEncoding("utf-8"); + child.stdin.write(stdin); + child.stdin.end(); + }); +} |