Created
November 30, 2017 18:01
-
-
Save dkarmalita/7f969af98f5f40d2cd9e40865928fdc0 to your computer and use it in GitHub Desktop.
async shellExec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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