From 1387d80e103699c1f4a52983db4f808b812ed547 Mon Sep 17 00:00:00 2001 From: oleg Date: Wed, 3 Dec 2014 00:21:07 +0300 Subject: [PATCH] add shell command --- lib/command/index.js | 10 ++++++++++ lib/command/shell.js | 25 +++++++++++++++++++++++++ lib/utils.js | 15 +++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 lib/command/index.js create mode 100644 lib/command/shell.js create mode 100644 lib/utils.js diff --git a/lib/command/index.js b/lib/command/index.js new file mode 100644 index 0000000..d214875 --- /dev/null +++ b/lib/command/index.js @@ -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); +}; diff --git a/lib/command/shell.js b/lib/command/shell.js new file mode 100644 index 0000000..eeabd4b --- /dev/null +++ b/lib/command/shell.js @@ -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); +}; diff --git a/lib/utils.js b/lib/utils.js new file mode 100644 index 0000000..4c3fd27 --- /dev/null +++ b/lib/utils.js @@ -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;