beater-cli
Version:
A command-line interface for beater
90 lines (76 loc) • 4.58 kB
JavaScript
;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var childProcess = require('child_process');
var events_1 = require('events');
var os_1 = require('os');
var promise_1 = require('./globals/promise');
var Beater = function (_events_1$EventEmitte) {
_inherits(Beater, _events_1$EventEmitte);
function Beater(options) {
_classCallCheck(this, Beater);
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(Beater).call(this));
_this.reporter = options.reporter;
_this.files = options.files;
_this.pendingFiles = _this.files.slice();
_this.finishedFiles = [];
_this.procs = options.procs || os_1.cpus().length;
_this.requires = options.requires || [];
_this.fileResults = {};
_this.on('next', _this.nextFile.bind(_this));
return _this;
}
_createClass(Beater, [{
key: 'start',
value: function start() {
var _this2 = this;
return new promise_1.Promise(function (resolve, reject) {
_this2.once('finish', function (hasError) {
(hasError ? reject : resolve)();
});
_this2.reporter.started();
_this2.pendingFiles.splice(0, _this2.procs).forEach(function (file) {
return _this2.emit('next', file);
});
});
}
}, {
key: 'nextFile',
value: function nextFile(file) {
var _this3 = this;
this.fileResults[file] = [];
var args = this.requires.reduce(function (requires, require) {
return requires.concat(['--require', require]);
}, []);
var cp = childProcess.fork(file, [], { execArgv: args });
cp.on('message', function (m) {
if (m.type === 'report-test-started') {
_this3.reporter.testStarted(m.test);
} else if (m.type === 'report-test-finished') {
_this3.fileResults[file].push(m.result);
_this3.reporter.testFinished(m.result);
} else if (m.type === 'error') {}
});
cp.on('close', function () {
_this3.finishedFiles.push(file);
if (_this3.pendingFiles.length > 0) {
_this3.emit('next', _this3.pendingFiles.shift());
} else if (_this3.finishedFiles.length === _this3.files.length) {
_this3.reporter.finished(Object.keys(_this3.fileResults).reduce(function (results, file) {
return results.concat(_this3.fileResults[file]);
}, []));
var hasError = Object.keys(_this3.fileResults).some(function (file) {
return _this3.fileResults[file].some(function (result) {
return !!result.error;
});
});
_this3.emit('finish', hasError);
} else {}
});
}
}]);
return Beater;
}(events_1.EventEmitter);
exports.Beater = Beater;