Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 1005 lines (849 sloc) 29.208 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 {
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell 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
d278a7e Get example app working with external model lib
mde authored
117 namespace('env', function () {
3e0c059 Added basic console feature
mde authored
118 task('init', function (environment) {
d278a7e Get example app working with external model lib
mde authored
119 jake.addListener('complete', function (e) {
120 jake.Task['env:cleanup'].invoke();
121 });
9e1fe04 Got generators working.
mde authored
122
3e0c059 Added basic console feature
mde authored
123 geddy.config = require('../lib/config').readConfig({
124 environment: environment || 'development'
125 });
9e1fe04 Got generators working.
mde authored
126 geddy.model = require('model');
127
5da7b94 Cleaned up env init process a little more
mde authored
128 require('../lib/init').init(geddy, function () {
9e1fe04 Got generators working.
mde authored
129 complete();
130 });
131 }, {async: true});
d278a7e Get example app working with external model lib
mde authored
132
133 task('cleanup', function () {
134 // Disconnect all the adapters
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS …
larzconwell authored
135 var adapters = geddy.model.loadedAdapters
136 , adapter;
137
d278a7e Get example app working with external model lib
mde authored
138 for (var p in adapters) {
48fb464 Fixed DB adapter connect/disconnect
mde authored
139 adapter = adapters[p];
140 if (typeof adapter.disconnect == 'function') {
141 adapter.disconnect();
142 }
d278a7e Get example app working with external model lib
mde authored
143 }
144 });
145
3e0c059 Added basic console feature
mde authored
146 });
147
148 namespace('console', function () {
149 task('start', ['env:init'], {async: true}, function (environment) {
150 var env = environment || 'development'
151 , t = jake.Task['env:init'];
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS …
larzconwell authored
152
3e0c059 Added basic console feature
mde authored
153 t.addListener('complete', function () {
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocom…
larzconwell authored
154 var repl = require('repl')
155 , rl;
156
157 rl = repl.start({
158 prompt: '>>> '
159 , input: process.stdin
160 , output: process.stdout
3e0c059 Added basic console feature
mde authored
161 });
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS …
larzconwell authored
162
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocom…
larzconwell authored
163 rl.on('close', function () {
164 console.log('Exiting...');
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
165 return complete();
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocom…
larzconwell authored
166 })
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
167
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocom…
larzconwell authored
168 rl.context.capture = function (err, data) {
169 return rl.context.results = {
170 err: err
171 , data: data
172 };
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
173 };
174
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocom…
larzconwell authored
175 rl.context.echo = function (err, data) {
176 rl.context.capture(err, data);
177 if (err) {
178 console.log('Error: ', err);
179 }
180
181 if (data) {
182 if (data.length) {
183 for (var i in data) {
184 if (data[i] && data[i].toData) {
185 console.log(data[i].toData());
186 } else {
187 console.log(data[i]);
188 }
189 }
190 }
191 else {
192 if (data && data.toData) {
193 console.log(data.toData());
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
194 } else {
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocom…
larzconwell authored
195 console.log(data);
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
196 }
197 }
198 } else {
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocom…
larzconwell authored
199 console.log('No data');
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
200 }
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocom…
larzconwell authored
201 };
3900a2b @larzconwell Closes #215 Added a geddy routes command that takes an optional route
larzconwell authored
202
203 rl.context.routes = function (resource) {
204 console.log(getRoutes(resource));
205 };
bb90185 @larzconwell Closes #252, Replace readline REPL with repl REPL, which does autocom…
larzconwell authored
206 });
f0c7bc1 @Techwraith added echo and capture helper funtions to the console
Techwraith authored
207
3e0c059 Added basic console feature
mde authored
208 t.invoke(env);
209 });
210 });
d278a7e Get example app working with external model lib
mde authored
211
3900a2b @larzconwell Closes #215 Added a geddy routes command that takes an optional route
larzconwell authored
212 namespace('routes', function () {
213
214 task('show', ['env:init'], {async: true}, function (resource) {
215 console.log('Showing route results for "' + resource + '"');
216 console.log(getRoutes(resource));
217 });
218
219 });
220
d278a7e Get example app working with external model lib
mde authored
221 namespace('db', function () {
48fb464 Fixed DB adapter connect/disconnect
mde authored
222 task('createTable', ['env:init'], function (name) {
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
223
9e1fe04 Got generators working.
mde authored
224 var modelName
48fb464 Fixed DB adapter connect/disconnect
mde authored
225 , createTable
d278a7e Get example app working with external model lib
mde authored
226 , adapters
9e1fe04 Got generators working.
mde authored
227 , adapter;
228
229 if (typeof name == 'string') {
230 if (name.indexOf(',') > -1) {
231 modelNames = name.split(',');
232 }
233 else {
234 modelNames = [name];
235 }
236 }
237 else {
238 modelNames = name;
239 }
240
d278a7e Get example app working with external model lib
mde authored
241 createTable = function () {
48fb464 Fixed DB adapter connect/disconnect
mde authored
242 if ((m = modelNames.shift())) {
9e1fe04 Got generators working.
mde authored
243
244 // Make sure this is a correct model-name
245 m = utils.string.getInflections(m).constructor.singular;
246 if (!geddy.model[m]) {
247 throw new Error(m + ' is not a known model.');
248 }
249
d278a7e Get example app working with external model lib
mde authored
250 adapter = geddy.model.adapters[m];
9e1fe04 Got generators working.
mde authored
251 if (adapter) {
252 console.log('Creating table for ' + m);
253 adapter.createTable(m, function (err, data) {
254 if (err) { throw err }
255 createTable();
256 });
257 }
258 else {
d278a7e Get example app working with external model lib
mde authored
259 createTable();
9e1fe04 Got generators working.
mde authored
260 }
d278a7e Get example app working with external model lib
mde authored
261 }
262 else {
263 complete();
264 }
265 };
266 createTable();
48fb464 Fixed DB adapter connect/disconnect
mde authored
267 }, {async: true});
268
269 task('retrofit', ['env:init'], function () {
270 var modelNames = Object.keys(geddy.model.descriptionRegistry)
271 , createTask = jake.Task['db:createTable'];
272 createTask.once('complete', function () {
273 complete();
274 });
275 createTask.invoke(modelNames);
d278a7e Get example app working with external model lib
mde authored
276 }, {async: true});
277
278 });
279
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
280 namespace('gen', function () {
281
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the def…
larzconwell authored
282 var _writeTemplate = function (name, filename, dirname, opts) {
283 var options = opts || {}
284 , names = utils.string.getInflections(name)
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
285 , text = fs.readFileSync(path.join(__dirname, filename + '.ejs'), 'utf8').toString()
cf999ea @larzconwell Deleted bare controller template
larzconwell authored
286 , bare = options.bare || false // Default to full controller
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS …
larzconwell authored
287 , adapter
288 , templContent
9ec7c3a @MiguelMadero Changed the generator to create the directory in case it doesn't exis…
MiguelMadero authored
289 , fileDir
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
290 , filePath;
291
292 // Render with the right model name
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS …
larzconwell authored
293 adapter = new Adapter({engine: 'ejs', template: text});
294 templContent = adapter.render({names: names, bare: bare, properties: options.properties});
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
295
296 // Write file
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
297 fileDir = dirname;
fc70402 @MiguelMadero Removed usage of fs.existsSync for compatibility with node 0.6 and ch…
MiguelMadero authored
298 if (!utils.file.existsSync(fileDir)) {
299 fs.mkdirSync(fileDir);
300 }
9ec7c3a @MiguelMadero Changed the generator to create the directory in case it doesn't exis…
MiguelMadero authored
301
302 filePath = path.join(fileDir, names.filename[options.inflection] + '.js');
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS …
larzconwell authored
303 fs.writeFileSync(filePath, templContent, 'utf8');
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
304
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
305 console.log('[Added] ' + filePath);
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
306 };
307
edf26c7 Formatting
mde authored
308 var _formatModelProperties = function (properties) {
515a929 @larzconwell Closes #259, Fix error where no properties at all would throw when co…
larzconwell authored
309 var obj = {default: {name: '', type: ''}};
9e1fe04 Got generators working.
mde authored
310 if (!properties) {
515a929 @larzconwell Closes #259, Fix error where no properties at all would throw when co…
larzconwell authored
311 return obj;
9e1fe04 Got generators working.
mde authored
312 }
515a929 @larzconwell Closes #259, Fix error where no properties at all would throw when co…
larzconwell authored
313 obj['default'].name = 'id';
89831d2 @larzconwell Default to string ID type instead of int for generators.
larzconwell authored
314 obj['default'].type = 'string';
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
315
d3f4d44 Spaces aren't legal inside passed Jake params.
mde authored
316 var itemsArr = properties.split('%')
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
317 , name
318 , type
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
319 , args
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
320 , i
321 , value;
322
26600c5 @larzconwell Just made a few loop optimizations for Array loops
larzconwell authored
323 i = -1;
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
324 while (++i < itemsArr.length) {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
325 value = itemsArr[i];
dd3a65b @larzconwell Don't allow nodel property names to be CamelCased
larzconwell authored
326 name = utils.string.camelize(value.replace(/:.*/g, ''));
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
327 type = value.replace(/[a-zA-Z]*:?/, '');
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
328 args = type.replace(/[a-zA-Z]*:?/, '');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
329
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
330 // Take off any args on the type
331 type = type.replace(/:.*/g, '');
332
9e1fe04 Got generators working.
mde authored
333 // Defaults and aliases
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
334 if (!type) {
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
335 type = 'string';
9e2a69d @larzconwell If text attribute is given it switches to string, also took out place…
larzconwell authored
336 }
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the …
larzconwell authored
337 if (args === 'def') {
338 args = 'default';
339 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
340
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
341 switch (type) {
f835d2f @larzconwell Move type checks for generatirs into a switch statement so can be eas…
larzconwell authored
342 case 'text':
343 type = 'string';
344 break;
345 case 'integer':
346 type = 'int';
347 break;
348 case 'bool':
349 type = 'boolean';
350 break;
351 case 'default':
352 case 'def':
353 type = 'string';
354 args = 'default';
355 break;
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
356 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
357
bc02170 @larzconwell Closes #230, Don't manually add the ID attribute into the model prope…
larzconwell authored
358 // Manage properties that deal with changing default properties
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the …
larzconwell authored
359 if (args === 'default') {
bc02170 @larzconwell Closes #230, Don't manually add the ID attribute into the model prope…
larzconwell authored
360 // Reset old default property to it's own property, only if it's not
361 // already the default
362 if (name !== obj['default'].name) {
363 // If the new default item already exists then delete it
364 if (obj[name]) {
365 delete obj[name];
366 }
367
368 obj[obj['default'].name] = obj[obj['default'].name] || obj['default'];
369 }
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the …
larzconwell authored
370
371 // Add new default property
372 obj['default'] = {name: name, type: type};
373 continue;
d9fe3bb @larzconwell Get rid of all the syntax differences, well most of them.
larzconwell authored
374 }
375
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the …
larzconwell authored
376 // If ID property is given and it matches the default
377 // then rewrite the default with the new ID property
378 if (name === 'id' && obj['default'].name === 'id') {
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
379 obj['default'] = {name: name, type: type};
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the …
larzconwell authored
380 continue;
9e1fe04 Got generators working.
mde authored
381 }
d9489a2 @larzconwell Closes 210, Generators will use `name` and `title` properties as the …
larzconwell authored
382
383 // If the name is name or title then set them to default, otherwise add
384 // the property normally
385 if (name === 'name' || name === 'title') {
386 // Reset old default to it's own property
387 obj[obj['default'].name] = obj[obj['default'].name] || obj['default'];
388
389 // Add new default property
390 obj['default'] = {name: name, type: type};
391 } else {
392 obj[name] = {name: name, type: type};
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
393 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
394 }
395
9925793 @larzconwell Added default option to model property arguments as @TechWraith sugge…
larzconwell authored
396 return obj;
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
397 };
398
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
399 // Creates a new Geddy app scaffold
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
400 task('app', function (name, engine, realtime) {
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
401 var basePath = path.join(__dirname, 'base')
402 , mkdirs
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
403 , cps
404 , text
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS …
larzconwell authored
405 , adapter;
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
406
9e1fe04 Got generators working.
mde authored
407 if (!name) {
408 throw new Error('No app name specified.');
409 }
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
410 if (!engine || engine == 'default') {
9e1fe04 Got generators working.
mde authored
411 engine = 'ejs';
412 }
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
413 if (realtime == 'default') {
414 realtime = false;
415 }
416
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
417 mkdirs = [
418 ''
419 , 'config'
420 , 'app/models'
421 , 'app/controllers'
422 , 'app/helpers'
423 , 'lib'
424 , 'log'
425 , 'node_modules'
426 , 'test'
427 ];
428 cps = [
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
429 (realtime) ? ['realtime/views/' + engine, 'app/views'] : ['views/' + engine, 'app/views']
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
430 , ['public', '']
431 , ['router.js', 'config']
432 , ['init.js', 'config']
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
433 , (realtime) ? ['realtime/environment.js', 'config'] : ['environment.js', 'config']
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
434 , ['development.js', 'config']
435 , ['production.js', 'config']
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
436 , ['secrets.json', 'config']
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
437 , ['main.js', 'app/controllers']
438 , ['application.js', 'app/controllers']
439 , ['favicon.ico', 'public']
7dbdced Use secrets.json for secrets and keys, instead of environment.js
mde authored
440 , ['gitignore.txt', '.gitignore']
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
441 ];
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
442
edf26c7 Formatting
mde authored
443 mkdirs.forEach(function (dir) {
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
444 jake.mkdirP(path.join(name, dir));
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
445 });
edf26c7 Formatting
mde authored
446 cps.forEach(function (cp) {
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
447 jake.cpR(path.join(basePath, cp[0]), path.join(name, cp[1]), {silent: true});
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
448 });
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
449
944267c @Techwraith RT-MVC: added build init step, copied socket.io into all realtime app…
Techwraith authored
450 // one offs
451 if (realtime) {
452 jake.cpR(path.join(__dirname, '..', 'node_modules', 'socket.io' ), path.join(name, 'node_modules'), {silent: true});
453 }
454
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
455 // Compile Jakefile
456 text = fs.readFileSync(path.join(basePath, 'Jakefile.ejs'), 'utf8').toString();
2e8e3fb @larzconwell Remove the third party dependency Templato(which was rewritten in JS …
larzconwell authored
457 adapter = new Adapter({engine: 'ejs', template: text});
458 fs.writeFileSync(path.join(name, 'Jakefile'), adapter.render({appName: name}), 'utf8');
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
459
b786faf @larzconwell Closes #273 Add template engine to package.json if you change the
larzconwell authored
460 // Compile package.json
461 text = fs.readFileSync(path.join(basePath, 'package.json.ejs'), 'utf8').toString();
462 adapter = new Adapter({engine: 'ejs', template: text});
463 fs.writeFileSync(path.join(name, 'package.json'), adapter.render({appName: name}), 'utf8');
464
465 // Add engine to package.json if it's not EJS
466 if (engine !== 'ejs') {
467 // Change to handlebars as we use it behind the scenes
468 if (engine === 'mustache') {
469 engine = 'handlebars';
470 }
471 var data = {dependencies: {}};
472 data.dependencies[engine] = "*";
473
474 mixinJSONData(path.join(name, 'package.json'), data);
475 }
476
ae3f611 @larzconwell Created app and resource generators for Jade, Handlebars and Mustache
larzconwell authored
477 console.log('Created app ' + name + '.');
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
478 });
479
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
480 // Creates a resource with a model, controller and a resource route
edf26c7 Formatting
mde authored
481 task('resource', function (name, modelProperties) {
9e1fe04 Got generators working.
mde authored
482 var names
483 , modelTask = jake.Task['gen:model'];
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
484
9e1fe04 Got generators working.
mde authored
485 if (!name) {
486 throw new Error('No resource name specified.');
487 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
488
9e1fe04 Got generators working.
mde authored
489 modelTask.on('complete', function () {
490 jake.Task['gen:controller'].invoke(name);
491 jake.Task['gen:route'].invoke(name);
492 names = utils.string.getInflections(name);
493 // Create views folder but not actions
494 jake.mkdirP(path.join('app', 'views', names.filename.plural));
495 console.log('[Added] ' + names.filename.plural + ' view directory');
496 complete();
497 });
498 modelTask.invoke(name, modelProperties);
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
499
9e1fe04 Got generators working.
mde authored
500 }, {async: true});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
501
502 // 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
503 task('scaffold', function (name, realtime, engine, modelProperties) {
9e1fe04 Got generators working.
mde authored
504 var modelTask = jake.Task['gen:model'];
505
506 if (!name) {
507 throw new Error('No scaffold name specified.');
508 }
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
509 if (!engine || engine == 'default') {
9e1fe04 Got generators working.
mde authored
510 engine = 'ejs';
511 }
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
512 if (!realtime || realtime == 'default') {
513 realtime = false;
514 }
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
515
9e1fe04 Got generators working.
mde authored
516 modelTask.on('complete', function () {
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
517 jake.Task['gen:test'].invoke(name,
518 {properties: modelProperties});
9e1fe04 Got generators working.
mde authored
519 jake.Task['gen:controllerScaffold'].invoke(name,
520 {properties: modelProperties});
521 jake.Task['gen:route'].invoke(name);
522 jake.Task['gen:viewsScaffold'].invoke(name,
057bd49 @Techwraith RT-MVC: Real time app generation and resource scaffolding is working
Techwraith authored
523 {engine: engine, properties: modelProperties, realtime: realtime});
9e1fe04 Got generators working.
mde authored
524 complete();
693a0fe @larzconwell Created a file utils function that will search for a dir/file in up t…
larzconwell authored
525 });
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the def…
larzconwell authored
526 modelTask.invoke(name, modelProperties, 'scaffold');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
527
9e1fe04 Got generators working.
mde authored
528 }, {async: true});
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
529
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the def…
larzconwell authored
530 task('model', function (name, properties, modelPath) {
9e1fe04 Got generators working.
mde authored
531 var createTableTask;
532 if (!name) {
533 throw new Error('No model name specified.');
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
534 }
b262b1c @larzconwell Default ID attribute is now 'int', if scaffold model template the def…
larzconwell authored
535 if (!modelPath) {
536 modelPath = 'resource';
537 }
538 modelPath += '/model';
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
539
109ee59 Added Jakefile, test dir to app, test stubs to scaffolds.
mde authored
540 _writeTemplate(name, modelPath, path.join('app', 'models'), {
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
541 inflection: 'singular'
542 , properties: _formatModelProperties(properties)
543 });
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
544
9e1fe04 Got generators working.
mde 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();
1282075 @larzconwell Fix old Fleegix lib code for template adapters
larzconwell authored
550 });
9e1fe04 Got generators working.
mde authored
551 createTableTask.invoke(name);
c389ea9 Move generator Jake tasks into a separate Jakefile.
mde authored
552
9e1fe04 Got generators working.
mde authored
553 }, {async: true});
a082031 @larzconwell Updated readme to reflect new generators
larzconwell authored
554
9e1fe04 Got generators working.
mde authored
555 task('controller', function (name) {
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
9e1fe04 Got generators working.
mde authored
587 task('bareController', function (name, engine) {
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 templ…
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 templ…
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 templ…
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 t…
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 langu…
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 …
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 langu…
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 …
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 …
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 acti…
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.