forked from geddy/geddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·182 lines (167 loc) · 4.12 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
#!/usr/bin/env node
// Load the basic Geddy toolkit
require('../lib/geddy');
var fs = require('fs')
, exec = require('child_process').exec
, path = require('path')
, parseopts = require('../lib/parseopts')
, utils = require('../lib/utils/index')
, App = require('../lib/app.js').App
, pkg = JSON.parse(fs.readFileSync(__dirname + '/../package.json').toString())
, parser
, args = process.argv.slice(2)
, optsReg
, cmds
, opts
, usage
, cmd
, filepath;
geddy.version = pkg.version;
usage = ''
+ 'Geddy web framework for Node.js\n'
+ '*********************************************************************************************\n'
+ 'With no flags, server starts running on port 4000, on http://localhost, in development mode\n'
+ '*********************************************************************************************\n'
+ '{Usage}: geddy [options]\n'
+ '\n'
+ '{Options}:\n'
+ ' -e, --environment Evironment config to use\n'
+ ' -p, --port NUM Port number, defaults to 4000\n'
+ ' -n, --workers NUM Number of worker processes to use, defaults to 2\n'
+ ' -V/v, --version Outputs the version of geddy that you have installed\n'
+ ' -d, --debug Sets the log level to output debug messages to the console\n'
+ ' -h, --help Outputs help information\n'
+ '';
optsReg = [
{ full: 'origins'
, abbr: 'o'
}
, { full: 'port'
, abbr: 'p'
}
, { full: 'workers'
, abbr: 'n'
}
, { full: 'version'
, abbr: 'V'
}
, { full: 'version'
, abbr: 'v'
}
, { full: 'help'
, abbr: 'h'
}
, { full: 'debug'
, abbr: 'd'
}
, { full: 'logLevel'
, abbr: 'l'
}
, { full: 'environment'
, abbr: 'e'
}
, { full: 'spawned'
, abbr: 'Q'
}
];
parser = new parseopts.Parser(optsReg);
parser.parse(args);
cmds = parser.cmds;
opts = parser.opts;
var die = function (str) {
console.log(str);
process.exit();
};
var start = function () {
var cluster
, master
, worker
, m
, w
, app;
// Node 0.6
try {
cluster = require('cluster');
geddy.FD_HACK = false;
master = require('../lib/cluster/master')
worker = require('../lib/cluster/worker');
}
// Node 0.4
catch (e) {
geddy.FD_HACK = true;
master = require('../lib/cluster/hack_master')
worker = require('../lib/cluster/hack_worker');
}
// Master-process
if ((cluster && cluster.isMaster) ||
(geddy.FD_HACK && !opts.spawned)) {
m = new master.Master();
m.start(opts);
}
// Worker-process -- start up an app
else {
w = new worker.Worker();
geddy.worker = w;
w.init(function () {
geddy.mixin(geddy, w);
app = new App();
app.init(function () {
w.startServer();
geddy.mixin(geddy, app);
});
});
}
};
if (typeof opts.help != 'undefined') {
die(usage);
}
else if (typeof opts.version != 'undefined') {
die(geddy.version);
}
else {
// `geddy app foo` or `geddy resource bar` etc. -- run generators
if (cmds.length) {
filepath = path.normalize(path.join(__dirname, '../Jakefile'));
if (process.platform == 'win32') {
filepath = '"' + filepath + '"';
}
cmd = 'jake -t -f ' + filepath + ' ';
if (cmds[0] != 'secret' && !cmds[1]) {
throw new Error(cmds[0] + ' command requires another argument.');
}
switch (cmds[0]) {
case 'app':
cmd += 'gen:app[' + cmds[1] + ']';
break;
case 'resource':
cmd += 'gen:resource[' + cmds[1] + ']';
break;
case 'controller':
cmd += 'gen:bareController[' + cmds[1] + ']';
break;
case 'secret':
cmd += 'gen:secret';
break;
default:
die(cmds[0] + ' is not a Geddy command.');
}
cmd += ' generator=true';
exec(cmd, function (err, stdout, stderr) {
if (err) {
throw err;
}
else {
if (stderr) {
console.log(utils.string.trim(stderr));
}
if (stdout) {
console.log(utils.string.trim(stdout));
}
}
});
}
// Just `geddy` -- start the server
else {
start();
}
}