diff --git a/lib/command/base.js b/lib/command/base.js index 6b7b3e2..449940b 100644 --- a/lib/command/base.js +++ b/lib/command/base.js @@ -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; -}; diff --git a/lib/command/spawn.js b/lib/command/spawn.js index 40f614e..6ff463b 100644 --- a/lib/command/spawn.js +++ b/lib/command/spawn.js @@ -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` */ diff --git a/lib/scm/base.js b/lib/scm/base.js index 222182a..5190d58 100644 --- a/lib/scm/base.js +++ b/lib/scm/base.js @@ -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` diff --git a/lib/scm/index.js b/lib/scm/index.js index 89d0ddc..7b74c75 100644 --- a/lib/scm/index.js +++ b/lib/scm/index.js @@ -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); };