import {LogOptions} from "dimensions/configloader";
import Extension from "dimensions/extension";
import ListenServer from "dimensions/listenserver";
import Logger from "dimensions/logger";
import {requireNoCache} from "dimensions/utils";
import * as glob from "glob";
import * as path from "path";

class Extensions {
    public static folder: string = "./extensions";

    public static loadExtensions(extensionsList: Extension[], listenServers: { [name: string]: ListenServer }, options: LogOptions, logging: Logger) {
        glob.sync(`${this.folder}/**/index.js`).forEach((file) => {
            const extension: Extension = new (requireNoCache(path.resolve(file), require).default)();
            extensionsList[extension.name] = extension;
            if (typeof extension.setListenServers === "function") {
                extension.setListenServers(listenServers);
            }

            if (options.extensionLoad) {
                if (options.outputToConsole) {
                    console.log(`\u001b[36m[Extension] ${extension.name} ${extension.version} loaded.\u001b[37m`);
                }

                logging.appendLine(`[Extension] ${extension.name} ${extension.version} loaded.`);
            }
        });
    }
}

export default Extensions;
import {LogOptions} from "dimensions/configloader";
import Extension from "dimensions/extension";
import ListenServer from "dimensions/listenserver";
import Logger from "dimensions/logger";
import {requireNoCache} from "dimensions/utils";
import * as glob from "glob";
import * as path from "path";

class Extensions {
    public static folder: string = "./extensions";

    public static loadExtensions(extensionsList: Extension[], listenServers: { [name: string]: ListenServer }, options: LogOptions, logging: Logger) {
        try {
            glob.sync(`${this.folder}/**/index.js`).forEach((file) => {
                const extension: Extension = new (requireNoCache(path.resolve(file), require).default)();
                extensionsList[extension.name] = extension;
                if (typeof extension.setListenServers === "function") {
                    extension.setListenServers(listenServers);
                }

                if (options.extensionLoad) {
                    if (options.outputToConsole) {
                        console.log(`\u001b[36m[Extension] ${extension.name} ${extension.version} loaded.\u001b[37m`);
                    }

                    logging.appendLine(`[Extension] ${extension.name} ${extension.version} loaded.`);
                }
            });
        } catch(e) {
            if (options.outputToConsole) {
                console.log("Failed to load extensions. Error: ");
                console.log(e);
            }
            
            logging.appendLine(`Failed to load extensions. Error: `);
            logging.appendLine(e);
        }
    }
}

export default Extensions;
Add error handling for extension loading
Add error handling for extension loading
