bit-bin
Version:
<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b
156 lines (118 loc) • 3.09 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.InsightManager = void 0;
function _bluebird() {
const data = require("bluebird");
_bluebird = function () {
return data;
};
return data;
}
function _defineProperty2() {
const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
_defineProperty2 = function () {
return data;
};
return data;
}
function _insightAlreadyExists() {
const data = _interopRequireDefault(require("./exceptions/insight-already-exists"));
_insightAlreadyExists = function () {
return data;
};
return data;
}
function _insightNotFound() {
const data = _interopRequireDefault(require("./exceptions/insight-not-found"));
_insightNotFound = function () {
return data;
};
return data;
}
class InsightManager {
/** insights is an insight registry */
constructor(
/**
* array of registered insights
*/
insights) {
(0, _defineProperty2().default)(this, "insights", new Map());
insights.forEach(insight => {
this.register(insight);
});
}
/**
* registers a new insight and returns the updated insight registry map
*/
register(insight) {
const name = insight.name;
if (this.insights.has(name)) {
throw new (_insightAlreadyExists().default)(name);
}
this.insights.set(name, insight);
}
/**
* list of all registered insights
*/
listInsights() {
return [...this.insights.keys()];
}
/**
* gets a specific insight by its name or undefined if doesn't exist
*/
getByName(insightName) {
return this.insights.get(insightName);
}
/**
* deletes a specific insight by its name if exists
*/
delete(insightName) {
if (!this.insights.has(insightName)) {
throw new (_insightNotFound().default)(insightName);
}
this.insights.delete(insightName);
}
/**
* execute an array of insights
*
*/
run(insightNames) {
var _this = this;
return (0, _bluebird().coroutine)(function* () {
const res = [];
yield Promise.all(insightNames.map( /*#__PURE__*/function () {
var _ref = (0, _bluebird().coroutine)(function* (insightName) {
const insight = _this.getByName(insightName);
if (insight) {
const insightRes = yield insight.run();
res.push(insightRes);
}
});
return function (_x) {
return _ref.apply(this, arguments);
};
}()));
return res;
})();
}
/**
* execute all insights in the registry
*
*/
runAll() {
var _this2 = this;
return (0, _bluebird().coroutine)(function* () {
const res = [];
for (const [, insight] of _this2.insights.entries()) {
// eslint-disable-next-line no-await-in-loop
const insightRes = yield insight.run();
res.push(insightRes);
}
return res;
})();
}
}
exports.InsightManager = InsightManager;