card-sharing
Version:
card sharing config generator by parsing online sources
359 lines (273 loc) • 12.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
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; }; }();
var _core = require('./core');
var _outputs = require('./outputs');
var Outputs = _interopRequireWildcard(_outputs);
var _sources = require('./sources');
var Sources = _interopRequireWildcard(_sources);
var _clineTester = require('./utils/cline-tester');
var _clineTester2 = _interopRequireDefault(_clineTester);
var _promisePool = require('promise-pool');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var DEFAULT_OPTIONS = {
sources: undefined,
output: 'PlainLines'
};
var CardSharing = function () {
function CardSharing() {
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, CardSharing);
var constructor = this.constructor;
options = _extends({}, DEFAULT_OPTIONS, options);
var _options = options,
output = _options.output,
sources = _options.sources;
this.output = output;
this.sources = sources;
}
_createClass(CardSharing, [{
key: 'fetch',
value: function () {
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(disableCache) {
var configs, promisedConfigs, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, source, i;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
if (!(this._cache_configs && !disableCache)) {
_context.next = 2;
break;
}
return _context.abrupt('return', this._cache_configs);
case 2:
configs = this._cache_configs = [];
// Place here a race/all condition
promisedConfigs = [];
_iteratorNormalCompletion = true;
_didIteratorError = false;
_iteratorError = undefined;
_context.prev = 7;
for (_iterator = this.sources[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
source = _step.value;
promisedConfigs.push(source.get());
}
_context.next = 15;
break;
case 11:
_context.prev = 11;
_context.t0 = _context['catch'](7);
_didIteratorError = true;
_iteratorError = _context.t0;
case 15:
_context.prev = 15;
_context.prev = 16;
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
case 18:
_context.prev = 18;
if (!_didIteratorError) {
_context.next = 21;
break;
}
throw _iteratorError;
case 21:
return _context.finish(18);
case 22:
return _context.finish(15);
case 23:
_context.t1 = regeneratorRuntime.keys(promisedConfigs);
case 24:
if ((_context.t2 = _context.t1()).done) {
_context.next = 36;
break;
}
i = _context.t2.value;
_context.t3 = configs.push;
_context.t4 = configs;
_context.t5 = _toConsumableArray;
_context.next = 31;
return promisedConfigs[i];
case 31:
_context.t6 = _context.sent;
_context.t7 = (0, _context.t5)(_context.t6);
_context.t3.apply.call(_context.t3, _context.t4, _context.t7);
_context.next = 24;
break;
case 36:
// let i = 0
// for ( let source of this.sources ) {
// configs.push( ... await source.get() )
// i++
// if ( i > 3 ) break
// }
console.warn('Just fetched a total of ' + configs.length + ' configs');
// deduplicate similar
// if ( configs && configs.length > 1 ) {
// for ( let i = 0; i < configs.length; i++ ) {
// let a = configs[i].similar
// for ( let x = i - 1; x >= 0; x-- ) {
// let b = configs[x].similar
//
// if ( a == b ) {
// configs.splice( i, 1 )
// i--
// }
// }
// }
// }
console.warn('After similar deduplication, we got only ' + configs.length + ' configs');
return _context.abrupt('return', configs);
case 39:
case 'end':
return _context.stop();
}
}
}, _callee, this, [[7, 11, 15, 23], [16,, 18, 22]]);
}));
function fetch(_x2) {
return _ref.apply(this, arguments);
}
return fetch;
}()
}, {
key: 'test',
value: function () {
var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(configs) {
var _this = this;
var testedConfigs, pool;
return regeneratorRuntime.wrap(function _callee3$(_context3) {
while (1) {
switch (_context3.prev = _context3.next) {
case 0:
console.warn('Testing ' + configs.length + ' CLines, this process might take a while ');
testedConfigs = [];
pool = new _promisePool.Pool(function () {
var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(config) {
var host, port, username, password, clineTest;
return regeneratorRuntime.wrap(function _callee2$(_context2) {
while (1) {
switch (_context2.prev = _context2.next) {
case 0:
host = config.host, port = config.port, username = config.username, password = config.password;
clineTest = new _clineTester2.default(host, port, username, password, 1500);
_context2.next = 4;
return clineTest.isValid();
case 4:
if (!_context2.sent) {
_context2.next = 9;
break;
}
process.stderr.write('x');
testedConfigs.push(config);
_context2.next = 10;
break;
case 9:
process.stderr.write('.');
case 10:
case 'end':
return _context2.stop();
}
}
}, _callee2, _this);
}));
return function (_x4) {
return _ref3.apply(this, arguments);
};
}());
pool.concurrency = 20;
pool.retries = 2;
pool.retryInterval = 5000;
pool.add(configs);
_context3.next = 9;
return pool.start();
case 9:
console.warn('After testing the CLines, we got only ' + testedConfigs.length + ' configs');
return _context3.abrupt('return', testedConfigs);
case 11:
case 'end':
return _context3.stop();
}
}
}, _callee3, this);
}));
function test(_x3) {
return _ref2.apply(this, arguments);
}
return test;
}()
}, {
key: 'render',
value: function () {
var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee4() {
var configs, output;
return regeneratorRuntime.wrap(function _callee4$(_context4) {
while (1) {
switch (_context4.prev = _context4.next) {
case 0:
_context4.next = 2;
return this.fetch();
case 2:
configs = _context4.sent;
_context4.next = 5;
return this.test(configs);
case 5:
configs = _context4.sent;
_context4.next = 8;
return this.output.render(configs);
case 8:
output = _context4.sent;
return _context4.abrupt('return', output);
case 10:
case 'end':
return _context4.stop();
}
}
}, _callee4, this);
}));
function render() {
return _ref4.apply(this, arguments);
}
return render;
}()
}, {
key: 'output',
get: function get() {
return this._output;
},
set: function set(value) {
this._output = (typeof value === 'undefined' ? 'undefined' : _typeof(value)) == 'object' && value instanceof _core.Output && value ||
// By name. Example: 'plain-lines'
typeof value == 'string' && Outputs[value] && new Outputs[value]() ||
// default output
new Outputs[DEFAULT_OPTIONS.output]();
}
}, {
key: 'sources',
get: function get() {
return this._sources;
},
set: function set(value) {
var saved = this._sources = [];
if (!value) {
for (var name in Sources) {
var source = new Sources[name]();
saved.push(source);
}
} else {
// TODO: YOLO
}
}
}]);
return CardSharing;
}();
exports.default = CardSharing;