add shell command

This commit is contained in:
oleg 2014-12-03 00:21:07 +03:00
parent 905a63fd20
commit 1387d80e10
3 changed files with 50 additions and 0 deletions

10
lib/command/index.js Normal file
View File

@ -0,0 +1,10 @@
'use strict';
var typesHash = {
'shell': require('./shell').ShellCommand
};
exports.createCommand = function(params) {
var Constructor = typesHash[params.type];
return new Constructor(params);
};

25
lib/command/shell.js Normal file
View File

@ -0,0 +1,25 @@
'use strict';
var ParentCommand = require('./spawn').SpawnCommand,
inherits = require('util').inherits;
function Command(params) {
ParentCommand.call(this, params);
}
exports.ShellCommand = Command;
inherits(Command, ParentCommand);
/**
* Executes `params.cmd` (e.g. 'echo 1 && echo 2') in `params.shell`
* (e.g. '/bin/sh') with `params.options`
*/
Command.prototype.run = function(params, callback) {
if (!params.shell) return callback(new Error('`shell` is not set'));
if (!params.cmd) return callback(new Error('`cmd` is not set'));
ParentCommand.prototype.exec.call(this, {
cmd: params.shell, args: ['-c', params.cmd], options: params.options
}, callback);
};

15
lib/utils.js Normal file
View File

@ -0,0 +1,15 @@
'use strict';
['Function', 'String', 'Number', 'Date', 'RegExp'].forEach(function(name) {
exports['is' + name] = function(obj) {
return toString.call(obj) == '[object ' + name + ']';
};
});
exports.isObject = function(obj) {
return obj === Object(obj);
};
exports.noop = function() {};
exports.slice = Array.prototype.slice;