const logger = require('./logger.js'); const util = require('./util.js'); const { spawn } = require('child_process'); function execute(cmd, args) { return new Promise(function (resolve, reject) { var spawned = spawn(cmd, args); spawned.timestamp = new Date(); spawned.stdout.on('data', function (data) { logger.debug(data); }); spawned.stderr.on('data', function (data) { logger.error(data); }); spawned.on('error', function (err) { reject('command \'' + cmd + '\' with args \'' + args + '\' encountered an error after ' + util.timeDiff(spawned.timestamp) + 'ms >>> ' + err); }); spawned.on('exit', function (code) { logger.debug('command \'' + cmd + '\' with args \'' + args + '\' finished with exit code ' + code + ' after ' + util.timeDiff(spawned.timestamp) + 'ms'); resolve(); }); }); } module.exports = { execute }