dynamic libimport and more

This commit is contained in:
Flummi 2019-08-20 18:01:01 +00:00
parent d8655e4d87
commit cc58237696
6 changed files with 49 additions and 41 deletions

View File

@ -1,38 +0,0 @@
import { irc as irclib } from "./src/clients/irc";
import { tg as tglib } from "./src/clients/tg";
import EventEmitter from "events";
const clients = [];
export default class cuffeo extends EventEmitter {
constructor(cfg) {
super();
for (let srv in cfg) {
if(cfg[srv].enabled) {
switch (cfg[srv].type) {
case "irc":
clients.push({
name: cfg[srv].network,
type: "irc",
client: new irclib(cfg[srv])
});
break;
case "tg":
clients.push({
name: "tg",
type: "tg",
client: new tglib(cfg[srv])
});
break;
}
}
}
clients.forEach(client => {
client.client.on("data", e => {
this.emit(e[0], e[1]);
});
});
}
};

View File

@ -2,7 +2,7 @@
"name": "cuffeo", "name": "cuffeo",
"version": "1.0.1", "version": "1.0.1",
"description": "lul", "description": "lul",
"main": "index.mjs", "main": "src/index.mjs",
"scripts": { "scripts": {
}, },
"repository": { "repository": {

View File

@ -35,7 +35,7 @@ const replaceColor = (match, color, text) => {
return text; return text;
}; };
export class irc extends EventEmitter { export default class irc extends EventEmitter {
constructor(options) { constructor(options) {
super(); super();
this.options = options || {}; this.options = options || {};

View File

@ -1,7 +1,7 @@
import fetch from "flumm-fetch-cookies"; import fetch from "flumm-fetch-cookies";
import EventEmitter from "events"; import EventEmitter from "events";
export class tg extends EventEmitter { export default class tg extends EventEmitter {
constructor(options) { constructor(options) {
super(); super();
this.options = options || {}; this.options = options || {};

46
src/index.mjs Normal file
View File

@ -0,0 +1,46 @@
import _fs from "fs";
import { fileURLToPath } from "url";
import { dirname } from "path";
import EventEmitter from "events";
const fs = _fs.promises;
const __dirname = dirname(fileURLToPath(import.meta.url));
export default class cuffeo extends EventEmitter {
constructor(cfg) {
super();
this.clients = [];
this.libs = {};
return (async () => {
await this.loadLibs();
this.clients = await this.init(cfg);
return this;
})();
}
async loadLibs() {
const _clients = (await fs.readdir(`${__dirname}/clients`)).filter(f => f.endsWith(".mjs"));
for(const client of _clients) {
const lib = await import(`./clients/${client}`);
this.libs[lib.default.name] = lib.default;
}
return;
}
async init(cfg) {
const clients = [];
for (const srv in cfg) {
if(!cfg[srv].enabled)
return new Error("not enabled");
if(!Object.keys(this.libs).includes(cfg[srv].type))
return new Error("not supported client");
clients.push({
name: cfg[srv].network,
type: cfg[srv].type,
client: new this.libs[cfg[srv].type](cfg[srv])
});
}
clients.forEach(client => client.client.on("data", e => this.emit(e[0], e[1])));
return clients;
}
};