-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
447 lines (341 loc) · 9.56 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/**
* @description Loads directory modules and injects dependencies.
* @author Eliézer Augusto de Moraes Andrade
* @version 1.4.2
*/
//TODO: Move eloader to class inheriting Events
const fs = require('fs');
const path = require('path');
const Events = require('events');
const directory = require('./directory.js');
const Cache = require('./Cache.js');
class EloaderEvents extends Events {
async ask(event, obj, path) {
let res = {stop: false};
let t = this.emit(event, obj, res, path);
return res;
}
}
const events = new EloaderEvents();
/**
* Regex to get function params.
* @type {RegExp}
*/
const STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
const ARGUMENT_NAMES = /([^\s,]+)/g;
let $$cache = {};
let $$routeDirs = [];
let $$serviceDirs = [];
/**
* @public
* @deprecated Will be removed in future releases. Use eloder.on('run.error')
* @description Called on throw exception in run. Can be overwritten.
* @param {String} Module path.
* @param {Exception/String} Error.
* @return {Boolean} True to stop load another files.
*/
let fallback = function (path, err) {
// By default continue loadind another files.
return null;
};
/**
* Check if will exit on error.
* @param {String/Error} error The error
* @return {Boolean} False if error dont stop the process.
*/
let throws = function (error, path) {
if (!error instanceof Error) {
error = new Error(error);
}
//Check for deprecated method.
if (path) {
let res = fallback(path,error);
if (res !== null) {
console.warn('[Deprecated Error] ', path,'\n[Details]', error);
if (res === true) {
return process.exit(1);
} else {
return false;
}
}
}
events.ask('error', error, path).then((res) => {
if (res.stop) {
events.emit('warn', 'Stopped.');
process.exit(1);
}
});
return false;
};
/**
* @public
* @description Add a personal object istance to cache. Emit warn if try overwrite an object.
* @param {String} Dependency name.
* @param {Object} Dependency isntance.
* @return {Object} Eloader instance.
*/
let add = function (name, object) {
addObject(name, object);
return this;
}
/**
* @description Store an object to be injected. Name can't be equal node modules. (Including installed packages).
* @param {String} Name to be used in injection.
* @return {Object} Eloader instance.
*/
let addObject = function (name, object) {
events.emit('info', '[AddObject ] => ' + name);
if (!name) {
events.emit('warn','Name is required!');
return this;
}
if (isNodeModule(name)) {
events.emit('warn', 'This module ['+ name +'] already exists in node!');
return this;
}
if ($$cache[name]) {
events.emit('warn', 'Name ['+ name +'] already in use!');
return this;
}
$$cache[name] = new Cache(object);
return this;
}
/**
* @public
* @description Add a directory of routes.
* @param {String} Route directory.
* @param {Boolean} True to search inside directories.
* @return {Object} Eloader instance.
*/
let addRoutes = function (dir, recursive) {
events.emit('info', '[AddRoutes ] => ' + dir);
let route = new directory.Route(dir, recursive);
if (route.path) $$routeDirs.push(route);
return this;
}
/**
* @public
* @description Add a directory with modules to be used as service. Modules needs exports $name to be injected.
* @param {String} Dependency directory
* @param {Boolean} True to search inside directories.
* @return {Object} Eloader instance.
*/
let addServices = function (dir, recursive) {
events.emit('info', '[AddServices] => ' + dir);
let service = new directory.Service(dir, recursive);
if (service.path) {
$$serviceDirs.push(service);
} else {
events.emit('warn', '[Not found ] => ' + dir);
}
return this;
}
/**
* @public
* @description Gets the instance in cache or from require.
* @param {String} Dependency name. Can be node module.
* @return {Object} Dependency instance. If node_module or node, the require of this.
*/
let get = function (name, list) {
try {
let module = require.resolve(name);
return require(name);
} catch (err) {
let cache = $$cache[name];
if (!cache) {
events.emit('warn','Service ' + name + ' not found!');
return null;
}
if (cache.isClass ) {
return getClass(name, cache, list);
}
return cache.get(get);
}
}
/**
*
* @description Get or instantiate a class.
* @param {Cache} cache The source cache.
* @param {Array} list List previous dependencies.
* @return {Object} An instance or null if error.
*/
let getClass = function (name, cache, list) {
if (typeof cache.value !== 'string') {
return cache.value;
} else {
let tmp = cache.get(get);
let dependency = getParamNames(tmp);
let inject = [];
list = list || [];
list.push(name);
let check = list.some(function (ls) {
if (dependency.indexOf(ls) >= 0) {
throws('error', 'Circular reference: ' + list.join(' => ') + ' => ' + ls);
return true;
}
return false;
});
if (check) return null;
for (let i = 0; i < dependency.length; i++) {
inject.push(get(dependency[i], list));
}
return $$cache[name].value = initialize(cache.require(), inject);
}
}
/**
* @private
* @description Get the dependencies from module.
* @param {Function} A function
* @return {Array} Array of string with the params
* May use space or comma or both.
*/
let getParamNames = function (func) {
if (Array.isArray(func.$inject)) return func.$inject;
if(typeof func.$inject === 'string') return func.$inject.match(ARGUMENT_NAMES);
if (func.main) func = func.main; //To get from route.
let fnStr = func.toString().replace(STRIP_COMMENTS, '');
let result = fnStr.slice(fnStr.indexOf('(')+1, fnStr.indexOf(')')).match(ARGUMENT_NAMES);
//'inject1 inject2,inject3' = ['inject1', 'inject2', 'inject3']
if(result === null)
result = [];
return result;
};
/**
* @private
* @description Try resolve a name as node module.
* @param {String} Name of the module.
* @return {Boolean} True if exists a module with this name.
*/
let isNodeModule = function (name) {
try {
require.resolve(name);
return true;
} catch (err) {
return false;
}
}
/**
* @private
* @description Check if is class
* @param {Function} fn A function
* @return {Boolean} True if its a class.
*/
let isClass = function(fn) {
return /^\s*class\s+/.test(fn.toString());
}
/**
* @private
* @description Initialize a route/service
* @param {Function} fn The route to load.
* @param {Array} arr List dependency,
* @return {Boolean/Object} If class return instance, else true for success.
*/
let initialize = function (fn, arr) {
try {
if (typeof fn === 'function') {
if (isClass(fn)) {
return new (Function.prototype.bind.apply(fn, [null].concat(arr)))();
} else {
fn.apply(null, arr);
}
return true;
} else {
return false;
}
} catch(err) {
return false;
}
}
/**
* @public
* @description Load a module as route.
* @param {String} Module path.
* @return {Boolean} True if module loads.
*/
let load = function (path) {
let dependencies = [];
let module = require(path);
if (typeof module.main !== 'function') {
events.emit('warn','Module without main.');
return false;
}
var list = getParamNames(module);
for (let i = 0; i < list.length; i++) {
let dependency = get(list[i])
if (dependency === null) return false;
dependencies.push(dependency);
}
initialize(module.main, dependencies);
return true;
}
/**
* Load all directories added by addRoutes and addServices
* @param {Boolean} Search in sub directories.
*/
let loadList = function (includeDirs) {
while ($$serviceDirs.length > 0) {
let services = $$serviceDirs.pop().searchSync(includeDirs);
for (let i = 0; i < services.length; i++) {
loadService(services[i]);
}
}
while ($$routeDirs.length > 0) {
let routes = $$routeDirs.pop().searchSync(includeDirs);
for (let j = 0; j < routes.length; j++ ) {
try {
if (!load(routes[j])) {
throws('Not loaded ' + routes[j], routes[j]);
}
} catch(err) {
throws(err, routes[j]);
}
}
}
}
/**
* @private
* @description Load service.
* @param {String} name [description]
* @param {String} path [description]
* @return {[type]} [description]
*/
let loadService = function (path) {
let req = require(path);
let name = req.$name;
if (!name) {
events.emit('info', '[Ignore ] => '+ path);
return;
}
$$cache[name] = new Cache(path, true, isClass(req));
}
/**
* @public
* @description Start to load modules.
* @param {String} Initial path to search for files.
* @optional {Boolean} Includes directories in search.
* @return {Object} Instance of this module.
*/
let run = function (path, includeDirs) {
if (path) addRoutes(path);
try {
loadList(includeDirs);
} catch (err) {
throws(err);
}
events.emit('load', true);
return this; //Just for retrocompatibility because it needs to load another route dir.
//run('\\routesA') since 1.1 use addRoutes();
//run('\\routesB')
}
//New 1.2
module.exports = events; //Emits errors, warns and info.
//New 1.1
module.exports.addRoutes = addRoutes; //Add directory of routes to be loaded on run.
module.exports.addServices = addServices; //Add directory of services to be loaded on run.
module.exports.addObject = addObject; //Add an Object like add.
//Since 1.0
module.exports.run = run;
module.exports.add = add; //Add Object (Keep to retrocompatibility)
module.exports.get = get; //Like require.
module.exports.load = load;
module.exports.fallback = fallback; //@deprecated On error loading routes.