Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 1005 lines (846 sloc) 29.136 kb
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
1 // Load the basic Geddy toolkit
2 require('../lib/geddy')
3
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
4 // Dependencies
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
5 var cwd = process.cwd()
6 , fs = require('fs')
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
7 , path = require('path')
d278a7e Get example app working with external model lib
mde authored
8 , utils = require('../lib/utils')
7ae4e05 Added generator for auth
mde authored
9 , Adapter = require('../lib/template/adapters')
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
10 , geddyPassport = 'geddy-passport@0.0.x'
7ae4e05 Added generator for auth
mde authored
11 , getRouterPath
3900a2b @larzconwell Closes #215 Added a geddy routes command that takes an optional route
larzconwell authored
12 , getRoutes
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
13 , addRoute
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
14 , mixinJSONData;
7ae4e05 Added generator for auth
mde authored
15
16 getRouterPath = function () {
17 var beginPath
18 , jsRouter = path.normalize('config/router.js')
19 , coffeeRouter = path.normalize('config/router.coffee')
20 , routerPath;
21 // Check if the router file exists
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
22 beginPath = path.join(cwd, 'config');
7ae4e05 Added generator for auth
mde authored
23 utils.file.searchParentPath(jsRouter, function (err) {
24 if (err) {
25 var jsErr = err;
26 // If jsEnvironment wasn't found, try finding coffee variant
27 utils.file.searchParentPath(coffeeRouter, beginPath, function (err) {
28 if (err) {
29 throw jsErr;
30 } else {
31 routerPath = coffeeRouter;
32 }
33 });
34 } else {
35 routerPath = jsRouter;
36 }
37 });
38 return routerPath;
39 };
40
3900a2b @larzconwell Closes #215 Added a geddy routes command that takes an optional route
larzconwell authored
41 getRoutes = function (resource) {
42 if (!resource) {
43 return geddy.router.toString();
44 }
45
46 var rts = []
47 , _rt
48 , i;
49
50 // If a full route name was given add it to the list(e,g,. users.index)
51 if (resource.match(/.+\..+/)) {
52 var res = resource.split('.')
53 , cont = res[0]
54 , action = res[1];
55
56 for (i in geddy.router.routes) {
57 _rt = geddy.router.routes[i];
58
59 if (_rt.params.controller.match(cont) &&
60 _rt.params.action.match(action)) {
61 rts.push(_rt);
62 }
63 }
64 }
65 else {
66 for (i in geddy.router.routes) {
67 _rt = geddy.router.routes[i];
68
69 if (_rt.params.controller.match(resource) ||
70 _rt.params.action.match(resource)) {
71 rts.push(_rt);
72 }
73 }
74 }
75
76 return rts.map(function (rt) {
77 return rt.toString();
78 }).join('\n');
79 };
80
7ae4e05 Added generator for auth
mde authored
81 addRoute = function (routerPath, newRoute) {
82 var text = fs.readFileSync(routerPath, 'utf8')
83 , routerArr;
84 // Don't add the same route over and over
85 if (text.indexOf(newRoute) == -1) {
86 // Add the new resource route just above the export
87 routerArr = text.split('exports.router');
88 routerArr[0] += newRoute + '\n';
89
90 text = routerArr.join('exports.router');
91 fs.writeFileSync(routerPath, text, 'utf8');
92 return true;
93 }
94 else {
95 return false;
96 }
97 };
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
98
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
99 mixinJSONData = function (file, obj) {
100 var data = obj || {};
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
101
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
102 if (utils.file.existsSync(file)) {
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
103 try {
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
104 var content = JSON.parse(fs.readFileSync(file, 'utf8'));
105 utils.object.merge(content, data);
106 fs.writeFileSync(file, JSON.stringify(content, null, 2));
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
107 }
108 catch (e) {
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
109 throw new Error("Could not parse " + file);
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
110 }
111 }
112 else {
3620c04 @MiguelMadero Fixes typo
MiguelMadero authored
113 console.log("There is no file " + file + " to add data to.");
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
114 }
115 };
116
f27b1a7 @MiguelMadero Adds tests for getTemplatePath and minor refactor
MiguelMadero authored
117
d278a7e Get example app working with external model lib
mde authored
118 namespace('env', function () {
3e0c059 Added basic console feature
mde authored
119 task('init', function (environment) {
d278a7e Get example app working with external model lib
mde authored
120 jake.addListener('complete', function (e) {
121 jake.Task['env:cleanup'].invoke();
122 });
9e1fe04 Got generators working.
mde authored
123
3e0c059 Added basic console feature
mde authored
124 geddy.config = require('../lib/config').readConfig({
125 environment: environment || 'development'
126 });
9e1fe04 Got generators working.
mde authored
127 geddy.model = require('model');
128
5da7b94 Cleaned up env init process a little more
mde authored
129 require('../lib/init').init(geddy, function () {
9e1fe04 Got generators working.
mde authored
130 complete();
131 });
132 }, {async: true});
d278a7e Get example app working with external model lib
mde authored
133
134 task('cleanup', function () {
135 // Disconnect all the adapters
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS and...
larzconwell authored
136 var adapters = geddy.model.loadedAdapters
137 , adapter;
138
d278a7e Get example app working with external model lib
mde authored
139 for (var p in adapters) {
48fb464 Fixed DB adapter connect/disconnect
mde authored
140 adapter = adapters[p];
141 if (typeof adapter.disconnect == 'function') {
142 adapter.disconnect();
143 }
d278a7e Get example app working with external model lib
mde authored
144 }
145 });
146
3e0c059 Added basic console feature
mde authored
147 });
148
149 namespace('console', function () {
150 task('start', ['env:init'], {async: true}, function (environment) {
151 var env = environment || 'development'
152 , t = jake.Task['env:init'];
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS and...
larzconwell authored
153
3e0c059 Added basic console feature
mde authored
154 t.addListener('complete', function () {
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocomple...
larzconwell authored
155 var repl = require('repl')
156 , rl;
157
158 rl = repl.start({
159 prompt: '>>> '
160 , input: process.stdin
161 , output: process.stdout
3e0c059 Added basic console feature
mde authored
162 });
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS and...
larzconwell authored
163
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocomple...
larzconwell authored
164 rl.on('close', function () {
165 console.log('Exiting...');
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
166 return complete();
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocomple...
larzconwell authored
167 })
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
168
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocomple...
larzconwell authored
169 rl.context.capture = function (err, data) {
170 return rl.context.results = {
171 err: err
172 , data: data
173 };
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
174 };
175
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocomple...
larzconwell authored
176 rl.context.echo = function (err, data) {
177 rl.context.capture(err, data);
178 if (err) {
179 console.log('Error: ', err);
180 }
181
182 if (data) {
183 if (data.length) {
184 for (var i in data) {
185 if (data[i] && data[i].toData) {
186 console.log(data[i].toData());
187 } else {
188 console.log(data[i]);
189 }
190 }
191 }
192 else {
193 if (data && data.toData) {
194 console.log(data.toData());
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
195 } else {
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocomple...
larzconwell authored
196 console.log(data);
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
197 }
198 }
199 } else {
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocomple...
larzconwell authored
200 console.log('No data');
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
201 }
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocomple...
larzconwell authored
202 };
3900a2b @larzconwell Closes #215 Added a geddy routes command that takes an optional route
larzconwell authored
203
204 rl.context.routes = function (resource) {
205 console.log(getRoutes(resource));
206 };
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocomple...
larzconwell authored
207 });
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
208
3e0c059 Added basic console feature
mde authored
209 t.invoke(env);
210 });
211 });
d278a7e Get example app working with external model lib
mde authored
212
3900a2b @larzconwell Closes #215 Added a geddy routes command that takes an optional route
larzconwell authored
213 namespace('routes', function () {
214
215 task('show', ['env:init'], {async: true}, function (resource) {
216 console.log('Showing route results for "' + resource + '"');
217 console.log(getRoutes(resource));
218 });
219
220 });
221
d278a7e Get example app working with external model lib
mde authored
222 namespace('db', function () {
48fb464 Fixed DB adapter connect/disconnect
mde authored
223 task('createTable', ['env:init'], function (name) {
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
224
9e1fe04 Got generators working.
mde authored
225 var modelName
48fb464 Fixed DB adapter connect/disconnect
mde authored
226 , createTable
d278a7e Get example app working with external model lib
mde authored
227 , adapters
9e1fe04 Got generators working.
mde authored
228 , adapter;
229
230 if (typeof name == 'string') {
c7f64c5 Can't use commas with new embeddable Jake
mde authored
231 if (name.indexOf('%') > -1) {
232 modelNames = name.split('%');
9e1fe04 Got generators working.
mde authored
233 }
234 else {
235 modelNames = [name];
236 }
237 }
238 else {
239 modelNames = name;
240 }
241
d278a7e Get example app working with external model lib
mde authored
242 createTable = function () {
48fb464 Fixed DB adapter connect/disconnect
mde authored
243 if ((m = modelNames.shift())) {
9e1fe04 Got generators working.
mde authored
244
245 // Make sure this is a correct model-name
246 m = utils.string.getInflections(m).constructor.singular;
247 if (!geddy.model[m]) {
248 throw new Error(m + ' is not a known model.');
249 }
250
d278a7e Get example app working with external model lib
mde authored
251 adapter = geddy.model.adapters[m];
9e1fe04 Got generators working.
mde authored
252 if (adapter) {
253 console.log('Creating table for ' + m);
254 adapter.createTable(m, function (err, data) {
255 if (err) { throw err }
256 createTable();
257 });
258 }
259 else {
d278a7e Get example app working with external model lib
mde authored
260 createTable();
9e1fe04 Got generators working.
mde authored
261 }
d278a7e Get example app working with external model lib
mde authored
262 }
263 else {
264 complete();
265 }
266 };
b13d0e0 Defer table-creation until deferred association-creation
mde authored
267 // Defer until associations are set up
268 setTimeout(function () {
269 createTable();
270 }, 0);
48fb464 Fixed DB adapter connect/disconnect
mde authored
271 }, {async: true});
272
d192f14 'retrofit' task should just be init for SQL DBs
mde authored
273 task('init', ['env:init'], function () {
48fb464 Fixed DB adapter connect/disconnect
mde authored
274 var modelNames = Object.keys(geddy.model.descriptionRegistry)
275 , createTask = jake.Task['db:createTable'];
276 createTask.once('complete', function () {
277 complete();
278 });
279 createTask.invoke(modelNames);
d278a7e Get example app working with external model lib
mde authored
280 }, {async: true});
281
282 });
283
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
284 namespace('gen', function () {
285
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
286 var _writeTemplate = function (name, filename, dirname, opts) {
287 var options = opts || {}
288 , names = utils.string.getInflections(name)
6934941 @MiguelMadero Fixes bug with paths, causing a problem when using the default templates
MiguelMadero authored
289 , text = fs.readFileSync(path.join(filename + '.ejs'), 'utf8').toString()
cf999ea @larzconwell Deleted bare controller template
larzconwell authored
290 , bare = options.bare || false // Default to full controller
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS and...
larzconwell authored
291 , adapter
292 , templContent
9ec7c3a @MiguelMadero Changed the generator to create the directory in case it doesn't exist. ...
MiguelMadero authored
293 , fileDir
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
294 , filePath;
295
296 // Render with the right model name
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS and...
larzconwell authored
297 adapter = new Adapter({engine: 'ejs', template: text});
298 templContent = adapter.render({names: names, bare: bare, properties: options.properties});
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
299
300 // Write file
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
301 fileDir = dirname;
fc70402 @MiguelMadero Removed usage of fs.existsSync for compatibility with node 0.6 and chang...
MiguelMadero authored
302 if (!utils.file.existsSync(fileDir)) {
303 fs.mkdirSync(fileDir);
304 }
9ec7c3a @MiguelMadero Changed the generator to create the directory in case it doesn't exist. ...
MiguelMadero authored
305
306 filePath = path.join(fileDir, names.filename[options.inflection] + '.js');
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS and...
larzconwell authored
307 fs.writeFileSync(filePath, templContent, 'utf8');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
308
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
309 console.log('[Added] ' + filePath);
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
310 };
311
edf26c7 Formatting
mde authored
312 var _formatModelProperties = function (properties) {
515a929 @larzconwell Closes #259, Fix error where no properties at all would throw when compi...
larzconwell authored
313 var obj = {default: {name: '', type: ''}};
9e1fe04 Got generators working.
mde authored
314 if (!properties) {
515a929 @larzconwell Closes #259, Fix error where no properties at all would throw when compi...
larzconwell authored
315 return obj;
9e1fe04 Got generators working.
mde authored
316 }
515a929 @larzconwell Closes #259, Fix error where no properties at all would throw when compi...
larzconwell authored
317 obj['default'].name = 'id';
89831d2 @larzconwell Default to string ID type instead of int for generators.
larzconwell authored
318 obj['default'].type = 'string';
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
319
d3f4d44 Spaces aren't legal inside passed Jake params.
mde authored
320 var itemsArr = properties.split('%')
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
321 , name
322 , type
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
323 , args
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
324 , i
325 , value;
326
26600c5 @larzconwell Just made a few loop optimizations for Array loops
larzconwell authored
327 i = -1;
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
328 while (++i < itemsArr.length) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
329 value = itemsArr[i];
dd3a65b @larzconwell Don't allow nodel property names to be CamelCased
larzconwell authored
330 name = utils.string.camelize(value.replace(/:.*/g, ''));
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
331 type = value.replace(/[a-zA-Z]*:?/, '');
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
332 args = type.replace(/[a-zA-Z]*:?/, '');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
333
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
334 // Take off any args on the type
335 type = type.replace(/:.*/g, '');
336
9e1fe04 Got generators working.
mde authored
337 // Defaults and aliases
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
338 if (!type) {
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
339 type = 'string';
9e2a69d @larzconwell If text attribute is given it switches to string, also took out placehol...
larzconwell authored
340 }
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the def...
larzconwell authored
341 if (args === 'def') {
342 args = 'default';
343 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
344
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
345 switch (type) {
f835d2f @larzconwell Move type checks for generatirs into a switch statement so can be easily...
larzconwell authored
346 case 'integer':
347 type = 'int';
348 break;
349 case 'bool':
350 type = 'boolean';
351 break;
352 case 'default':
353 case 'def':
354 type = 'string';
355 args = 'default';
356 break;
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
357 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
358
bc02170 @larzconwell Closes #230, Don't manually add the ID attribute into the model property...
larzconwell authored
359 // Manage properties that deal with changing default properties
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the def...
larzconwell authored
360 if (args === 'default') {
bc02170 @larzconwell Closes #230, Don't manually add the ID attribute into the model property...
larzconwell authored
361 // Reset old default property to it's own property, only if it's not
362 // already the default
363 if (name !== obj['default'].name) {
364 // If the new default item already exists then delete it
365 if (obj[name]) {
366 delete obj[name];
367 }
368
369 obj[obj['default'].name] = obj[obj['default'].name] || obj['default'];
370 }
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the def...
larzconwell authored
371
372 // Add new default property
373 obj['default'] = {name: name, type: type};
374 continue;
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
375 }
376
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the def...
larzconwell authored
377 // If ID property is given and it matches the default
378 // then rewrite the default with the new ID property
379 if (name === 'id' && obj['default'].name === 'id') {
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
380 obj['default'] = {name: name, type: type};
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the def...
larzconwell authored
381 continue;
9e1fe04 Got generators working.
mde authored
382 }
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the def...
larzconwell authored
383
384 // If the name is name or title then set them to default, otherwise add
385 // the property normally
386 if (name === 'name' || name === 'title') {
387 // Reset old default to it's own property
388 obj[obj['default'].name] = obj[obj['default'].name] || obj['default'];
389
390 // Add new default property
391 obj['default'] = {name: name, type: type};
392 } else {
393 obj[name] = {name: name, type: type};
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
394 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
395 }
396
9925793 @larzconwell Added default option to model property arguments as @TechWraith suggeste...
larzconwell authored
397 return obj;
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
398 };
399
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
400 // Creates a new Geddy app scaffold
4ae812a @MiguelMadero Changed the order of commands since templatesPath is always required I l...
MiguelMadero authored
401 task('app', function (name, templatesPath, engine, realtime) {
5bafa03 @MiguelMadero Moves getTemplatePath function to its file and call this from the cli to...
MiguelMadero authored
402
403 var mkdirs
404 , cps
405 , text
406 , adapter;
407 if (!name) {
408 throw new Error('No app name specified.');
409 }
410 if (!engine || engine == 'default') {
411 engine = 'ejs';
412 }
413 if (realtime == 'default') {
414 realtime = false;
415 }
416
417 templatesPath = path.join(templatesPath, "base");
418
419 mkdirs = [
420 ''
421 , 'config'
422 , 'app/models'
423 , 'app/controllers'
424 , 'app/helpers'
425 , 'lib'
426 , 'log'
427 , 'node_modules'
428 , 'test'
429 ];
430 cps = [
431 (realtime) ? ['realtime/views/' + engine, 'app/views'] : ['views/' + engine, 'app/views']
432 , ['public', '']
433 , ['config', '']
434 , ['app/controllers', 'app']
435 , ['gitignore.txt', '.gitignore']
436 ];
437
438 if (realtime) {
439 cps.push(['realtime/environment.js', 'config']);
440 }
441
442 mkdirs.forEach(function (dir) {
443 jake.mkdirP(path.join(name, dir));
444 });
445 cps.forEach(function (cp) {
446 jake.cpR(path.join(templatesPath, cp[0]), path.join(name, cp[1]), {silent: true});
447 });
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
448
5bafa03 @MiguelMadero Moves getTemplatePath function to its file and call this from the cli to...
MiguelMadero authored
449 // one offs
450 if (realtime) {
451 jake.cpR(path.join(__dirname, '..', 'node_modules', 'socket.io' ), path.join(name, 'node_modules'), {silent: true});
452 }
eb9cff8 @MiguelMadero Uses npm root to get to the node_modules folder needed by getTemplatesPa...
MiguelMadero authored
453
5bafa03 @MiguelMadero Moves getTemplatePath function to its file and call this from the cli to...
MiguelMadero authored
454 // Compile Jakefile
455 text = fs.readFileSync(path.join(templatesPath, 'Jakefile.ejs'), 'utf8').toString();
456 adapter = new Adapter({engine: 'ejs', template: text});
457 fs.writeFileSync(path.join(name, 'Jakefile'), adapter.render({appName: name}), 'utf8');
eb9cff8 @MiguelMadero Uses npm root to get to the node_modules folder needed by getTemplatesPa...
MiguelMadero authored
458
5bafa03 @MiguelMadero Moves getTemplatePath function to its file and call this from the cli to...
MiguelMadero authored
459 // Compile package.json
460 text = fs.readFileSync(path.join(templatesPath, 'package.json.ejs'), 'utf8').toString();
461 adapter = new Adapter({engine: 'ejs', template: text});
462 fs.writeFileSync(path.join(name, 'package.json'), adapter.render({appName: name}), 'utf8');
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
463
5bafa03 @MiguelMadero Moves getTemplatePath function to its file and call this from the cli to...
MiguelMadero authored
464 // Add engine to package.json if it's not EJS
465 if (engine !== 'ejs') {
466 // Change to handlebars as we use it behind the scenes
467 if (engine === 'mustache') {
468 engine = 'handlebars';
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
469 }
5bafa03 @MiguelMadero Moves getTemplatePath function to its file and call this from the cli to...
MiguelMadero authored
470 var data = {dependencies: {}};
471 data.dependencies[engine] = "*";
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
472
5bafa03 @MiguelMadero Moves getTemplatePath function to its file and call this from the cli to...
MiguelMadero authored
473 mixinJSONData(path.join(name, 'package.json'), data);
474 }
475
476 console.log('Created app ' + name + '.');
477 });
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
478
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
479 // Creates a resource with a model, controller and a resource route
edf26c7 Formatting
mde authored
480 task('resource', function (name, modelProperties) {
9e1fe04 Got generators working.
mde authored
481 var names
482 , modelTask = jake.Task['gen:model'];
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
483
9e1fe04 Got generators working.
mde authored
484 if (!name) {
485 throw new Error('No resource name specified.');
486 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
487
9e1fe04 Got generators working.
mde authored
488 modelTask.on('complete', function () {
489 jake.Task['gen:controller'].invoke(name);
490 jake.Task['gen:route'].invoke(name);
491 names = utils.string.getInflections(name);
492 // Create views folder but not actions
493 jake.mkdirP(path.join('app', 'views', names.filename.plural));
494 console.log('[Added] ' + names.filename.plural + ' view directory');
495 complete();
496 });
497 modelTask.invoke(name, modelProperties);
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
498
9e1fe04 Got generators working.
mde authored
499 }, {async: true});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
500
501 // Creates a full scaffold with views, a model, controller and a resource route
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
502 task('scaffold', function (name, realtime, engine, modelProperties) {
9e1fe04 Got generators working.
mde authored
503 var modelTask = jake.Task['gen:model'];
504
505 if (!name) {
506 throw new Error('No scaffold name specified.');
507 }
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
508 if (!engine || engine == 'default') {
9e1fe04 Got generators working.
mde authored
509 engine = 'ejs';
510 }
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
511 if (!realtime || realtime == 'default') {
512 realtime = false;
513 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
514
9e1fe04 Got generators working.
mde authored
515 modelTask.on('complete', function () {
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
516 jake.Task['gen:test'].invoke(name,
517 {properties: modelProperties});
9e1fe04 Got generators working.
mde authored
518 jake.Task['gen:controllerScaffold'].invoke(name,
519 {properties: modelProperties});
520 jake.Task['gen:route'].invoke(name);
521 jake.Task['gen:viewsScaffold'].invoke(name,
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
522 {engine: engine, properties: modelProperties, realtime: realtime});
9e1fe04 Got generators working.
mde authored
523 complete();
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
524 });
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
525 modelTask.invoke(name, modelProperties, 'scaffold');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
526
9e1fe04 Got generators working.
mde authored
527 }, {async: true});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
528
4ae812a @MiguelMadero Changed the order of commands since templatesPath is always required I l...
MiguelMadero authored
529 task('model', function (name, templatesPath, properties, modelPath) {
9e1fe04 Got generators working.
mde authored
530 var createTableTask;
531 if (!name) {
532 throw new Error('No model name specified.');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
533 }
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the defaul...
larzconwell authored
534 if (!modelPath) {
535 modelPath = 'resource';
536 }
537 modelPath += '/model';
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
538
5bafa03 @MiguelMadero Moves getTemplatePath function to its file and call this from the cli to...
MiguelMadero authored
539 modelPath = path.join(templatesPath, modelPath);
540 _writeTemplate(name, modelPath, path.join('app', 'models'), {
541 inflection: 'singular'
542 , properties: _formatModelProperties(properties)
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
543 });
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
544
5bafa03 @MiguelMadero Moves getTemplatePath function to its file and call this from the cli to...
MiguelMadero authored
545 // Try to create a table -- should be a no-op if an
546 // appropriate DB adapter can't be found
547 createTableTask = jake.Task['db:createTable'];
548 createTableTask.on('complete', function () {
549 complete();
550 });
551 createTableTask.invoke(name);
552
9e1fe04 Got generators working.
mde authored
553 }, {async: true});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
554
4ae812a @MiguelMadero Changed the order of commands since templatesPath is always required I l...
MiguelMadero authored
555 task('controller', function (name, templatesPath) {
9e1fe04 Got generators working.
mde authored
556 if (!name) {
557 throw new Error('No controller name specified.');
558 }
559
e7a7bda @larzconwell Remove the require for child_process since we don't use it anymore
larzconwell authored
560
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
561 _writeTemplate(name, 'resource/controller', path.join('app', 'controllers'),
9e1fe04 Got generators working.
mde authored
562 {inflection: 'plural', bare: false});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
563 });
564
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
565 task('test', function (name) {
566 if (!name) {
567 throw new Error('No test name specified.');
568 }
569
570 _writeTemplate(name, 'resource/test', 'test',
571 {inflection: 'singular'});
572 });
573
9e1fe04 Got generators working.
mde authored
574 task('controllerScaffold', function (name, options) {
575 if (!name) {
576 throw new Error('No controller name specified.');
577 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
578 options = options || {};
579
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
580 _writeTemplate(name, 'scaffold/controller', path.join('app', 'controllers'), {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
581 inflection: 'plural'
582 , bare: false
583 , properties: _formatModelProperties(options.properties)
584 });
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
585 });
586
4ae812a @MiguelMadero Changed the order of commands since templatesPath is always required I l...
MiguelMadero authored
587 task('bareController', function (name, templatesPath, engine) {
9e1fe04 Got generators working.
mde authored
588 if (!name) {
589 throw new Error('No controller name specified.');
590 }
e7a7bda @larzconwell Remove the require for child_process since we don't use it anymore
larzconwell authored
591 if (!engine || engine == 'default') {
9e1fe04 Got generators working.
mde authored
592 engine = 'ejs';
593 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
594
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
595 _writeTemplate(name, 'resource/controller', path.join('app', 'controllers'),
596 {inflection: 'plural', bare: true});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
597 jake.Task['gen:route'].invoke(name, {bare: true});
598 jake.Task['gen:views'].invoke(name, {bare: true, engine: engine});
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
599 });
600
9e1fe04 Got generators working.
mde authored
601 task('route', function (name, options) {
602 if (!name) {
603 throw new Error('No route name specified.');
604 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
605 options = options || {};
606
d92f157 Get generators working
mde authored
607 var names = utils.string.getInflections(name)
7ae4e05 Added generator for auth
mde authored
608 , routerPath = getRouterPath()
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
609 , routeType = options.bare ? 'Bare' : 'Resource'
7ae4e05 Added generator for auth
mde authored
610 , newRoute;
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
611
6967867 Misc. fixes to get generators working
mde authored
612 if (routerPath) {
613 if (routerPath.match('.coffee')) {
614 if (options.bare) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
615 newRoute = 'router.match(\'/' + names.filename.plural +
616 '\').to controller: \'' + names.constructor.plural +
617 '\', action: \'index\'';
baa8cdf @larzconwell Updated /site to reflect new features related to generators and template...
larzconwell authored
618 } else {
619 newRoute = 'router.resource \'' + names.filename.plural + '\'';
620 }
6967867 Misc. fixes to get generators working
mde authored
621 } else if (routerPath.match('.js')) {
622 if (options.bare) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
623 newRoute = 'router.match(\'/' + names.filename.plural +
624 '\').to({controller: \'' + names.constructor.plural +
625 '\', action: \'index\'});';
baa8cdf @larzconwell Updated /site to reflect new features related to generators and template...
larzconwell authored
626 } else {
627 newRoute = 'router.resource(\'' + names.filename.plural + '\');';
628 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
629 }
630
7ae4e05 Added generator for auth
mde authored
631 if (addRoute(routerPath, newRoute)) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
632 console.log('[Added] ' + routeType + ' ' + names.filename.plural +
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
633 ' route added to ' + routerPath);
634 }
635 else {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
636 console.log(routeType + ' ' + names.filename.plural + ' route already defined in ' +
637 routerPath);
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
638 }
7ae4e05 Added generator for auth
mde authored
639 }
640 else {
baa8cdf @larzconwell Updated /site to reflect new features related to generators and template...
larzconwell authored
641 console.log('There is no router file to add routes too');
642 }
7ae4e05 Added generator for auth
mde authored
643
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
644 });
645
9e1fe04 Got generators working.
mde authored
646 task('views', function (name, options) {
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
647 if (!name) {
648 throw new Error('No view name specified.');
649 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
650
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
651 options = options || {};
652
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
653 // Option defaults
654 options.engine = options.engine || 'ejs';
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
655
d92f157 Get generators working
mde authored
656 var names = utils.string.getInflections(name)
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
657 , engine = options.engine
658 , appViewDir = path.join('app', 'views', names.filename.plural)
659 , templateViewDir = path.join(__dirname, 'views', engine)
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
660 , cmds = []
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
661 , ext = '.html'
662 , appLayoutPath
663 , actions
664 , addActionView;
665
666 // Set extension based on engine option
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
667 switch (engine) {
668 case 'ejs':
669 ext += '.ejs';
670 break;
671 case 'jade':
672 ext += '.jade';
673 break;
674 case 'handlebars':
675 ext += '.hbs';
676 break;
677 case 'mustache':
678 ext += '.ms';
679 break;
680 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
681
682 // Set application layout path
683 appLayoutPath = path.join('app', 'views', 'layouts', 'application');
684 // Copy template view to application path
edf26c7 Formatting
mde authored
685 addActionView = function (action) {
dc80e70 @larzconwell Add ejs extension to copy and make it silent when copying over a new app
larzconwell authored
686 jake.cpR(path.join(templateViewDir, action + ext + '.ejs'), appViewDir, {silent: true});
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
687 };
688
689 jake.mkdirP(appViewDir);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
690 jake.mkdirP('app/views/layouts');
691 addActionView('index');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
692
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
693 // Add views for the other CRUD actions when doing a full-on resource
6967867 Misc. fixes to get generators working
mde authored
694 if (!options.bare) {
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
695 ['add', 'edit', 'show'].forEach(function (action) {
696 addActionView(action);
697 });
698 }
699
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
700 // Create default layout if one doesn't exist
701 // Hack: There should be a better way to detect if a application layout exists
6967867 Misc. fixes to get generators working
mde authored
702 if (!utils.file.existsSync(appLayoutPath + '.html.ejs') && !utils.file.existsSync(appLayoutPath + '.html.jade') &&
703 !utils.file.existsSync(appLayoutPath + '.html.hbs') && !utils.file.existsSync(appLayoutPath + '.html.ms')) {
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
704 // Copy template layout file to apps application layout file
dc80e70 @larzconwell Add ejs extension to copy and make it silent when copying over a new app
larzconwell authored
705 jake.cpR(path.join(templateViewDir, 'layout' + ext + '.ejs'), appLayoutPath + ext, {silent: true});
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
706 }
707
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
708 // Add engine to package.json if it's not EJS
709 if (engine !== 'ejs') {
710 // Change to handlebars as we use it behind the scenes
711 if (engine === 'mustache') {
712 engine = 'handlebars';
713 }
714 var data = {dependencies: {}};
715 data.dependencies[engine] = "*";
716
717 mixinJSONData('package.json', data);
718 }
719
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
720 console.log('[Added] View templates');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
721 });
722
edf26c7 Formatting
mde authored
723 task('viewsScaffold', function (name, options) {
6967867 Misc. fixes to get generators working
mde authored
724 if (!name) {
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up to 5...
larzconwell authored
725 throw new Error('No view name specified.');
726 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
727
728 options = options || {};
729
730 // Option defaults
731 options.engine = options.engine || 'ejs';
732
d92f157 Get generators working
mde authored
733 var names = utils.string.getInflections(name)
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
734 , engine = options.engine
735 , appViewDir = path.join('app', 'views', names.filename.plural)
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
736 , templateViewDir
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
737 , cmds = []
738 , ext = '.html'
739 , appLayoutPath
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
740 , layoutViewPath
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
741 , actions
742 , addActionView
ca35849 @larzconwell Closes #250, Add realtime code generation to the other template language...
larzconwell authored
743 , viewPath
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
744 , text
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS and...
larzconwell authored
745 , adapter
746 , templContent;
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
747
748 // Set extension based on engine option
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
749 switch (engine) {
750 case 'ejs':
751 ext += '.ejs';
752 break;
753 case 'jade':
754 ext += '.jade';
755 break;
756 case 'handlebars':
757 ext += '.hbs';
758 break;
759 case 'mustache':
760 ext += '.ms';
761 break;
762 }
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
763
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
764 // Get view path
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
765 templateViewDir = options.realtime ?
766 path.join(__dirname, 'scaffold', 'realtime', 'views', engine) :
767 path.join(__dirname, 'scaffold', 'views', engine);
768
769
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
770 // Set application layout path
771 appLayoutPath = path.join('app', 'views', 'layouts', 'application');
772
773 // Function to compile the template
edf26c7 Formatting
mde authored
774 addActionView = function (action) {
ca35849 @larzconwell Closes #250, Add realtime code generation to the other template language...
larzconwell authored
775 viewPath = path.join(templateViewDir, action + ext + '.ejs');
776 if (!utils.file.existsSync(viewPath)) {
777 return;
778 }
779 text = fs.readFileSync(viewPath).toString();
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
780
781 // Compile template text
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS and...
larzconwell authored
782 adapter = new Adapter({engine: 'ejs', template: text});
783 templContent = adapter.render({names: names, properties: _formatModelProperties(options.properties)});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
784
785 // Write file
786 fs.writeFileSync(path.join(appViewDir, action + ext),
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS and...
larzconwell authored
787 templContent.replace(/<@/g, '<%').replace(/@>/g, '%>'), 'utf8');
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
788 };
789
790 jake.mkdirP(appViewDir);
791 jake.mkdirP('app/views/layouts');
792
793 // Add views for the other CRUD actions when doing a full-on resource
9f7a384 @larzconwell Closes #246, Generator: Put form into a partial for add and edit actions...
larzconwell authored
794 ['index', 'add', 'show', 'edit', 'form'].forEach(function (action) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
795 addActionView(action);
796 });
797
798 // Create default layout if one doesn't exist
799 // Hack: There should be a better way to detect if a application layout exists
6967867 Misc. fixes to get generators working
mde authored
800 if (!utils.file.existsSync(appLayoutPath + '.html.ejs') && !utils.file.existsSync(appLayoutPath + '.html.jade') &&
801 !utils.file.existsSync(appLayoutPath + '.html.hbs') && !utils.file.existsSync(appLayoutPath + '.html.ms')) {
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
802 // Copy template layout file to apps application layout file
dc80e70 @larzconwell Add ejs extension to copy and make it silent when copying over a new app
larzconwell authored
803 jake.cpR(path.join(templateViewDir, 'layout' + ext + '.ejs'), appLayoutPath + ext, {silent: true});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
804 }
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
805
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
806 // Add engine to package.json if it's not EJS
807 if (engine !== 'ejs') {
808 // Change to handlebars as we use it behind the scenes
809 if (engine === 'mustache') {
810 engine = 'handlebars';
811 }
812 var data = {dependencies: {}};
813 data.dependencies[engine] = "*";
814
815 mixinJSONData('package.json', data);
816 }
817
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
818 console.log('[Added] View templates');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
819 });
820
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
821 // Generate a new application secret in environment.js
9e1fe04 Got generators working.
mde authored
822 task('secret', function () {
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
823 var secretsFile = path.join(cwd, 'config', 'secrets.json')
824 , secret = utils.string.uuid(128);
825
826 mixinJSONData(secretsFile, {secret: secret});
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
827 console.log('Added app-secret to config/secrets.json.\n' +
828 'DO NOT add this file into your revision control.\n' +
829 'DO make a backup of it, keep it someplace safe.');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
830 });
831
832 });
7ae4e05 Added generator for auth
mde authored
833
834 namespace('auth', function () {
835
aa49bd9 Added generator for auth:update
mde authored
836 task('update', {async: true}, function () {
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
837 var updatePath = path.join('geddy-passport', 'app', 'helpers', 'passport')
838 , from
839 , to;
840
841 console.log('Updating passport helpers from', geddyPassport);
842 jake.exec('npm uninstall ' + geddyPassport +
843 ' && npm install ' + geddyPassport, function () {
844 from = path.join(cwd, 'node_modules', updatePath);
845 to = path.join(cwd, 'app', 'helpers');
846
847 jake.rmRf(path.join(cwd, 'passport'), {silent: true});
aa49bd9 Added generator for auth:update
mde authored
848 jake.cpR(from, to, {silent: true});
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
849
850 console.log("\nCleaning up...");
851 jake.exec('npm uninstall ' + geddyPassport, function () {
aa49bd9 Added generator for auth:update
mde authored
852 complete();
853 });
854 }, {printStdout: true});
855 });
856
c189922 @larzconwell Include engine in auth:init, auth:init will now use the give engines
larzconwell authored
857 task('init', {async: true}, function (engine) {
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
858 var readline = require('readline')
859 , fromBase = path.join(cwd, 'node_modules', 'geddy-passport')
860 , install = false
861 , rl, installPackages, passportCopy;
862
c189922 @larzconwell Include engine in auth:init, auth:init will now use the give engines
larzconwell authored
863 if (!engine || engine == 'default') {
864 engine = 'ejs';
865 }
866
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
867 // Create and start the prompt
868 rl = readline.createInterface({
869 input: process.stdin
870 , output: process.stdout
871 });
872 rl.setPrompt("WARNING: This command will create/overwrite files in your app.\n" +
873 "Do you wish to continue?(yes|no)\n\n");
7ae4e05 Added generator for auth
mde authored
874 rl.prompt();
875
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
876 rl.on('line', function (line) {
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
877 if (line === 'yes' || line === 'y') {
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
878 install = true;
7ae4e05 Added generator for auth
mde authored
879 }
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
880
7ae4e05 Added generator for auth
mde authored
881 rl.close();
882 });
883
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
884 rl.on('close', function () {
885 if (!install) {
886 return;
887 }
7ae4e05 Added generator for auth
mde authored
888
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
889 console.log('Installing', geddyPassport);
890 jake.exec('npm uninstall ' + geddyPassport +
c189922 @larzconwell Include engine in auth:init, auth:init will now use the give engines
larzconwell authored
891 ' && npm install ' + geddyPassport, installPackages, {printStdout: true});
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
892 });
7ae4e05 Added generator for auth
mde authored
893
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
894 // Gets the package versions from geddy-passport's package.json
895 // and installs them, then calls passportCopy
896 installPackages = function () {
897 var deps = require(path.join(fromBase, 'package')).dependencies
898 , packages = ''
899 , k;
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
900
dc80e70 @larzconwell Add ejs extension to copy and make it silent when copying over a new app
larzconwell authored
901 mixinJSONData(path.join(cwd, 'package.json'), {dependencies: deps});
902
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
903 for (k in deps) {
904 packages += k + '@' + deps[k] + ' ';
905 }
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
906
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
907 console.log("\nInstalling", packages);
908 jake.exec('npm uninstall ' + packages +
909 ' && npm install ' + packages, passportCopy, {printStdout: true});
910 };
911
912 // Copy the contents of geddy-passport into the application
913 passportCopy = function () {
914 var list = require(path.join(fromBase, 'file_list'))
915 , routerPath = getRouterPath()
916 , newRoute
917 , from
c189922 @larzconwell Include engine in auth:init, auth:init will now use the give engines
larzconwell authored
918 , to
dc80e70 @larzconwell Add ejs extension to copy and make it silent when copying over a new app
larzconwell authored
919 , p
c189922 @larzconwell Include engine in auth:init, auth:init will now use the give engines
larzconwell authored
920 , i;
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
921
922 // Copy files to the application
c189922 @larzconwell Include engine in auth:init, auth:init will now use the give engines
larzconwell authored
923 for (i = 0; i < list.length; i++) {
924 item = list[i];
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
925 from = path.join(fromBase, item);
926 to = path.dirname(path.join(cwd, item));
927
c189922 @larzconwell Include engine in auth:init, auth:init will now use the give engines
larzconwell authored
928 if (item.match('/app/views')) {
929 // If the view is not for the given engine, skip it
930 if (!item.match(engine)) {
931 continue;
932 }
933
934 to = to.replace(engine, "");
935 }
936
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
937 jake.mkdirP(to);
dc80e70 @larzconwell Add ejs extension to copy and make it silent when copying over a new app
larzconwell authored
938 console.log('Creating file:', path.join(to, path.basename(item)));
939
940 // Delete any existing interferring templates
941 if (item.match('/app/views')) {
942 ['.jade', '.ejs', '.ms', '.mustache', '.hbs', '.handlebars'].forEach(function (ext) {
943 p = path.basename(item, path.extname(item)) + ext;
944
945 jake.rmRf(path.join(to, p), {silent: true});
946 });
947 }
948
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
949 jake.cpR(from, to, {silent: true});
c189922 @larzconwell Include engine in auth:init, auth:init will now use the give engines
larzconwell authored
950 }
564357e Integrated generator with CLI, did some cleanup
mde authored
951
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
952 // Add new routes to router
953 if (routerPath) {
954 // CoffeeScript routes
955 if (routerPath.match('.coffee')) {
956 newRoute = "router.get('/login').to 'Main.login'\n" +
957 "router.get('/logout').to 'Main.logout'\n" +
958 "router.post('/auth/local').to 'Auth.local'\n" +
959 "router.get('/auth/twitter').to 'Auth.twitter'\n" +
960 "router.get('/auth/twitter/callback').to 'Auth.twitterCallback'\n" +
961 "router.get('/auth/facebook').to 'Auth.facebook'\n" +
962 "router.get('/auth/facebook/callback').to 'Auth.facebookCallback'\n" +
963 "router.get('/auth/yammer').to 'Auth.yammer'\n" +
964 "router.get('/auth/yammer/callback').to 'Auth.yammerCallback'\n" +
965 "router.resource 'users'";
966 } else {
967 newRoute = "router.get('/login').to('Main.login');\n" +
968 "router.get('/logout').to('Main.logout');\n" +
969 "router.post('/auth/local').to('Auth.local');\n" +
970 "router.get('/auth/twitter').to('Auth.twitter');\n" +
971 "router.get('/auth/twitter/callback').to('Auth.twitterCallback');\n" +
972 "router.get('/auth/facebook').to('Auth.facebook');\n" +
973 "router.get('/auth/facebook/callback').to('Auth.facebookCallback');\n" +
974 "router.get('/auth/yammer').to('Auth.yammer');\n" +
975 "router.get('/auth/yammer/callback').to('Auth.yammerCallback');\n" +
976 "router.resource('users');";
977 }
978
979 if (addRoute(routerPath, "\n" + newRoute)) {
980 console.log('\nAdded authentication routes:\n' + newRoute);
981 } else {
982 console.log('\nAuthentication routes already defined in', routerPath);
983 }
7ae4e05 Added generator for auth
mde authored
984 }
a9d96ec @larzconwell Include auth in usage dialog.
larzconwell authored
985 else {
986 console.log('\nThere is no router file to add routes too.');
987 }
988
989 // Create secrets and copy the secrets template
990 console.log("\nCreating secrets.json file with stubbed-out Passport config.");
991 jake.cpR(path.join(fromBase, 'config', 'secrets.json.template'),
992 path.join(cwd, 'config', 'secrets.json'), {silent: true});
993 jake.Task['gen:secret'].invoke();
994
995 // Remove geddy-passport as it isn't needed anymore
996 console.log('\nCleaning up...');
997 jake.exec('npm uninstall geddy-passport', function () {
998 console.log('Please set up your Passport config in config/secrets.json');
999 complete();
1000 });
1001 };
7ae4e05 Added generator for auth
mde authored
1002 });
1003
1004 });
Something went wrong with that request. Please try again.