forked from geddy/geddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·233 lines (216 loc) · 6.9 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
#!/usr/bin/env node
// Dependencies
var geddy = require('../lib/geddy')
, exec = require('child_process').exec
, path = require('path')
, utils = require('utilities')
, parseopts = require('../lib/parseopts');
// Variables
var cwd = process.cwd()
, args = process.argv.slice(2)
, parser
, optsMap
, cmds
, opts
, usage
, cmd
, engineCmd
, modelCmd
, filepath
, die
, jake
, jakeArgs
, jakeProgram
, jakeLoader
, start;
// Usage dialog
usage = [
'Geddy web framework for Node.js'
, ''
, 'Usage:'
, ' geddy [options/commands] [arguments]'
, ''
, 'Options:'
, ' --environment, -e Environment to use'
, ' --port, -p Port to connect to'
, ' --workers, -w Number of worker processes to start (default: 1)'
, ' --debug, -d Sets the log level to output debug messages to'
, ' the console'
, ' --jade, -j When generating views this will create Jade'
, ' templates(Default: EJS)'
, ' --handle, -H When generating views this will create Handlebars'
, ' templates(Default: EJS)'
, ' --mustache, -m When generating views this will create Mustache'
, ' templates(Default: EJS)'
, ' --help, -h Output this usage dialog'
, ' --version, -v Output the version of Geddy that\'s installed'
, ''
, 'Commands:'
, ' app <name> Create a new Geddy application'
, ' resource <name> [attrs] Create a new resource. A resource includes'
, ' a model, controller and route'
, ' scaffold <name> [attrs] Create a new scaffolding. Scaffolding includes'
, ' the views, a model, controller and route'
, ' secret Generate a new application secret in'
, ' `congig/environment`'
, ' controller <name> Generate a new controller including an index view'
, ' and and a route'
, ' model <name> [attrs] Generate a new model'
, ' console Opens a geddy console'
, ''
, 'Examples:'
, ' geddy Start Geddy on localhost:4000 in development mode'
, ' or if the directory isn\'t a Geddy app it\'ll'
, ' this usage dialog'
, ' geddy -p 3000 Start Geddy on port 3000'
, ' geddy -e production Start Geddy in production mode'
, ' geddy -j scaffold user Generate a users scaffolding using Jade templates'
, ' geddy resource user name admin:boolean'
, ' Generate a users resource with the model properties'
, ' name as a string and admin as a boolean'
, ' geddy scaffold user name:string:default'
, ' Generate a users scaffolding user name as the default'
, ' value to display data with'
, ''
].join('\n');
// Options available
optsMap = [
{ full: 'origins', abbr: 'o' }
, { full: 'port', abbr: 'p' }
, { full: 'workers', abbr: ['n', 'w'] }
, { full: 'version', abbr: ['v', 'V'], args: false }
, { full: 'help', abbr: 'h', args: false }
, { full: 'debug', abbr: 'd' }
, { full: 'loglevel', abbr: 'l' }
, { full: 'environment', abbr: 'e' }
, { full: 'spawned', abbr: ['s', 'q', 'Q'] }
, { full: 'jade', abbr: 'j', args: false }
, { full: 'handle', abbr: 'H', args: false }
, { full: 'handlebars', abbr: 'H', args: false }
, { full: 'mustache', abbr: 'm', args: false }
];
// Parse optsMap and generate options and cmd commands
parser = new parseopts.Parser(optsMap);
parser.parse(args);
cmds = parser.cmds;
opts = parser.opts;
// Set handlebars option to handle option
opts.handle = opts.handlebars || opts.handle;
// Exit the process with a message
die = function (str) {
console.log(str);
process.exit();
};
// Start Geddy with options
start = function () {
geddy.config(opts);
geddy.start();
};
if (opts.help) {
die(usage);
}
if (opts.version) {
die(geddy.version);
}
// `geddy app foo` or `geddy resource bar` etc. -- run generators
if (cmds.length) {
// Get templates Jake file
filepath = path.normalize(path.join(__dirname, '..', 'templates', 'Jakefile'));
cmd = '';
// Some commands take only one arg
if (!(cmds[0] == 'jake' ||
cmds[0] == 'secret' ||
cmds[0] == 'db:init' ||
cmds[0] == 'console')
&& !cmds[1]) {
throw new Error(cmds[0] + ' command requires another argument.');
}
// Add engines to command
if (opts.jade) {
engineCmd = ',' + 'jade';
} else if (opts.handle) {
engineCmd = ',' + 'handlebars';
} else if (opts.mustache) {
engineCmd = ',' + 'mustache';
} else engineCmd = '';
// Get the model properties
if (cmds.slice(2).length > 0) {
modelCmd = ',' + cmds.slice(2).join('%');
} else modelCmd = '';
// Add Jake argument based on commands
switch (cmds[0]) {
case 'jake':
cmd = 'jake';
jakeArgs = cmds.slice(1);
break;
case 'console':
// Create DBs
cmd += 'console:start[' + (cmds[1] || 'development') + ']';
break;
case 'db:init':
// Create DBs
cmd += 'db:init';
break;
case 'db:createTable':
// Create DBs
cmd += 'db:createTable[' + cmds[1] + ']';
break;
case 'app':
// Generating application
cmd += 'gen:app[' + cmds[1] + engineCmd + ']';
break;
case 'resource':
// Generating resource
cmd += 'gen:resource[' + cmds[1] + modelCmd + ']';
break;
case 'scaffold':
// Generating application
cmd += 'gen:scaffold[' + cmds[1] + engineCmd + modelCmd + ']';
break;
case 'controller':
// Generating controller
cmd += 'gen:bareController[' + cmds[1] + engineCmd + ']';
break;
case 'model':
// Generating model
cmd += 'gen:model[' + cmds[1] + modelCmd + ']';
break;
case 'secret':
// Generating new app secret
cmd += 'gen:secret';
break;
default:
die(cmds[0] + ' is not a Geddy command.');
}
jake = require('jake');
jakeProgram = jake.program;
jakeLoader = jake.loader;
// Load Geddy's bundled Jakefile
jakeLoader.loadFile(filepath);
if (cmd == 'jake') {
jakeProgram.parseArgs(jakeArgs);
// Load Jakefile and jakelibdir files for app
jakeLoader.loadFile(jakeProgram.opts.jakefile);
jakeLoader.loadDirectory(jakeProgram.opts.jakelibdir);
// Prepend env:init to load Geddy env
jakeProgram.taskNames.unshift('env:init');
}
else {
jakeProgram.init({
quiet: !opts.debug
, trace: true
});
jakeProgram.setTaskNames([cmd]);
}
jakeProgram.run();
}
// Just `geddy` -- start the server
else {
// Search for 'config' directory in parent directories
utils.file.searchParentPath('config', function (err, filePath) {
if (err) {
die(usage);
}
start();
});
}