Skip to content

Instantly share code, notes, and snippets.

@alexsasharegan
Created April 24, 2018 22:29
Show Gist options
  • Save alexsasharegan/ae67d09b1de5061b36b1e1ebb7cdf5c7 to your computer and use it in GitHub Desktop.
Save alexsasharegan/ae67d09b1de5061b36b1e1ebb7cdf5c7 to your computer and use it in GitHub Desktop.
const { spawn } = require("child_process");
function stream_cmd(cmd, args, { stdout, stderr, close, error }, options = {}) {
const proc = spawn(cmd, args, options);
if (stdout) {
proc.stdout.on("data", stdout);
}
if (stderr) {
proc.stderr.on("data", stderr);
}
if (close) {
proc.on("close", close);
}
if (error) {
proc.on("error", error);
}
return proc;
}
stream_cmd("git", ["status"], {
close: code => console.log(`Exited with code:`, code),
error: err => console.error(`command failed:`, err),
stderr: buf => console.error(`stderr:`, String(buf)),
stdout: buf => console.log(`stdout:`, String(buf)),
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment