summaryrefslogtreecommitdiff
path: root/source/controller.js
diff options
context:
space:
mode:
authorMhykol <mchaeldonald62@pm.me>2024-05-28 10:41:49 -0400
committerMhykol <mchaeldonald62@pm.me>2024-05-28 10:41:49 -0400
commit2b994a685bf849144049343cf24b653bb42369ed (patch)
tree9c1b5c998ccffc19354cb2d83e1a3be3cee53f61 /source/controller.js
parent4a6e4ca37affe06880f11d08876451f20edeffe9 (diff)
Added meta tags to scripts and improved controller
Diffstat (limited to 'source/controller.js')
-rw-r--r--source/controller.js67
1 files changed, 34 insertions, 33 deletions
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`)
}
})
})