forked from inventures/hatchjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
167 lines (145 loc) · 4.68 KB
/
module.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
module.exports = HatchModule;
var fs = require('fs');
var path = require('path');
// Debug anchor: hmod
var debug = function() {};
if (process.env.NODE_DEBUG && /hmod/.test(process.env.NODE_DEBUG)) {
var $ = require('./colors').$;
debug = function(x) {
$.puts($('HATCH MODULE: ').blue + x);
};
}
/**
* Hatch module class.
*
* @param {HatchPlatform} hatch - hatch platform.
* @param {Function} mod - module initializer.
* @constructor
*/
function HatchModule(hatch, name, mod) {
debug('instantiate module ' + name);
var module = this;
this.hatch = hatch;
this.name = name;
if ('function' === typeof mod) {
this.app = mod(hatch.compound);
this.compound = this.app && this.app.compound || null;
} else {
return;
}
var compound = module.compound;
var app = module.app;
if (compound) {
compound.parent = hatch.compound;
compound.hatch = hatch;
module.path = compound.root;
Object.keys(compound.parent.models).forEach(function(m) {
compound.models[m] = compound.parent.models[m];
});
}
module.loadConfig();
module.loadPages();
module.registerHooks();
module.registerWidgets();
if (compound) {
compound.on('ready', function() {
if (!compound.injectMiddlewareBefore(app.router, hatch.middleware.hatch(compound))) {
app.use(app.router);
}
hatch.compound.emit('module', module);
});
} else {
hatch.compound.emit('module', module);
}
}
/**
* Register hooks for module. Called on initialization of module.
*/
HatchModule.prototype.registerHooks = function registerHooks() {
var mod = this;
if (!this.compound) {
return;
}
this.compound.on('before controller', function(ctl, act, req, res, next) {
// TODO: understand why this is required
// if (req.pagePath !== req.url) {
ctl.pathTo = ctl.path_to = mod.compound.map.clone(req.req.pagePath);
// }
});
};
HatchModule.prototype.get = function get(key) {
return this.app.get(key);
};
HatchModule.prototype.set = function set(key, val) {
return this.app.set(key, val);
};
/**
* Initialize widgets listed in ./config/widgets.yml
*/
HatchModule.prototype.registerWidgets = function registerWidgets() {
if (!fs.existsSync(this.path + '/config/widgets.yml')) {
debug('no widgets declared');
return;
}
debug('register widgets');
var mod = this;
var ws = require(this.path + '/config/widgets.yml')[0];
if (ws) {
debug('register widgets: ' + Object.keys(ws));
Object.keys(ws).forEach(function (w) {
mod.hatch.widget.register(mod.name, w, {info: ws[w]});
});
}
mod.compound.on('routes', function (map) {
map.namespace('widgets', function (widgets) {
widgets.post(':controller/:action');
widgets.post('*', function notFound(req, res) {
res.send('Widget for ' + req.url + ' not found');
});
});
});
mod.compound.on('structure', function (structure) {
structure.controllers['widgets/common_controller'] = mod.compound.parent.structure.controllers['widgets_common_controller'];
structure.views['layouts/widgets_layout'] = mod.compound.parent.structure.views['layouts/widgets_layout'];
if (structure.helpers['application_helper']) {
var source = mod.compound.parent.structure.helpers['widgets_common_helper'];
var destination = structure.helpers['application_helper'];
Object.keys(source).forEach(function (helperName) {
destination[helperName] = source[helperName];
});
} else {
structure.helpers['application_helper'] = mod.compound.parent.structure.helpers['widgets_common_helper'];
}
});
};
/**
* Initialize special pages located in ./app/pages
*/
HatchModule.prototype.loadPages = function loadPages() {
var hatch = this.hatch;
var pagesPath = this.path + '/app/pages/';
if (!fs.existsSync(pagesPath)) {
return;
}
debug('load pages');
fs.readdirSync(pagesPath).forEach(function (file) {
var name = file.replace(/\.(js|coffee)$/, '');
hatch.page.register(name, require(pagesPath + file));
});
}
/**
* Load config from ./config/module.yml
*/
HatchModule.prototype.loadConfig = function loadConfig() {
var mod = this;
var confPath = this.path + '/config/module.yml';
if (fs.existsSync(confPath)) {
debug('load config');
try {
mod.info = require(confPath)[0];
} catch (e) {}
}
if (!mod.info) {
mod.info = {};
}
};