-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·49 lines (46 loc) · 2.13 KB
/
app.js
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
// --- importing modules --- //
mod ={};
// read modules list
var fs = require('fs');
try {
var modules_config = JSON.parse(fs.readFileSync('./modules/modules.json', 'utf8'));
} catch (e) {
var modules_config = {};
}
// loop on modules found
var i = 0;
var load_next = function () {
if(modules_config.modules.length > i){ // there is more modules to load
var my_module = modules_config.modules[i]; // select module config
process.stdout.write(new Date().toISOString() + " : Loading " + my_module.name + " ... ");
if(my_module.async_init){ // the module contains async functions, so we have to wait for them to finish
mod[my_module.name] = require(( // add module's public results to main modules object
my_module.entry? // modules's config provides an entry file
my_module.entry.replace('{modules_root}', './modules') // so start it
:'./modules/' + my_module.name) // else we start a predefined entry
)(function () { // this function will be provided to mdule's entry as a callback, so it will run our callback after it finishes it's job
if(typeof(mod[my_module.name].init) == 'function') mod[my_module.name].init(); // run module's inti function if it's neccesary
process.stdout.write(" done\n");
i+=1;
load_next(); // forward to next module
});
}else{ // run module normally
mod[my_module.name] = require(( // add module's public results to main modules object, but this time without providing callback function
my_module.entry?
my_module.entry.replace('{modules_root}', './modules')
:'./modules/' + my_module.name)
);
if(typeof(mod[my_module.name].init) == 'function') mod[my_module.name].init(); // run module's inti function if it's neccesary
process.stdout.write(" done\n");
i+=1;
load_next(); // forward to next module
}
}else{ // all modules was loaded
start_modules();
}
};
load_next(); // start loading modules
// --- start imported modules --- //
var start_modules = function () {
Object.keys(mod).forEach(function (m) { if(typeof(mod[m].start) == 'function') mod[m].start(); });
};