-
Notifications
You must be signed in to change notification settings - Fork 3
/
eve.js
412 lines (339 loc) · 8.91 KB
/
eve.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
module.exports = require('edenjs').extend(function() {
/* Require
-------------------------------*/
var separator = require('path').sep;
/* Constants
-------------------------------*/
/* Public Properties
-------------------------------*/
/* Protected Properties
-------------------------------*/
this._deploy = null;
this._settings = null;
/* Private Properties
-------------------------------*/
/* Magic
-------------------------------*/
/* Public Methods
-------------------------------*/
/**
* Adds package to package settings
*
* @param string name
* @param callback
* @return this
*/
this.addPackage = function(name, callback) {
//what is the build path ?
var path = this.getBuildPath() + '/package/' + name;
var settings = this.getSettings();
var build = this.getBuildPath();
this
.sync(function(next) {
this.Folder(path).getFolders(null, false, next);
})
.then(function(error, folders, next) {
if(error) {
callback(error);
return;
}
var environments = [];
for(var i = 0; i < folders.length; i++) {
environments.push(folders[i].getName());
}
//loop through folders
next.thread('environment-loop', 0, environments);
})
.thread('environment-loop', function(i, environments, next) {
if(i < environments.length) {
var file = build + '/config/' + environments[i] + '/packages.json';
this.File(file).getData(function(error, data) {
if(error) {
callback(error, null);
return;
}
next.thread('set-build', i, environments, data);
});
return;
}
//next
next();
})
.thread('set-build', function(i, environments, data, next) {
var file = build + '/config/' + environments[i] + '/packages.json';
if(data.indexOf(name) === -1) {
data.push(name);
}
this.File(file).setData(data, function(error) {
if(error) {
callback(error);
return;
}
var deploy = settings.environments[environments[i]].path;
//if destination starts with a .
if(deploy.indexOf('.') === 0) {
//destination is relative to local
deploy = build + deploy.substr(1);
}
if(settings.environments[environments[i]].type !== 'server') {
deploy += '/application';
}
var file = deploy + '/config/packages.json';
this.File(file).getData(function(error, data) {
if(error) {
callback(error);
return;
}
next.thread('set-deploy', i, environments, data);
});
}.bind(this));
})
.thread('set-deploy', function(i, environments, data, next) {
var deploy = settings.environments[environments[i]].path;
//if destination starts with a .
if(deploy.indexOf('.') === 0) {
//destination is relative to local
deploy = build + deploy.substr(1);
}
if(settings.environments[environments[i]].type !== 'server') {
deploy += '/application';
}
var file = deploy + '/config/packages.json';
if(data.indexOf(name) === -1) {
data.push(name);
}
this.File(file).setData(data, function(error) {
if(error) {
callback(error);
return;
}
next.thread('environment-loop', i + 1, environments);
})
})
.then(function(next) {
callback(null);
});
return this;
};
/**
* Default callback just for processing errors
*
* @return this
*/
this.error = function(error) {
if (error) {
this.trigger('error', error);
}
return this;
};
/**
* Returns eve path
*
* @return string
*/
this.getEvePath = function(root) {
return __dirname;
};
/**
* Returns build path
*
* @return string
*/
this.getBuildPath = function() {
return process.env.PWD || process.cwd();
};
/**
* Returns a database config
*
* @param string
* @return object
*/
this.getDatabase = function(key) {
var settings = this.getSettings();
if(key && settings.databases[key]) {
return settings.databases[key];
}
//find the first default one
for(key in settings.databases) {
if(settings.databases.hasOwnProperty(key)) {
if(settings.databases[key].default) {
return settings.databases[key];
}
}
}
//um return the first one?
for(key in settings.databases) {
return settings.databases[key];
}
};
/**
* Returns the deploy path
*
* @return string
*/
this.getDeployPath = function() {
return this._deploy;
};
/**
* Returns the project settings in build.json
*
* @return object
*/
this.getSettings = function() {
if(this._settings === null
&& this.File(this.getBuildPath() + '/build.json').isFile()) {
this._settings = require(this.getBuildPath() + '/build.json') || null;
}
return this._settings || {
databases: {},
environments: {} };
};
/**
* Normalizes generated fields
*
* @param object schema
* @param bool true if a list of fields vs just one field
* @return object normalized schema
*/
this.normalize = function(field, fields) {
if(fields) {
for(var key in field) {
if(field.hasOwnProperty(key)) {
field[key] = this.normalize(field[key]);
}
}
return field;
}
var normal = { type: field.type || 'string' };
normal.field = field.field || ['text'];
normal.valid = field.valid || [];
normal.label = field.label || '';
normal.holder = field.holder || '';
normal.search = field.search || false;
if(field.type === 'file') {
normal.field = 'file';
normal.multiple = field.multiple || false;
}
if(field.field === false) {
normal.field = false;
} else if(typeof normal.field === 'string') {
normal.field = [normal.field];
}
if(typeof normal.valid === 'string') {
normal.valid = [[normal.valid]];
}
if(typeof field.default !== 'undefined') {
normal.default = field.default;
}
if(normal.default === null || normal.default === 'NULL') {
normal.default = 'NULL'
} else if(normal.type === 'int' && typeof normal.default !== 'undefined') {
normal.default = parseInt(normal.default) || '0';
normal.default += '';
} else if(normal.type === 'float' && typeof normal.default !== 'undefined') {
normal.default = parseFloat(normal.default) || '0.00';
normal.default += '';
} else if(normal.type === 'boolean' && typeof normal.default !== 'undefined') {
normal.default = !!normal.default ? '1': '0';
} else if(normal.type === 'datetime'
&& (normal.default === 'now'
|| normal.default === 'now()'
|| normal.default === 'CURRENT_TIMESTAMP')) {
normal.default = 'CURRENT_TIMESTAMP';
} else if(typeof normal.default === 'string') {
normal.default = "'" + normal.default + "'";
}
var valid = [];
for(var i = 0; i < normal.valid.length; i++) {
if(normal.valid[i] instanceof Array) {
valid.push(normal.valid[i]);
continue;
}
valid.push([normal.valid[i]]);
}
normal.valid = valid;
if(field.options instanceof Array) {
normal.options = [];
for(i = 0; i < field.options.length; i++) {
if(typeof field.options[i] === 'string') {
normal.options.push({
value: field.options[i],
label: field.options[i][0].toUpperCase() + field.options[i].substr(1)
});
continue;
}
normal.options.push(field.options[i]);
}
if(normal.type !== 'file') {
valid.push(['one', normal.options]);
}
}
if(normal.field === false) {
normal.valid = [];
}
return normal;
};
/**
* Fixes path
*
* @param string
* @return string
*/
this.path = function(path) {
return path.replace(/\//g, separator);
};
/**
* Sets the current deploy path
*
* @param string
* @return this
*/
this.setDeployPath = function(path) {
this._deploy = this.path(path);
//if destination starts with a .
if(this._deploy.indexOf('.') === 0) {
//destination is relative to local
this._deploy = this.getBuildPath() + this._deploy.substr(1);
}
return this;
};
/**
* Sets settings into local build.json
*
* @param object
* @param function
* @return this
*/
this.setSettings = function(settings, callback) {
this._settings = settings;
this.File(this.getBuildPath() + '/build.json').setData(settings, callback);
return this;
};
/**
* Runs a command if it is an actual command
*
* @param string
* @return this
*/
this.run = function(command) {
//default command is "eve watch"
action = command.shift() || 'watch';
//just let the files be responsible for the actions if it exists
//first check for the file
var script = __dirname + '/eve/run/' + action + '.js';
//if script is not a file
if(!this.File(script).isFile()) {
//give an error and return
this.trigger('error', 'This is not a valid command');
return;
}
this.trigger(action, command);
//require the file and let it do the rest
require(script)(this, command);
return this;
};
/* Protected Methods
-------------------------------*/
/* Private Methods
-------------------------------*/
}).register('eve').singleton();