From 2b994a685bf849144049343cf24b653bb42369ed Mon Sep 17 00:00:00 2001 From: Mhykol Date: Tue, 28 May 2024 10:41:49 -0400 Subject: Added meta tags to scripts and improved controller --- source/controller.js | 67 ++++++++++++++++++++++++++-------------------------- source/readfile.js | 60 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 87 insertions(+), 40 deletions(-) (limited to 'source') diff --git a/source/controller.js b/source/controller.js index 2c8cb37..c66df01 100644 --- a/source/controller.js +++ b/source/controller.js @@ -4,44 +4,26 @@ 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) => { + constructor() { + this.main = new Promise((res, rej) => { fs.readdir('./scripts/', (err, files) => { if (err) { - // ! Remember logging - logger.Error(`Error reading directory ${err}`) - rej(false) + logger.Error(`${err.code}: Failed to read directory`) + rej(`${err.code}: Failed to read directory`) } this.require = new Map() files.forEach(file => { try { - this.require.set(file.replace('.js', ''), require('../scripts/' + file)()) + this.require.set(file.replace('.js', ''), require(`../scripts/${file}`)()) } catch (err) { - logger.Error(err) + logger.Error(`${err.code}: Failed to read ${file}`) + rej(`${err.code}: Failed to read ${file}`) } }) res(this.require) }) - }) - - return loadScripts.then(output => { + }).then(output => { return new Promise((res, rej) => { const map = output const object = { @@ -55,21 +37,38 @@ class Controller { object.return.set(key, this.#GetMethods(value)) }) } else { - logger.Error(`'index.js' does not exist`) - rej(false) + logger.Error(`ERROR: 'index.js' does not exist`) + rej(`ERROR: 'index.js' does not exist`) } res(object.return) }) }) } + + async Main(x) { + return new Promise((res, rej) => { + this.#LoadModules(x).then(output => { + if (output) { + res(output) + } else { + logger.Error('ERROR: Unable to load modules') + rej('ERROR: Unable to load modules') + } + }) + }) + } + + #GetMethods(x) {return Method().GetMethods(x)} async #LoadModules(x) { return new Promise((res, rej) => { - this.#LoadScripts().then(output => { + this.main.then(output => { if (output.get('index')) { this.methods = new Map() + output.forEach((value, key) => { this.key = key - if (this.key == 'index') { + + if (this.key === 'index') { this.methods.set('/', value.get('Main')()) } else { value.forEach((value, key) => { @@ -81,13 +80,15 @@ class Controller { }) } }) - if (this.methods != undefined) { + if (this.methods !== undefined) { res(this.methods) } else { - rej(false) + logger.Error(`Error: Failed to get methods`) + rej(`Error: Failed to get methods`) } } else { - logger.Error(`'index.js' does not exist`) + logger.Error(`Error: 'index.js does not exist'`) + rej(`Error: 'index.js' does not exist`) } }) }) diff --git a/source/readfile.js b/source/readfile.js index 394ea2d..b0e0ed2 100644 --- a/source/readfile.js +++ b/source/readfile.js @@ -16,13 +16,13 @@ class ReadFile { this.object = { baseUrl: x.baseUrl, mime: '', - main: this.#Build(), + main: '', types: x.mime, layouts: { header: `