pbc/libs/commands.js

27 lines
982 B
JavaScript
Raw Normal View History

const logger = require('./logger.js');
2022-03-10 00:32:38 +01:00
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);
2022-03-10 00:32:38 +01:00
spawned.timestamp = new Date();
spawned.stdout.on('data', function (data) {
logger.debug(data);
});
spawned.stderr.on('data', function (data) {
logger.error(data);
});
spawned.on('close', function (code) {
2022-03-10 00:32:38 +01:00
logger.debug('command \'' + cmd + '\' with args \'' + args + '\' finished with exit code ' + code + ' after ' + util.timeDiff(spawned.timestamp) + 'ms');
resolve();
});
spawned.on('error', function (err) {
2022-03-10 00:32:38 +01:00
return reject('command \'' + cmd + '\' with args \'' + args + '\' encountered an error after ' + util.timeDiff(spawned.timestamp) + 'ms >>> ' + err);
});
});
}
module.exports = {
execute
}