Skip to content

Instantly share code, notes, and snippets.

@dkarmalita
Created November 30, 2017 18:01
Show Gist options
  • Save dkarmalita/7f969af98f5f40d2cd9e40865928fdc0 to your computer and use it in GitHub Desktop.
Save dkarmalita/7f969af98f5f40d2cd9e40865928fdc0 to your computer and use it in GitHub Desktop.
async shellExec
/* eslint no-console: 0 */
'use strict'
const shellExec = (
cmd, // shell command to execute
suppressErr=false, // drop errors if any
suppressOutput=false // execute silent
) => new Promise((resolve,reject)=>{
const { spawn } = require('child_process');
const worker = spawn(cmd, { shell: true })
if(!suppressOutput){
worker.stdout.on('data', (data) => {
console.log(`${data}`);
});
if(!suppressErr){
worker.stderr.on('data', (data) => {
console.log(`${data}`);
});
}
}
worker.on('close', (code) => {
if(code===0 || suppressErr){
resolve()
}else{
reject()
}
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment