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;
function Command(params) {
params = params || {};
this.emitIn = params.emitIn;
this.emitOut = params.emitOut;
this.emitErr = params.emitErr;
this.attachStderr = params.attachStderr;
this.setParams(params);
}
exports.Command = Command;
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');
function Command(params) {
params = params || {};
ParentCommand.call(this, params);
this.cwd = params.cwd;
ParentCommand.call(this, params || {});
}
exports.Command = Command;
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`
*/

View File

@ -1,22 +1,46 @@
'use strict';
var ParentCommand = require('../command/spawn').Command,
inherits = require('util').inherits;
var EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits,
_ = require('underscore');
function Scm(params) {
ParentCommand.call(this, params);
this.repository = params.repository;
if (!this.repository && !this.cwd) throw new Error(
'`repository` or `cwd` must be set'
);
this.collectOut = true;
this.emitIn = true;
this.attachStderr = true;
var self = this;
EventEmitter.call(self);
self.repository = params.repository;
self.cwd = params.cwd;
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;
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`

View File

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