96 lines
2.6 KiB
JavaScript
96 lines
2.6 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
exports = module.exports = {
|
||
|
sudo: sudo,
|
||
|
exec: exec,
|
||
|
system: system
|
||
|
};
|
||
|
|
||
|
var assert = require('assert'),
|
||
|
child_process = require('child_process'),
|
||
|
debug = require('debug')('e2e:shell.js'),
|
||
|
once = require('once'),
|
||
|
util = require('util');
|
||
|
|
||
|
var SUDO = '/usr/bin/sudo';
|
||
|
|
||
|
function exec(tag, file, args, options, callback) {
|
||
|
assert.strictEqual(typeof tag, 'string');
|
||
|
assert.strictEqual(typeof file, 'string');
|
||
|
assert(util.isArray(args));
|
||
|
|
||
|
if (typeof options === 'function') {
|
||
|
callback = options;
|
||
|
options = { };
|
||
|
} else {
|
||
|
assert.strictEqual(typeof options, 'object');
|
||
|
assert.strictEqual(typeof callback, 'function');
|
||
|
}
|
||
|
|
||
|
callback = once(callback); // exit may or may not be called after an 'error'
|
||
|
|
||
|
debug(tag + ' execFile: %s %s', file, args.join(' '));
|
||
|
|
||
|
var cp = child_process.spawn(file, args, options);
|
||
|
cp.stdout.on('data', function (data) {
|
||
|
debug(tag + ' (stdout): %s', data.toString('utf8'));
|
||
|
});
|
||
|
|
||
|
cp.stderr.on('data', function (data) {
|
||
|
debug(tag + ' (stderr): %s', data.toString('utf8'));
|
||
|
});
|
||
|
|
||
|
cp.on('exit', function (code, signal) {
|
||
|
if (code || signal) debug(tag + ' code: %s, signal: %s', code, signal);
|
||
|
|
||
|
callback(code === 0 ? null : new Error(util.format('Exited with error %s signal %s', code, signal)));
|
||
|
});
|
||
|
|
||
|
cp.on('error', function (error) {
|
||
|
debug(tag + ' code: %s, signal: %s', error.code, error.signal);
|
||
|
callback(error);
|
||
|
});
|
||
|
|
||
|
return cp;
|
||
|
}
|
||
|
|
||
|
function system(tag, cmd, options, callback) {
|
||
|
assert.strictEqual(typeof tag, 'string');
|
||
|
assert.strictEqual(typeof cmd, 'string');
|
||
|
|
||
|
if (typeof options === 'function') {
|
||
|
callback = options;
|
||
|
options = { };
|
||
|
} else {
|
||
|
assert.strictEqual(typeof options, 'object');
|
||
|
assert.strictEqual(typeof callback, 'function');
|
||
|
}
|
||
|
|
||
|
debug(tag + ' system: %s %j', cmd, options);
|
||
|
|
||
|
var cp = child_process.exec(cmd, options, function (error, stdout, stderr) {
|
||
|
if (error) debug(tag + ' (error) ' + error);
|
||
|
|
||
|
callback(error, stdout, stderr);
|
||
|
});
|
||
|
|
||
|
cp.stdout.on('data', function (data) {
|
||
|
debug(tag + ' (stdout): %s', data.toString('utf8'));
|
||
|
});
|
||
|
|
||
|
cp.stderr.on('data', function (data) {
|
||
|
debug(tag + ' (stderr): %s', data.toString('utf8'));
|
||
|
});
|
||
|
}
|
||
|
|
||
|
function sudo(tag, args, callback) {
|
||
|
assert.strictEqual(typeof tag, 'string');
|
||
|
assert(util.isArray(args));
|
||
|
assert.strictEqual(typeof callback, 'function');
|
||
|
|
||
|
// -S makes sudo read stdin for password
|
||
|
var cp = exec(tag, SUDO, [ '-S' ].concat(args), callback);
|
||
|
cp.stdin.end();
|
||
|
}
|
||
|
|