const fs = require('fs') const Method = require('./method') const logger = require('./logger')() // Reads all scripts in the `scripts` folder, sends to `Method` class to get all methods in the class, and send output to `app.js` class Controller { constructor() {} async Main(x) { return new Promise((res, rej) => { this.#LoadModules(x).then(output => { if (output) { res(output) } else { logger.Error('Unable to load modules') rej(false) } }) }) } #GetMethods(x) {return Method().GetMethods(x)} async #LoadScripts() { const loadScripts = new Promise((res, rej) => { fs.readdir('./scripts/', (err, files) => { if (err) { // ! Remember logging logger.Error(`Error reading directory ${err}`) rej(false) } this.require = new Map() files.forEach(file => { try { this.require.set(file.replace('.js', ''), require('../scripts/' + file)()) } catch (err) { logger.Error(err) } }) res(this.require) }) }) return loadScripts.then(output => { return new Promise((res, rej) => { const map = output const object = { map: output, index: map.get('index'), return: new Map() } if (object.index) { object.map.forEach((value, key) => { object.return.set(key, this.#GetMethods(value)) }) } else { logger.Error(`'index.js' does not exist`) rej(false) } res(object.return) }) }) } async #LoadModules(x) { return new Promise((res, rej) => { this.#LoadScripts().then(output => { if (output.get('index')) { this.methods = new Map() output.forEach((value, key) => { this.key = key if (this.key == 'index') { this.methods.set('/', value.get('Main')()) } else { value.forEach((value, key) => { if (key === 'Main') { this.methods.set('/' + this.key.toLowerCase(), value(x)) } else { this.methods.set('/' + this.key.toLowerCase() + '/' + key.toLowerCase(), value(x)) } }) } }) if (this.methods != undefined) { res(this.methods) } else { rej(false) } } else { logger.Error(`'index.js' does not exist`) } }) }) } } module.exports = Controller