-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy patheb
executable file
·382 lines (312 loc) · 7.8 KB
/
eb
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
#!/usr/bin/env node
/**
* ExpressJs MVC Bootstrap
* @Params - cmd - serve
r | script | params
*/
/**
* Framework version.
*/
var version = '0.1.2';
/**
* Require everything in the library folder
*/
require.paths.unshift(__dirname + '/lib');
/**
* Explicit module dependencies
*/
var fs = require('fs'), nodepath = require('path'), exec = require('child_process').exec;
/**
* Paths
**/
var path = fs.realpathSync('.');
var bootstrapPath = __dirname;
/**
* Main Command router
*/
var appLauncher = {
command:'cluster',
server: { port:3000 },
script: { name:'help',
params: []
}
};
for(var i in process.argv) {
// Skip the first two - Node and app.js path
if(i>1) {
processParam(process.argv[i],i);
}
}
runLauncher(appLauncher);
/**
* Run the launcher
* @param appLauncher
*/
function runLauncher(appLauncher) {
// Always use current directory?
console.log('\r\n\x1b[36mLaunching bootstrap from: \x1b[0m ' + path);
console.log('\x1b[36mScript directory: \x1b[0m ' + bootstrapPath + '/scripts');
// Check if this is a bootstrap application?
if(isLibrary()) {
console.log('\x1b[1mDo not run this from the bootstrap library folder, please run from an empty directory or a bootstrapped app, or please run "npm install"\x1b[0m\r\n');
return;
}
// Check if this is a bootstrap application?
if(!isBootstrap() && appLauncher.command != 'create-app') {
console.log('\x1b[1mApplication not bootstrapped - you must run:\x1b[0m eb create-app\r\n');
return;
}
switch(appLauncher.command) {
case 'test':
runTests(appLauncher.script);
break;
case 'cluster':
runCluster(appLauncher.server.port);
break;
case 'server':
runServer(appLauncher.server.port);
break;
case 'script':
runScript(appLauncher.script);
break;
case 'create-app':
createApplication(path);
break;
default:
appLauncher.command = 'script';
runScript(appLauncher.script);
}
}
/**
* Check if express app.js exists
**/
function isLibrary() {
return nodepath.existsSync(path + '/eb');
}
/**
* Check if .bootstrap exists
**/
function isBootstrap() {
return nodepath.existsSync(path + '/.eb-status');
}
/**
* Run a script
* @param appLauncher
* Runs by default from path where bootstrap runs via __dirname.
*/
function runScript(scriptLauncher) {
var script = require(bootstrapPath + '/scripts/'+ scriptLauncher.name);
script.execute(scriptLauncher.params,path);
}
/**
* Process params into array to enable launch
* @param param
* @param params
*/
function processParam(param,depth) {
var paramArray = param.split("=");
// Run command - must always come after the app
if(i == 2) {
appLauncher.command = param;
}
// Server.port
if(paramArray[0] == "server.port" && paramArray[1] != undefined) {
appLauncher.server.port = paramArray[1];
}
//
if((appLauncher.command == "script" || appLauncher.command == "test") && i == 3) {
appLauncher.script.name = param;
}
// Script params
if(appLauncher.command == 'script' && i > 3) {
appLauncher.script.params.push(param);
}
}
/**
* Run expresso tests
*/
function runTests(appLauncher) {
var test = appLauncher.name ? appLauncher.name : 'all';
if(test == 'help') { // This is the default - ugly I know!
test = ['unit','integration','functional'];
} else {
test = [test];
}
test.forEach(function (item, index) {
exec('expresso -I . -s tests/' + item + '/* ', { timeout: 60000, cwd:path }, function (error, stdout, stderr) {
console.log("Finished test: " + item);
console.log(stdout);
console.log(stderr);
});
});
}
/**
* Launch a server
*/
function runServer(port) {
// Ensure we run in the local folder of the application
process.chdir(path);
var app = require(path + '/app').boot();
app.listen(port);
console.log('\r\n\x1b[36mApplication started on port:\x1b[0m ' + port);
}
/**
* Launch a cluster
*/
function runCluster(port) {
// Ensure we run in the local folder of the application
process.chdir(path);
require(path + '/app-cluster').boot(port,path);
console.log('\r\n\x1b[36mLaunching cluster mode on port: \x1b[0m' + port);
}
/**
* Application Creation - Borrowed from Express scripts
**/
/**
* Create application at the given directory `path`.
*
* @param {String} path
*/
function createApplicationAt(path) {
mkdir(path + '/models',function() {
// Disabled as no default models
// copy(bootstrapPath + '/models/*',path + '/models');
});
mkdir(path + '/controllers',function() {
copy(bootstrapPath + '/controllers/*',path + '/controllers');
});
mkdir(path + '/conf',function() {
copy(bootstrapPath + '/conf/*',path + '/conf');
});
mkdir(path + '/utils',function() {
copy(bootstrapPath + '/utils/*',path + '/utils');
});
mkdir(path + '/views',function() {
copy(bootstrapPath + '/views/*',path + '/views');
});
mkdir(path + '/public',function() {
copy(bootstrapPath + '/public/*',path + '/public');
});
mkdir(path + '/lib',function() {
copy(bootstrapPath + '/lib/*',path + '/lib');
});
mkdir(path + '/tests',function() {
mkdir(path + '/tests/unit',function() {
copy(bootstrapPath + '/tests/unit/*',path + '/tests/unit');
});
mkdir(path + '/tests/integration',function() {
copy(bootstrapPath + '/tests/integration/*',path + '/tests/integration');
});
mkdir(path + '/tests/functional',function() {
copy(bootstrapPath + '/tests/functional/*',path + '/tests/functional');
});
});
mkdir(path + '/logs');
mkdir(path + '/pids');
copy(bootstrapPath + '/app-cluster.js',path + '/');
copy(bootstrapPath + '/app.js',path + '/');
write(path + '/.eb-status','Created @ ' + new Date());
}
function createApplication(path) {
emptyDirectory(path, function(empty){
if (empty) {
createApplicationAt(path);
} else {
confirm('This will over-write the existing application, continue? ', function(ok){
if (ok) {
process.stdin.destroy();
createApplicationAt(path);
} else {
abort('aborting');
}
});
}
});
};
/**
* Check if the given directory `path` is empty.
*
* @param {String} path
* @param {Function} fn
*/
function emptyDirectory(path, fn) {
fs.readdir(path, function(err, files){
if (err && 'ENOENT' != err.code) throw err;
fn(!files || !files.length);
});
}
/**
* echo str > path.
*
* @param {String} path
* @param {String} str
*/
function write(path, str) {
fs.writeFile(path, str);
console.log(' \x1b[36mcreate\x1b[0m : ' + path);
}
/**
* Prompt confirmation with the given `msg`.
*
* @param {String} msg
* @param {Function} fn
*/
function confirm(msg, fn) {
prompt(msg, function(val){
fn(/^ *y(es)?/i.test(val));
});
}
/**
* Prompt input with the given `msg` and callback `fn`.
*
* @param {String} msg
* @param {Function} fn
*/
function prompt(msg, fn) {
// prompt
if (' ' == msg[msg.length - 1]) {
process.stdout.write(msg);
} else {
console.log(msg);
}
// stdin
process.stdin.setEncoding('ascii');
process.stdin.once('data', function(data){
fn(data);
}).resume();
}
/**
* Mkdir -p.
*
* @param {String} path
* @param {Function} fn
*/
function mkdir(path, fn) {
exec('mkdir -p ' + path, function(err){
if (err) throw err;
console.log(' \x1b[36mcreate\x1b[0m : ' + path);
fn && fn();
});
}
/**
* cp -r
*
* @param {String} path
* @param {Function} fn
*/
function copy(from, to, fn) {
exec('cp -R ' + from + ' ' + to, function(err){
if (err) throw err;
console.log(' \x1b[36mCopied\x1b[0m : ' + from + ' to ' + to);
fn && fn();
});
}
/**
* Exit with the given `str`.
*
* @param {String} str
*/
function abort(str) {
console.error(str);
process.exit(1);
}