scm uses command (but not command itself)

This commit is contained in:
oleg 2016-02-27 23:29:18 +03:00
parent 5a6a440d11
commit a7840fc219
4 changed files with 51 additions and 29 deletions

View File

@ -4,23 +4,9 @@ var EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits; inherits = require('util').inherits;
function Command(params) { function Command(params) {
params = params || {}; this.setParams(params);
this.emitIn = params.emitIn;
this.emitOut = params.emitOut;
this.emitErr = params.emitErr;
this.attachStderr = params.attachStderr;
} }
exports.Command = Command; exports.Command = Command;
inherits(Command, EventEmitter); inherits(Command, EventEmitter);
Command.prototype.enableEmitter = function() {
this.emitOut = true;
return this;
};
Command.prototype.disableEmitter = function() {
this.emitOut = false;
return this;
};

View File

@ -6,15 +6,22 @@ var spawn = require('child_process').spawn,
_ = require('underscore'); _ = require('underscore');
function Command(params) { function Command(params) {
params = params || {}; ParentCommand.call(this, params || {});
ParentCommand.call(this, params);
this.cwd = params.cwd;
} }
exports.Command = Command; exports.Command = Command;
inherits(Command, ParentCommand); inherits(Command, ParentCommand);
Command.prototype.setParams = function(params) {
if (params.cwd) this.cwd = params.cwd;
if (params.emitIn) this.emitIn = params.emitIn;
if (params.emitOut) this.emitOut = params.emitOut;
if (params.emitErr) this.emitErr = params.emitErr;
if (params.attachStderr) this.attachStderr = params.attachStderr;
if (params.collectOut) this.collectOut = params.collectOut;
};
/** /**
* Executes `params.cmd` with `params.args` and `params.options` * Executes `params.cmd` with `params.args` and `params.options`
*/ */

View File

@ -1,22 +1,46 @@
'use strict'; 'use strict';
var ParentCommand = require('../command/spawn').Command, var EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits; inherits = require('util').inherits,
_ = require('underscore');
function Scm(params) { function Scm(params) {
ParentCommand.call(this, params); var self = this;
this.repository = params.repository;
if (!this.repository && !this.cwd) throw new Error( EventEmitter.call(self);
'`repository` or `cwd` must be set'
); self.repository = params.repository;
this.collectOut = true; self.cwd = params.cwd;
this.emitIn = true;
this.attachStderr = true; if (!self.repository && !self.cwd) {
throw new Error('`repository` or `cwd` must be set');
}
self.command = params.command;
self.command.setParams({
collectOut: true,
emitIn: true,
attachStderr: true
});
self.command.on('stdin', function(data) {
self.emit('stdin', data);
});
} }
exports.Scm = Scm; exports.Scm = Scm;
inherits(Scm, ParentCommand); inherits(Scm, EventEmitter);
Scm.prototype.run = function(params, callback) {
if (this.cwd) {
params.options = params.options || {};
params.options.cwd = this.cwd;
}
this.command.run(params, callback);
};
/** /**
* Clone repository to the `dst` update to `rev` and set `this.cwd` to `dst` * Clone repository to the `dst` update to `rev` and set `this.cwd` to `dst`

View File

@ -1,6 +1,11 @@
'use strict'; 'use strict';
var SpawnCommand = require('../command/spawn').Command;
exports.createScm = function(params) { exports.createScm = function(params) {
var Constructor = require('./' + params.type).Scm; var Constructor = require('./' + params.type).Scm;
params.command = params.command || new SpawnCommand();
return new Constructor(params); return new Constructor(params);
}; };