-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
271 lines (219 loc) · 7.38 KB
/
cli.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
var eve = require('./eve.js'),
nodemon = require('./eve/nodemon'),
phonegap = require('./eve/phonegap'),
mocha = require('./eve/mocha'),
command = Array.prototype.slice.apply(process.argv),
executable = command.shift(),
bin = command.shift();
eve()
.on('error', function(message, soft) {
console.log('\x1b[31m%s\x1b[0m', '[eve] ' + message);
if(!soft) {
this.trigger('complete');
}
})
.on('success', function(message) {
console.log('\x1b[32m%s\x1b[0m', '[eve] ' + message);
})
.on('message', function(message) {
console.log('\x1b[33m%s\x1b[0m', '[eve] ' + message);
})
.on('database-complete', function(config, noDeploy) {
this.trigger('success', 'Database has been added!');
if(!noDeploy) {
this.trigger('message', 'Deploying environments ...');
require('./eve/run/deploy')(this, []);
return;
}
this.trigger('complete');
})
.on('create-complete', function(name, type, noDeploy) {
this.trigger('success', name + ' installation complete!');
if(!noDeploy) {
this.trigger('message', 'Deploying environments ...');
require('./eve/run/deploy')(this, []);
return;
}
this.trigger('complete');
})
.on('generate-complete', function(package, environments) {
this.trigger('success', package + ' was successfully generated!');
this.trigger('message', 'Deploying environments ...');
require('./eve/run/deploy')(this, []);
})
.on('relate-complete', function(schema, environments) {
this.trigger('success', schema.name + ' was successfully generated!');
this.trigger('message', 'Deploying environments ...');
require('./eve/run/deploy')(this, []);
})
.on('remove-complete', function(package) {
this.trigger('success', package + ' was successfully removed!');
this.trigger('message', 'Deploying environments ...');
require('./eve/run/deploy')(this, []);
})
.on('deploy-complete', function() {
this.trigger('success', 'Build files were successfully deployed!');
this.trigger('message', 'Mapping ...');
require('./eve/run/map')(this, []);
})
.on('map-complete', function() {
this.trigger('success', 'Deploy files were successfully mapped!');
this.trigger('complete');
})
.on('watch-init', function(watcher, environments) {
process.stdin.once('data', function(command) {
command = command.toString().trim().split(' ');
if(command[0] !== 'watch') {
this.once('complete', function() {
var command = 'watch ' + environments.join(',');
command = command.split(' ');
this.run(command);
});
}
this.trigger('message', 'Shutting down watcher ...');
for(var i = 0; i < watching.length; i++) {
watching[i].server.emit('quit');
}
watcher.close();
this.run(command);
}.bind(this));
var settings = this.getSettings();
for(var watching = [], config, i = 0; i < environments.length; i++) {
if(settings.environments[environments[i]].type === 'phonegap') {
phonegap(eve, {
projectDir: eve()
.setDeployPath(settings.environments[environments[i]].path)
.getDeployPath()
}, function(environment, error, data) {
watching.push({
environment: environment,
server: data.server });
}.bind(this, environments[i]));
} else if(settings.environments[environments[i]].type === 'server') {
// starts the nodemon
config = settings.environments[environments[i]].nodemon || {
scriptPosition : 1,
script : 'index.js',
args : ['--harmony'],
cwd: settings.environments[environments[i]].path,
ignore: ['node_modules', 'upload'],
ext: 'js json' };
//eventually we should write a better programmatic nodemon and watcher
//but for now it's this...
watching.push({
environment: environments[i],
server: nodemon(eve, config) });
}
}
this.trigger('message', 'watching on ' + environments.join(', '));
})
.on('watch-update', function(event, type, name, source, destination, push) {
//we only care if something has changed
if (event !== 'change' && event !== 'add') {
console.log('\x1b[32m%s\x1b[0m', event + ' - ' + destination);
push(event, source, destination, function(error) {
if (error) {
eve().trigger('error', error);
}
});
return;
}
//we only care if this is a js file
if (this.File(source).getExtension() !== 'js') {
this.trigger('success', event + ' - ' + destination);
push(event, source, destination, function(error) {
if (error) {
eve().trigger('error', error);
}
});
return;
}
var build = this.getBuildPath(),
settings = this.getSettings();
//if this is a test file
if(source.substr((build + '/package').length).split('/')[4] === 'test') {
//do not push just run the test
this.trigger('message', 'Testing ...');
//delete the cache to reload again
delete require.cache[source];
mocha.reset().run(eve, [source]);
return;
}
//we only care if there are tests
if(!(settings.environments[name].test instanceof Array)
|| !settings.environments[name].test.length) {
console.log('\x1b[32m%s\x1b[0m', event + ' - ' + destination);
push(event, source, destination, function(error) {
if (error) {
eve().trigger('error', error);
}
});
return;
}
//okay it is a js file, it is not a test file and we have testing to do
this.trigger('success', event + ' - ' + destination);
//generate a list of test folders
for(var tests = [], i = 0; i < settings.environments[name].test.length; i++) {
//settings.environments[name].test[i] -> sample/sample
tests.push(build + '/package/' + settings.environments[name].test[i] + '/' + name + '/test');
}
//capture the contents of the deploy folder
this
.sync(function(next) {
this.File(destination).getContent(next);
})
//then call the push command
.then(function(error, content, next) {
push(event, source, destination, next.bind(this, content));
})
//then run the mocha tests
.then(function(content, error, next) {
if (error) {
eve().trigger('error', error);
return;
}
this.trigger('message', 'Testing ...');
mocha.run(eve, tests, next.bind(this, content));
})
//then revert if nessisary
.then(function(content, failed, next) {
//if there was a fail and this is not a mocha test
if(failed) {
//revert
this
.trigger('error', 'Reverting ' + destination + ' because of a failed test')
.File(destination).setContent(content.toString('utf8'));
}
});
})
//update map
.on('watch-update', function(event, type, name, source, destination, push) {
//we only care if this is not a server
if(type === 'server') {
return;
}
//we only care if we are creating or unlinking
if(event !== 'add' && event !== 'unlink') {
return;
}
this.trigger('message', 'Mapping ...');
require('./eve/run/map')(this, [name]);
})
//phonegap events
.on('phonegap-start', function() {
console.log('\x1b[35m%s\x1b[0m', '[eve] Phonegap has started ...');
})
.on('phonegap-quit', function() {
console.log('\x1b[35m%s\x1b[0m', '[eve] Phonegap has stopped ...');
})
//nodemon events
.on('nodemon-start', function() {
console.log('\x1b[35m%s\x1b[0m', '[eve] Server has started ...');
})
.on('nodemon-quit', function() {
console.log('\x1b[35m%s\x1b[0m', '[eve] Server has stopped ...');
})
.on('nodemon-restart', function() {
console.log('\x1b[35m%s\x1b[0m', '[eve] Server has restarted ...');
})
.run(command);