forked from FabricLabs/maki
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Maki.js
237 lines (186 loc) · 6.38 KB
/
Maki.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
'use strict';
if (process.env.NODE_ENV === 'debug') require('debug-trace')({ always: true });
var fs = require('fs-extra');
var _ = require('lodash');
var colors = require('colors');
var util = require('util');
var async = require('async');
var Maki = function( config ) {
var defaults = require( __dirname + '/../config');
this.config = _.merge( defaults , config );
this.debug = (process.env.NODE_ENV === 'debug');
this.routes = {};
this.clients = {};
this.plugins = {};
this.resources = {};
this.services = {}; // stores instances
this._services = {}; // stores classes
this.types = {}; // stores [custom] attribute types
this.resourcePlugins = {};
this.resourceModifiers = {};
this.Datastore = require('../lib/Datastore');
this.Resource = require('../lib/Resource');
this.Messenger = require('../lib/Messenger');
this.Service = require('../lib/Service');
this.Queue = require('maki-queue');
this.mongoose = require('mongoose');
//this.mockgoose = require('mongoose');
//this.mockgoose = require('mockgoose')( require('../node_modules/mongoose') );
this.register('http', require('../lib/Service/http') );
};
util.inherits( Maki , require('events').EventEmitter );
Maki.prototype.define = function( name , options ) {
var self = this;
if (!name) throw new Error('"name" is required.');
//Apply all resource modifiers to this resource's options
if (self.resourceModifiers[ name ]) {
self.resourceModifiers[ name ].forEach(function( modifier ) {
options = modifier( options );
});
}
var resource = new self.Resource( name , options );
resource.attach( self );
self.emit('resource', resource );
return resource;
};
Maki.prototype.use = function( plugin ) {
var self = this;
if (plugin.extends.resources) {
Object.keys( plugin.extends.resources ).forEach(function( n ) {
//Build array of plugins which extend this resource
if (!self.resourcePlugins[ n ]) self.resourcePlugins[ n ] = [];
self.resourcePlugins[ n ].push( plugin.extends.resources[ n ].plugin );
//Build array of modifiers which extend this resource
if (!self.resourceModifiers[ n ]) self.resourceModifiers[ n ] = [];
if (plugin.extends.resources[ n ].modifier) {
self.resourceModifiers[ n ].push( plugin.extends.resources[ n ].modifier );
}
});
}
if (plugin.extends.services) {
if (plugin.extends.services.http) {
if (!self.plugins['http']) self.plugins['http'] = [];
self.plugins['http'].push( plugin.extends.services.http );
}
}
return self;
};
Maki.prototype.register = function( name , service ) {
this._services[ name ] = service;
return this;
};
Maki.prototype.serve = function( services , cb ) {
var self = this;
if (typeof services === 'String') var services = [ services ];
if (!self.resources['Index']) {
console.log(
'[WARNING]'.yellow,
'Maki is now serving content, but no index was defined',
'so it created one for you.'
);
self.resources['Index'] = new self.Resource('Index', {
name: 'Index',
template: 'index',
routes: {
query: '/'
},
static: true,
internal: true
});
}
async.each( services , function( name , complete ) {
var Service = self._services[ name ];
self.services[ name ] = new Service();
if (self.plugins[ name ] && self.plugins[ name ].length) {
self.plugins[ name ].forEach(function(plugin) {
self.services[ name ]._plugins.push( plugin );
['pre', 'post'].forEach(function(prop) {
var option = plugin[ prop ];
if (!option) return;
Object.keys( option ).forEach(function(method) {
self.services[ name ][ prop ]( method , option[ method ] );
});
});
});
}
self.services[ name ].init();
self.services[ name ].attach( self );
self.services[ name ].start( complete );
}, function(err, results) {
self.served = true;
self.emit('ready');
return cb();
});
return self;
};
Maki.prototype.start = function( done ) {
var self = this;
if (!done) var done = new Function();
self.messenger = new self.Messenger();
self.datastore = new self.Datastore( self.config );
self.datastore.connect(function() {
self.queue = new self.Queue( self.config );
if (!self.served) return self.serve( Object.keys( self._services ) , done );
done();
});
};
Maki.prototype.destroy = function( done ) {
var self = this;
if (!done) var done = new Function();
async.each( self.services , function( service , complete ) {
service.destroy( complete );
}, function(err) {
self.datastore.disconnect( done );
});
};
Maki.prototype.bootstrap = function(done) {
var self = this;
var base = process.env.PWD;
var maki = __dirname + '/..';
var main = [
'/views',
'/views/layouts',
'/views/partials'
];
if (!done) {
var done = new Function();
}
main.forEach(function(f) {
if (!fs.existsSync(base+f)) fs.mkdirSync(base+f);
});
Object.keys(self.resources).forEach(function(r) {
var resource = self.resources[r];
var listName = resource.plural.toLowerCase();
var viewName = resource.name.toLowerCase();
var listView = base+'/views/'+listName+'.jade';
var viewView = base+'/views/'+viewName+'.jade';
var reqs = [
'/views/layouts/default.jade',
'/views/partials/navbar.jade',
'/views/partials/flash.jade'
];
reqs.forEach(function(r) {
if (!fs.existsSync(base+r)) fs.copySync(maki+r, base+r);
});
if (!fs.existsSync(listView)) {
var template = fs.readFileSync(maki+'/data/resource-list.jaml')
.toString()
.replace(/{{resource.name}}/gi, resource.name)
.replace(/{{resource.plural}}/gi, resource.plural)
.replace(/{{resource.names.query}}/gi, resource.names.query)
.replace(/{{resource.names.get}}/gi, resource.names.get);
fs.writeFileSync(listView, template);
}
if (!fs.existsSync(viewView)) {
var template = fs.readFileSync(maki+'/data/resource-view.jaml')
.toString()
.replace(/{{resource.name}}/gi, resource.name)
.replace(/{{resource.plural}}/gi, resource.plural)
.replace(/{{resource.names.query}}/gi, resource.names.query)
.replace(/{{resource.names.get}}/gi, resource.names.get);
fs.writeFileSync(viewView, template);
}
});
done();
};
module.exports = Maki;