1 |
|
2 |
|
3 |
|
4 |
|
5 | var path = require('path');
|
6 | var fs = require('fs');
|
7 | var _ = require('lodash');
|
8 | var requireAll = require('require-all');
|
9 | var async = require('async');
|
10 | var Controller = require('./class/Controller.js');
|
11 |
|
12 | module.exports = {
|
13 | loadModels: function loadModels(we, cb) {
|
14 | var models = {};
|
15 | async.parallel([
|
16 | function loadPluginModels(done) {
|
17 | for (var i = 0; i < we.pluginNames.length; i++) {
|
18 | if (!we.plugins[we.pluginNames[i]].models) continue;
|
19 | _.merge(models, we.plugins[we.pluginNames[i]].models);
|
20 | }
|
21 | done();
|
22 | },
|
23 | function loadProjectModels(done) {
|
24 | var PMP = path.resolve(we.projectPath, 'server', 'models');
|
25 | fs.exists(PMP, function (exists) {
|
26 |
|
27 | if (!exists) return done();
|
28 |
|
29 | _.merge(models, requireAll({
|
30 | dirname : PMP,
|
31 | filter : /(.+)\.js$/,
|
32 | resolve : function (model) {
|
33 | return model(we);
|
34 | }
|
35 | }));
|
36 | done();
|
37 | });
|
38 | }
|
39 | ], function (err) {
|
40 | return cb(err, models);
|
41 | });
|
42 | },
|
43 |
|
44 | loadControllers: function loadControllers(we, cb) {
|
45 | var controllers = {};
|
46 | async.parallel([
|
47 | function loadPluginControllers(done) {
|
48 | async.each(we.pluginNames, function(pluginName, next) {
|
49 | if (!we.plugins[pluginName].controllers) return next();
|
50 | _.merge(controllers, we.plugins[pluginName].controllers);
|
51 | next();
|
52 | }, done);
|
53 | },
|
54 | function loadProjectControllers(done) {
|
55 | var PMP = path.resolve(we.projectPath, 'server', 'controllers');
|
56 | fs.exists(PMP, function(exists) {
|
57 |
|
58 | if (!exists) return done();
|
59 |
|
60 | _.merge(controllers, requireAll({
|
61 | dirname : PMP,
|
62 | filter : /(.+)\.js$/
|
63 | }));
|
64 |
|
65 | done();
|
66 | });
|
67 | }
|
68 | ], function (err) {
|
69 | var controllerOO = {};
|
70 | var controllerNames = Object.keys(controllers);
|
71 | controllerNames.forEach(function(n) {
|
72 | controllerOO[n] = new Controller(controllers[n]);
|
73 | });
|
74 |
|
75 | return cb(err, controllerOO);
|
76 | });
|
77 | }
|
78 | }
|