summaryrefslogtreecommitdiff
path: root/source/controller.js
blob: c66df017ef2b7cdad5d04c34a3ab35f941d590d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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() {
        this.main = new Promise((res, rej) => {
            fs.readdir('./scripts/', (err, files) => {
                if (err) {
                    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}`)())
                    } catch (err) {
                        logger.Error(`${err.code}: Failed to read ${file}`)
                        rej(`${err.code}: Failed to read ${file}`)
                    }
                })
                res(this.require)
            })
        }).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(`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.main.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 {
                        logger.Error(`Error: Failed to get methods`)
                        rej(`Error: Failed to get methods`)
                    }
                } else {
                    logger.Error(`Error: 'index.js does not exist'`)
                    rej(`Error: 'index.js' does not exist`)
                }
            })
        })
    }
}

module.exports = Controller