From ce9dccba5da5a229a97932fd26920de315534d9c Mon Sep 17 00:00:00 2001 From: oleg Date: Mon, 4 May 2015 00:53:11 +0300 Subject: [PATCH] proxy shell input and output from command to executor --- lib/command/base.js | 1 + lib/command/spawn.js | 10 ++++++++++ lib/distributor.js | 6 +++++- lib/executor/base.js | 6 +++++- lib/executor/local.js | 19 +++++++++++++++++-- lib/node.js | 2 ++ projects/project1/config.json | 1 + 7 files changed, 41 insertions(+), 4 deletions(-) diff --git a/lib/command/base.js b/lib/command/base.js index 3ae7ce8..a9431b3 100644 --- a/lib/command/base.js +++ b/lib/command/base.js @@ -5,6 +5,7 @@ var EventEmitter = require('events').EventEmitter, function Command(params) { params = params || {}; + this.emitIn = params.emitIn; this.emitOut = params.emitOut; } diff --git a/lib/command/spawn.js b/lib/command/spawn.js index 2bf1ac7..3225e61 100644 --- a/lib/command/spawn.js +++ b/lib/command/spawn.js @@ -21,20 +21,29 @@ inherits(Command, ParentCommand); Command.prototype.run = function(params, callback) { var self = this, stdout = self.collectOut ? '' : null; + if (!params.cmd) return callback(new Error('`cmd` is not set')); if (!params.args) return callback(new Error('`args` is not set')); callback = utils.once(callback); params.options = params.options || {}; params.options.cwd = params.options.cwd || this.cwd; + var cmd = spawn(params.cmd, params.args, params.options); + + if (self.emitIn) { + self.emit('stdin', params.cmd + ' ' + params.args.join(' ')); + } + cmd.stdout.on('data', function(data) { if (self.emitOut) self.emit('stdout', data); if (self.collectOut) stdout += data; }); + cmd.stderr.on('data', function(data) { callback(new Error('Spawned command outputs to stderr: ' + data)); cmd.kill(); }); + cmd.on('close', function(code) { var err = null; if (code !== 0) err = new Error( @@ -42,5 +51,6 @@ Command.prototype.run = function(params, callback) { ); callback(err, stdout); }); + return cmd; }; diff --git a/lib/distributor.js b/lib/distributor.js index 93814bd..96efa71 100644 --- a/lib/distributor.js +++ b/lib/distributor.js @@ -55,11 +55,15 @@ Distributor.prototype._runNext = function(callback) { self.queue.splice(queueItemIndex, 1); var stepCallback = this.slot(); - node.run(queueItem.project, build.params, function(err) { + var executor = node.run(queueItem.project, build.params, function(err) { build.status = err ? 'error' : 'done'; build.error = err; self._updateBuild(build, stepCallback); }); + + executor.on('data', function(data) { + console.log('>>> executor`s data = ', data); + }); }, callback ); diff --git a/lib/executor/base.js b/lib/executor/base.js index 3de0f6a..51aa340 100644 --- a/lib/executor/base.js +++ b/lib/executor/base.js @@ -2,7 +2,9 @@ var Steppy = require('twostep').Steppy, path = require('path'), - _ = require('underscore'); + _ = require('underscore'), + EventEmitter = require('events').EventEmitter, + inherits = require('util').inherits; function Executor(params) { this.project = params.project; @@ -11,6 +13,8 @@ function Executor(params) { exports.Executor = Executor; +inherits(Executor, EventEmitter); + Executor.prototype._getSources = function(params, callback) { }; diff --git a/lib/executor/local.js b/lib/executor/local.js index fa8f625..b7d721c 100644 --- a/lib/executor/local.js +++ b/lib/executor/local.js @@ -5,7 +5,8 @@ var Steppy = require('twostep').Steppy, ParentExecutor = require('./base').Executor, createScm = require('../scm').createScm, createCommand = require('../command').createCommand, - fs = require('fs'); + fs = require('fs'), + _ = require('underscore'); function Executor(params) { ParentExecutor.call(this, params); @@ -61,7 +62,21 @@ Executor.prototype._runStep = function(params, callback) { } // set command cwd to executor cwd params.cwd = self.cwd; - var command = createCommand(params); + var command = createCommand( + _({ + emitIn: true, + emitOut: true + }).extend(params) + ); + + command.on('stdin', function(data) { + self.emit('data', '> ' + String(data)); + }); + + command.on('stdout', function(data) { + self.emit('data', String(data)); + }); + command.run(params, this.slot()) }, callback diff --git a/lib/node.js b/lib/node.js index 9801aee..1057e8c 100644 --- a/lib/node.js +++ b/lib/node.js @@ -39,4 +39,6 @@ Node.prototype.run = function(project, params, callback) { delete self.executors[project.name]; callback(err); }); + + return this.executors[project.name]; }; diff --git a/projects/project1/config.json b/projects/project1/config.json index d58fa60..d3d9290 100644 --- a/projects/project1/config.json +++ b/projects/project1/config.json @@ -6,6 +6,7 @@ "rev": "default" }, "steps": [ + {"type": "shell", "cmd": "echo 11"}, {"type": "shell", "cmd": "echo 1 > 1.txt"}, {"type": "shell", "cmd": "echo 2 > 2.txt"} ]