Skip to content

Instantly share code, notes, and snippets.

@othree othree/exec
Created Aug 9, 2017

Embed
What would you like to do?
const spawn = require('child_process').spawn;
// const exec = require('child_process').execSync;
const exec = function (cmd, args) {
return new Promise(function (resolve) {
const execution = spawn(cmd, args);
execution.stdout.on('data', (data) => {
var str = data.toString();
var lines = str.split(/(\r?\n)/g);
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim()) {
console.log(lines[i]);
}
}
});
execution.stderr.on('data', (data) => {
var str = data.toString();
var lines = str.split(/(\r?\n)/g);
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim()) {
console.log('[Error]: ' + lines[i]);
}
}
});
execution.on('close', (code) => {
resolve();
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.