forked from emahuni/sails-util-micro-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
210 lines (170 loc) · 6.03 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
const async = require('async'),
path = require('path'),
util = require('util'),
decache = require('decache'),
appDir = process.env.PWD;
module.exports = function(sails, hook_dirname) {
// make sure we don't cache this module, caching is preventing hook_dirname detection from working properly
decache(module.id);
if (!sails) {
console.log(sails);
throw new Error ('Error! The Sails app passed to sails-util-micro-apps seems invalid.');
}
hook_dirname = hook_dirname || path.dirname(module.parent.filename);
// sails.log.debug('sails: ', util.inspect(sails, {customInspect: false}));
// sails.log.debug('attempting to instantiate util');
// check if the Archive model has been exposed (past stage 5 of Sails app lifecycle), if this was being called from a hook then it'd be before stage 5
if (sails.isLifted || global['Archive']) throw new Error (`Cannot inject micro-app '${hook_dirname}' after Sails is lifted. Runtime injection not supported at the moment.`);
const Loader = {
defaults: {},
injectHooks: function(dir, cb) {
require(__dirname + '/libs/hooks')(sails, dir);
},
injectPolicies: function(dir) {
require(__dirname + '/libs/policies')(sails, dir);
},
injectViews: function(dir) {
require(__dirname + '/libs/views')(sails, dir, appDir);
},
injectAssets: function(dir) {
require(__dirname + '/libs/assets')(sails, dir, appDir);
},
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);
},
injectHelpers: function(dir, cb) {
require(__dirname + '/libs/helpers')(sails, dir, cb);
},
// Inject hooks, config and policies synchronously into the Sails app
configure: function(dir) {
if (!dir) {
dir = {
hooks: hook_dirname,
config: hook_dirname + '/config',
policies: hook_dirname + '/api/policies',
assets: hook_dirname + '/assets',
views: hook_dirname + '/views'
};
}
this.injectAll(dir);
},
// Inject models, controllers, helpers & services asynchronously into the Sails app
inject: function(dir, next) {
// sails.log.debug('hook dirname: ', hook_dirname);
// 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: hook_dirname + '/api/models',
controllers: hook_dirname + '/api/controllers',
helpers: hook_dirname + '/api/helpers',
services: hook_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('Micro-app-loader: 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('Micro-app-loader: User hook controllers loaded from ' + dir.controllers + '.');
return next(null);
});
};
var loadHelpers = function(next) {
self.injectHelpers(dir.helpers, function(err) {
if (err) {
return next(err);
}
sails.log.info('Micro-app-loader: User hook helpers loaded from ' + dir.helpers + '.');
return next(null);
});
};
var loadServices = function(next) {
self.injectServices(dir.services, function(err) {
if (err) {
return next(err);
}
sails.log.info('Micro-app-loader: User hook services loaded from ' + dir.services + '.');
return next(null);
});
};
// sails.log.debug('dirs: ', dir);
// we should add dependant hooks early during config before hooks are initialized
if (dir.hooks) {
self.injectHooks(dir.hooks);
sails.log.info('Micro-app-loader: Hooks loaded from ' + dir.hooks + '.');
}
if (dir.policies) {
self.injectPolicies(dir.policies);
sails.log.info('Micro-app-loader: User hook policies loaded from ' + dir.policies + '.');
}
if (dir.config) {
self.injectConfig(dir.config);
sails.log.info('Micro-app-loader: User hook config loaded from ' + dir.config + '.');
}
if (dir.views) {
self.injectViews(dir.views);
sails.log.info('Micro-app-loader: User hook config loaded from ' + dir.views + '.');
}
if (dir.assets) {
self.injectAssets(dir.assets);
sails.log.info('Micro-app-loader: User hook config loaded from ' + dir.assets + '.');
}
var toLoad = [];
if (dir.models) {
toLoad.push(loadModels);
}
if (dir.controllers) {
toLoad.push(loadControllers);
}
if (dir.helpers) {
toLoad.push(loadHelpers);
}
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;
};