app.js
5.37 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
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
/*
* app.js: Core Application object for managing plugins and features in broadway
*
* (C) 2011, Nodejitsu Inc.
* MIT LICENSE
*
*/
var utile = require('utile'),
async = utile.async,
events = require('eventemitter2'),
bootstrapper = require('./bootstrapper'),
common = require('./common'),
features = require('./features');
var App = exports.App = function (options) {
//
// Setup options and `App` constants.
//
options = options || {};
this.root = options.root;
this.delimiter = options.delimiter || '::';
//
// Inherit from `EventEmitter2`
//
events.EventEmitter2.call(this, {
delimiter: this.delimiter,
wildcard: true
});
//
// Setup other relevant options such as the plugins
// for this instance.
//
this.options = options;
this.env = options.env || process.env['NODE_ENV'] || 'development'
this.plugins = options.plugins || {};
this.initialized = false;
this.bootstrapper = options.bootstrapper || bootstrapper;
this.initializers = {};
this.initlist = [];
//
// Bootstrap this instance
//
this.bootstrapper.bootstrap(this);
};
//
// Inherit from `EventEmitter2`.
//
utile.inherits(App, events.EventEmitter2);
//
// ### function init (options, callback)
// #### @options {Object} **Optional** Additional options to initialize with.
// #### @callback {function} Continuation to respond to when complete.
// Initializes this instance by the following procedure:
//
// 1. Initializes all plugins (starting with `core`).
// 2. Creates all directories in `this.config.directories` (if any).
// 3. Ensures the files in the core directory structure conform to the
// features required by this application.
//
App.prototype.init = function (options, callback) {
if (!callback && typeof options === 'function') {
callback = options;
options = {};
}
if (this.initialized) {
return callback();
}
var self = this;
options = options || {};
callback = callback || function () {};
this.env = options.env || this.env;
this.options = common.mixin({}, this.options, options);
function onComplete() {
self.initialized = true;
self.emit('init');
callback();
}
function ensureFeatures (err) {
return err
? onError(err)
: features.ensure(this, onComplete);
}
function initPlugin(plugin, next) {
if (typeof self.initializers[plugin] === 'function') {
return self.initializers[plugin].call(self, function (err) {
if (err) {
return next(err);
}
self.emit(['plugin', plugin, 'init']);
self.initializers[plugin] = true;
next();
});
}
next();
}
function initPlugins() {
async.forEach(self.initlist, initPlugin, ensureFeatures);
}
//
// Emit and respond with any errors that may short
// circuit the process.
//
function onError(err) {
self.emit(['error', 'init'], err);
callback(err);
}
//
// Run the bootstrapper, initialize plugins, and
// ensure features for this instance.
//
this.bootstrapper.init(this, initPlugins);
};
//
// ### function use(plugin, callback)
// Attachs the plugin with the specific name to this `App` instance.
//
App.prototype.use = function (plugin, options, callback) {
options = options || {};
if (typeof plugin === 'undefined') {
console.log('Cannot load invalid plugin!');
return callback && callback(new Error('Invalid plugin'));
}
var name = plugin.name,
self = this;
// If the plugin doesn't have a name, use itself as an identifier for the plugins hash.
if (!name) {
name = common.uuid();
}
if (this.plugins[name]) {
return callback && callback();
}
//
// Setup state on this instance for the specified plugin
//
this.plugins[name] = plugin;
this.options[name] = common.mixin({}, options, this.options[name] || {});
//
// Attach the specified plugin to this instance, extending
// the `App` with new functionality.
//
if (this.plugins[name].attach && options.attach !== false) {
this.plugins[name].attach.call(this, options);
}
//
// Setup the initializer only if `options.init` is
// not false. This allows for some plugins to be lazy-loaded
//
if (options.init === false) {
return callback && callback();
}
if (!this.initialized) {
this.initializers[name] = plugin.init || true;
this.initlist.push(name);
return callback && callback();
}
else if (plugin.init) {
plugin.init.call(this, function (err) {
var args = err
? [['plugin', name, 'error'], err]
: [['plugin', name, 'init']];
self.emit.apply(self, args);
return callback && (err ? callback(err) : callback());
});
}
};
//
// ### function remove(name)
// Detaches the plugin with the specific name from this `App` instance.
//
App.prototype.remove = function (name) {
// if this is a plugin object set the name to the plugins name
if (name.name) {
name = name.name;
}
if (this.plugins[name] && this.plugins[name].detach) {
this.plugins[name].detach.call(this);
}
delete this.plugins[name];
delete this.options[name];
delete this.initializers[name];
var init = this.initlist.indexOf(name);
if (init !== -1) {
this.initlist.splice(1, init);
}
}
//
// ### function inspect ()
// Inspects the modules and features used by the current
// application directory structure
//
App.prototype.inspect = function () {
};