ircgrampp
Version:
IRCGram++ is a complexly simple Telegram <-> IRC Gateway
142 lines (102 loc) • 4.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _path = _interopRequireDefault(require("path"));
var _lodash = require("lodash");
var _package = _interopRequireDefault(require("../../package.json"));
var _config = _interopRequireWildcard(require("../config"));
var _hooks = require("../hooks");
var _injector = _interopRequireDefault(require("./injector"));
var _debug = _interopRequireDefault(require("debug"));
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const debug = (0, _debug.default)('plugins.interface');
const methodRegExp = '(before|after)([A-Z][a-z]+)((?:[A-Z][a-z]*)*)?$';
const LIB_PATH = _config.default.get('pluginspath');
const getMethods = function (oinstance) {
var props = [];
let instance = oinstance;
do {
props = props.concat(Object.getOwnPropertyNames(instance));
instance = Object.getPrototypeOf(instance);
} while (instance);
return (0, _lodash.uniq)(props).filter(x => {
return typeof oinstance[x] === 'function';
});
};
const translateHookMethod = function (mName) {
let reg = new RegExp(methodRegExp);
let parts = reg.exec(mName);
if (!parts) {
throw new Error(`${mName} don't match as hook method`);
}
let type = parts[1];
let nameSpace = parts[2];
let rest = parts[3] || "";
if (rest !== "") {
rest = rest.replace(/([A-Z])/g, '.$1').replace(/^\./, '');
}
return {
type: type,
name: `${nameSpace}:${rest}`.toLowerCase()
};
};
class PluginInterface {
constructor(pluginName) {
debug(`Loading plugin ${pluginName}`);
if (!pluginName.match(/^[a-z0-9]+$/i)) {
throw new Error(`Invalid plugin name ${pluginName}`);
}
pluginName = pluginName.toLowerCase();
this._name = pluginName;
this._config = (0, _config.getConfInterface)((0, _config.getPluginConfig)(this._name));
if (!this._config.get('enable')) {
throw new Error(`Plugin ${this._name} isn't enabled`);
}
let injector = new _injector.default(this);
let PluginClass = PluginInterface.getClass(this._name);
this._plugin = new PluginClass(injector);
if (!this._plugin.checkVersion()) {
throw new Error(`Plugin ${this._name} is incompible with this version`);
}
this.subscribeHooks();
}
subscribeHooks() {
let methods = getMethods(this._plugin);
methods.forEach(prop => {
if (prop.match(/^(after|before)/)) {
let hookInfo = translateHookMethod(prop);
(0, _hooks.subscribeTo)(hookInfo.name, hookInfo.type, this._plugin[prop].bind(this._plugin));
}
});
}
getConfig() {
return this._config;
}
get name() {
return this._name;
}
static load(name) {
return new PluginInterface(name);
}
static getClass(name) {
let pluginClass;
let project = _package.default.name.toLowerCase().replace(/[^a-z0-9]/ig, '');
let packName = _path.default.resolve(LIB_PATH, 'node_modules', `${project}-plugin-${name}`);
try {
pluginClass = require(packName).default;
} catch (e) {
throw new Error(`Plugin ${name} is not installed`);
}
/* if (!(pluginClass instanceof PluginBase)) {
debug('c', PluginBase);
debug(pluginClass);
throw new Error(`Plugin ${this._name} isn't a valid Plugin`);
} */
return pluginClass;
}
}
exports.default = PluginInterface;