-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
217 lines (202 loc) · 6.32 KB
/
app.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
var fs = require('fs');
var express = require('express');
var packagejson = require('./package.json');
var coffee = require('coffee-script');
app = {
find: function (name, group) {
var i, j, k;
if (group) {
if (group === 'services') {
for (i in app.services) {
if (name === app.services[i].name) return app.services[i].exports;
}
} else if (group === 'modules' || group === 'controllers') {
for (j = 0; j < app[group].length; j++) {
if (name === app[group][j].name) return app[group][j].exports;
}
}
} else {
for (i in app.services) {
if (name === app.services[i].name) return app.services[i].exports;
}
for (j in app.modules) {
if (name === app.modules[j].name) return app.modules[j].exports;
}
for (k = 0; k < app.controllers.length; k++) {
if (name === app.controllers[k].name) return app.controllers[k].exports;
}
}
},
services: {}
};
function extendPackage(env) {
if (env.dependencies) {
var deps = env.dependencies;
for (var key in deps) {
packagejson.dependencies[key] = deps[key];
}
}
}
/*
Module
@constructor
@desc model for controllers and modules
*/
function Module(name, env, folder) {
if (folder) this.folder = folder;
else this.folder = 'controllers';
this.settings = {
libraries: '',
env: env,
name: name
};
}
Module.prototype.load = function () {
var settings = this.settings;
// attributes = require('./'+ this.folder + '/'+this.name+'/package.json');
var isCore = /^\.\//;
this.exports = require('./' + this.folder + '/' + this.name + '/index.js');
return this;
};
/*
Package
@constructor
@desc model for controllers and modules
*/
function Package(name, env, folder) {
if (folder) this.folder = folder;
else this.folder = 'controllers';
this.settings = {
libraries: '',
env: env,
name: name
};
}
Package.prototype.load = function (o) {
var settings = this.settings;
this.attributes = require('./' + this.folder + '/' + this.name + '/package.json');
var isCore = /^\.\//;
this.exports = require('./' + this.folder + '/' + this.name + '/index');
if (o.assets) app.services.express.exports.use('/' + this.name, express.static('./' + this.folder + '/' + this.name + '/assets'));
};
Package.prototype.init = function (options) {
if (this.folder === 'controllers' && typeof this.exports.prototype === 'object') new this.exports(this.attributes);
else if (typeof this.exports === 'function') this.exports.call(this.attributes);
};
function Interface(name, env, folder) {
if (folder) this.folder = folder;
else this.folder = 'controllers';
this.settings = {
libraries: '',
env: env,
name: name
};
}
Interface.prototype = {
constructor: Interface,
load: function (o) {
var settings = this.settings;
this.attributes = require('./' + this.folder + '/' + this.name + '/package.json');
var isCore = /^\.\//;
this.exports = require('./' + this.folder + '/' + this.name + '/index');
this.init = this.exports.init;
}
};
// load services
// the services folder works just like the controllers folder
// what you do inside the folder will be different
exports.loadServices = function () {
services = fs.readdirSync(__dirname + '/services');
if (services) {
var packages = [];
for (var i = 0, len = services.length; i < len; i++) {
if (fs.statSync(__dirname + '/services/' + services[i]).isDirectory()) {
var env;
var folderservices = fs.readdirSync(__dirname + '/services/' + services[i]);
if (require(__dirname + '/services/' + services[i] + '/package.json')) {
env = require(__dirname + '/services/' + services[i] + '/package.json');
} else {
env = app.env;
}
var pack = new Interface(services[i], env, 'services');
pack.name = services[i];
packages.push(pack);
}
}
for (var a = 0, l = packages.length; a < l; a++) {
// services don't have assets
packages[a].load({
assets: false
});
app.services[packages[a].name] = packages[a];
}
}
};
exports.loadControllers = function () {
// load controllers
var controllers = fs.readdirSync('./controllers');
if (controllers) {
var packages = app.controllers = [];
for (var i = 0, len = controllers.length; i < len; i++) {
if (fs.statSync('./controllers/' + controllers[i]).isDirectory()) {
var env;
var folderFiles = fs.readdirSync('./controllers/' + controllers[i]);
if (require('./controllers/' + controllers[i] + '/package.json')) {
env = require('./controllers/' + controllers[i] + '/package.json');
} else {
env = app.env;
}
var pack = new Package(controllers[i], env);
extendPackage(env);
pack.name = controllers[i];
packages.push(pack);
}
}
var l = packages.length;
for (var a = 0; a < l; a++) {
packages[a].load({
assets: true
});
}
for (var b = 0; b < l; b++) {
packages[b].init({});
}
}
};
exports.loadModules = function () {
// load modules
var files = fs.readdirSync('./modules');
var packages = [];
if (files.length) {
for (var i = 0, len = files.length; i < len; i++) {
if (fs.statSync('./modules/' + files[i]).isDirectory()) {
var env;
var folderFiles = fs.readdirSync('./modules/' + files[i]);
require('./modules/' + files[i] + '/package.json') ?
env = require('./modules/' + files[i] + '/package.json') : env = app.env;
var pack = new Module(files[i], env, 'modules');
pack.name = files[i];
packages.push(pack);
}
}
app.modules = {};
for (var a = 0, l = packages.length; a < l; a++) {
var module = packages[a].load();
app.modules[module.name] = module.exports;
}
}
};
exports.initServices = function () {
var services = app.services;
for (var i in services) {
console.log('initializing:', services[i].folder, services[i].name);
if (typeof services[i].init === 'function') services[i].init();
}
};
exports.createPackage = function () {
/* build package.json */
// pull packages from controllers, services, and modules
// extend with current package.json
// write to file
fs.writeFileSync('./package.json', JSON.stringify(packagejson, null, 4));
}