From b6d3212828d1dabaaebe3e8f620e3261298642f5 Mon Sep 17 00:00:00 2001 From: Flummi Date: Tue, 20 Aug 2019 18:51:55 +0000 Subject: [PATCH] lul --- src/index.mjs | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/src/index.mjs b/src/index.mjs index 9f05438..dc53922 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -1,9 +1,8 @@ -import _fs from "fs"; +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 { @@ -13,34 +12,29 @@ export default class cuffeo extends EventEmitter { this.libs = {}; return (async () => { - await this.loadLibs(); - this.clients = await this.init(cfg); + this.libs = await this.loadLibs(); + this.clients = this.registerClients(cfg); return this; })(); } async loadLibs() { - const _clients = (await fs.readdir(`${__dirname}/clients`)).filter(f => f.endsWith(".mjs")); - for(const client of _clients) { + const _libs = {}; + for (const client of (await fs.promises.readdir(`${__dirname}/clients`)).filter(f => f.endsWith(".mjs"))) { const lib = await import(`./clients/${client}`); - this.libs[lib.default.name] = lib.default; + _libs[lib.default.name] = lib.default; } - return; + return _libs; } - 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"); + registerClients(cfg) { + return cfg.filter(e => e.enabled).map(srv => { + if(!Object.keys(this.libs).includes(srv.type)) + throw new Error(`not supported client: ${srv.type}`); - 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; + return { + name: srv.network, + type: srv.type, + client: new this.libs[srv.type](srv) + }; + }); } };