initializers.js
3.9 KB
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
/**
* Module dependencies.
*/
var path = require('path')
, fs = require('fs')
, existsSync = fs.existsSync || path.existsSync // node <=0.6
, debug = require('debug')('bootable-di');
var IoC = require('electrolyte');
var async = require('asyncawait/async');
var await = require('asyncawait/await');
/**
* Initializer execution phase.
*
* This phase will execute all initializer scripts in a directory, allowing the
* application to initialize modules, including connecting to databases and
* other network services. Any annotated dependencies (using `exports['@require']`)
* will be injected into the function when invoked.
*
* Examples:
*
* app.phase(bootabledi.initializers());
*
* app.phase(bootabledi.initializers('config/initializers'));
*
* @param {String|Object} options
* @return {Function}
* @api public
*/
module.exports = function (options, container) {
if (!container) {
try {
container = require('electrolyte');
} catch (_) {
// workaround when `npm link`'ed for development
var prequire = require('../utils/parent-require');
container = prequire('electrolyte');
}
}
if ('string' == typeof options) {
options = { dirname: options };
}
options = options || {};
var dirname = options.dirname || 'etc/init'
, extensions = options.extensions || Object.keys(require.extensions).map(function (ext) { return ext; })
, exts = extensions.map(function (ext) {
if ('.' != ext[0]) { return ext; }
return ext.slice(1);
})
, regex = new RegExp('\\.(' + exts.join('|') + ')$');
return function initializers(done) {
var dir = path.resolve(dirname);
if (!existsSync(dir)) { return done(); }
var self = this
, files = fs.readdirSync(dir).sort()
, idx = 0;
function next(err) {
if (err) { return done(err); }
var file = files[idx++];
// all done
if (!file) { return done(); }
if (regex.test(file)) {
try {
debug('initializer %s', file);
var mod = require(path.join(dir, file))
, deps = mod['@require'] || []
, args = [];
var loadDeps = async(function () {
for (var i = 0, len = deps.length; i < len; ++i) {
var inst = await(IoC.create(deps[i]));
args.push(inst);
}
});
loadDeps().then(function () {//异步加载依赖
if (typeof mod == 'function') {
var arity = mod.length;
if (arity == deps.length + 1) {
// Async initializer. Exported function will be invoked, with next
// being called when the initializer finishes.
args.push(next);
mod.apply(self, args);
} else {
// Sync initializer. Exported function will be invoked, with next
// being called immediately.
mod.apply(self, args);
next();
}
} else {
// Initializer does not export a function. Requiring the initializer
// is sufficient to invoke it, next immediately.
next();
}
});
} catch (ex) {
next(ex);
}
} else {
next();
}
}
next();
};
};