-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (42 loc) · 1.59 KB
/
index.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
'use strict';
var fs = require('fs');
var path = require('path');
var ModuleManager = require('./ModuleManager');
var instances = [];
var modex = function(options) {
var instance = ModuleManager.create(options, instances.length);
//var instance = {start:function(){console.log('starting')}};
instances.push(instance);
return instance;
}
/** Get the Manager instance with the specified id. If no id specified, get the last created instance */
modex.getInstance = function(id) {
if (instances.length > 0) return instances[instances.length-1];
}
modex.middleware = ModuleManager.prototype.middleware = require('./middleware');
modex.models = ModuleManager.prototype.models = requireAllJsFilesInDir('./models');
modex.views = ModuleManager.prototype.views = require('./views');
modex.controllers = ModuleManager.prototype.controllers = requireAllJsFilesInDir('./controllers');
modex.helper = ModuleManager.prototype.helper = {
model : require('./helper.model'),
view : require('./helper.view'),
controller : require('./helper.controller')
};
function requireAllJsFilesInDir(dirPath) {
var c = {};
var absDirPath = path.resolve(__dirname, dirPath)
var files = fs.readdirSync(absDirPath);
files.forEach(function(fname) {
var fp = path.join(absDirPath, fname);
if (fs.statSync(fp).isDirectory()) {
c[fname] = requireAllJsFilesInDir(dirPath + '/' + fname);
} else {
var ext = path.extname(fname);
if (ext === '.js') {
c[path.basename(fname, ext)] = require(dirPath + '/' + fname);
}
}
});
return c;
}
module.exports = modex;