nci/lib/executor/base.js

87 lines
1.9 KiB
JavaScript
Raw Normal View History

'use strict';
var Steppy = require('twostep').Steppy,
path = require('path'),
_ = require('underscore'),
EventEmitter = require('events').EventEmitter,
2015-05-07 19:03:57 +00:00
inherits = require('util').inherits,
utils = require('../utils');
function Executor(params) {
this.project = params.project;
this.cwd = path.join(this.project.dir, 'workspace');
}
exports.Executor = Executor;
inherits(Executor, EventEmitter);
2015-05-20 20:49:53 +00:00
Executor.prototype.throttledEmit = _(function() {
this.emit.apply(this, arguments);
}).throttle(1500);
Executor.prototype._getSources = function(params, callback) {
};
Executor.prototype._runStep = function(params, callback) {
};
Executor.prototype.run = function(params, callback) {
var self = this,
project = _({}).extend(self.project, params);
Steppy(
function() {
2015-05-20 20:49:53 +00:00
self.throttledEmit('currentStep', 'get sources');
self._getSources(project.scm, this.slot());
},
2015-05-09 16:59:27 +00:00
function(err, scmData) {
self.emit('scmData', scmData);
2015-05-05 23:11:28 +00:00
var funcs = project.steps.map(function(step, index) {
return function() {
2015-05-20 20:49:53 +00:00
self.throttledEmit('currentStep', step.name);
self._runStep(step, this.slot());
};
});
funcs.push(this.slot());
Steppy.apply(this, funcs);
},
callback
);
2015-06-23 19:18:13 +00:00
};
// Returns target rev and filtered changes according to `catchRev`
Executor.prototype._getTarget = function(rev, changes) {
var result = {rev: rev, changes: changes},
catchRev = this.project.catchRev;
if (catchRev) {
// reverse before search
changes = changes.reverse();
var index;
var comment = catchRev.comment;
if (comment) {
index = _(changes).findIndex(function(change) {
if (_(comment).isRegExp()) {
return comment.test(change.comment);
} else {
return comment === change.comment;
}
});
}
if (index !== -1) {
result.rev = changes[index].id;
result.changes = changes.slice(0, index + 1);
2015-06-23 19:18:13 +00:00
result.changes.reverse();
}
// reverse back before return
changes = changes.reverse();
}
return result;
};