-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
145 lines (116 loc) · 4.33 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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var async = require('async');
module.exports = function (sails) {
if(!sails) {
console.log('Warning! The Sails app injected into sails-util-mvcsloader seems invalid.');
}
var Loader = {
defaults: {},
injectPolicies: function (dir) {
require(__dirname + '/libs/policies')(sails, dir);
},
injectConfig: function (dir) {
require(__dirname + '/libs/config')(sails, dir);
},
injectControllers: function (dir, cb) {
require(__dirname + '/libs/controllers')(sails, dir, cb);
},
injectModels: function (dir, cb) {
require(__dirname + '/libs/models')(sails, dir, cb);
},
injectServices: function (dir, cb) {
require(__dirname + '/libs/services')(sails, dir, cb);
},
// Inject config and policies synchronously into the Sails app
configure: function (dir) {
if (!dir) {
dir = {
config: __dirname + '/../../config',
policies: __dirname + '/../../api/policies'
};
}
this.injectAll(dir);
},
// Inject models, controllers & services asynchronously into the Sails app
inject: function (dir, next) {
// No parameters or only a callback (function) as first parameter
if((typeof dir === 'function' || !dir) && !next) {
var tmp = next;
next = dir || function(){};
dir = tmp || {
models: __dirname + '/../../api/models',
controllers: __dirname + '/../../api/controllers',
services: __dirname + '/../../api/services'
};
}
// Backward compatibility, next and dir inverted
else if(typeof next === 'object' && typeof dir === 'function') {
var tmp = next;
next = dir;
dir = tmp;
}
// Be sure to have a callback
next = next || function(){};
this.injectAll(dir, next);
},
injectAll: function (dir, cb) {
cb = cb || function(){};
var self = this;
var loadModels = function (next) {
self.injectModels(dir.models, function (err) {
if (err) {
return next(err);
}
sails.log.info('User hook models loaded from ' + dir.models + '.');
return next(null);
});
};
var loadControllers = function (next) {
self.injectControllers(dir.controllers, function (err) {
if (err) {
return next(err);
}
sails.log.info('User hook controllers loaded from ' + dir.controllers + '.');
return next(null);
});
};
var loadServices = function (next) {
self.injectServices(dir.services, function (err) {
if (err) {
return next(err);
}
sails.log.info('User hook services loaded from ' + dir.services + '.');
return next(null);
});
};
if (dir.policies) {
self.injectPolicies(dir.policies);
sails.log.info('User hook policies loaded from ' + dir.policies + '.');
}
if (dir.config) {
self.injectConfig(dir.config);
sails.log.info('User hook config loaded from ' + dir.config + '.');
}
var toLoad = [];
if (dir.models) {
toLoad.push(loadModels);
}
if (dir.controllers) {
toLoad.push(loadControllers);
}
if (dir.services) {
toLoad.push(loadServices);
}
async.parallel(toLoad, function (err) {
if (err) {
sails.log.error(err);
}
if (cb) {
cb(err);
}
});
}
};
// Backward compatibility
Loader.adapt = Loader.inject;
return Loader;
};