UNPKG

2.38 kBJavaScriptView Raw
1/**
2 * We.js config and resouces loader
3 */
4
5var path = require('path');
6var fs = require('fs');
7var _ = require('lodash');
8var requireAll = require('require-all');
9var async = require('async');
10var Controller = require('./class/Controller.js');
11
12module.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 // this project dont have models and the models folder
27 if (!exists) return done();
28 // load and merge all models
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 // this project dont have controllers and the controllers folder
58 if (!exists) return done();
59 // load and merge all controllers
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}