-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathGenerator.js
505 lines (387 loc) · 19.3 KB
/
Generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/**
* Module dependencies
*/
var util = require('util');
var _ = require('lodash');
var fs = require('fs');
var path = require('path');
_.defaults = require('merge-defaults');
_.str = require('underscore.string');
/**
* sails-generate-scaffold
*
* Usage:
* `sails generate scaffold`
*
* @description Generates a scaffold
* @help See http://links.sailsjs.org/docs/generators
*/
module.exports = {
/**
* `before()` is run before executing any of the `targets`
* defined below.
*
* This is where we can validate user input, configure default
* scope variables, get extra dependencies, and so on.
*
* @param {Object} scope
* @param {Function} cb [callback]
*/
before: function (scope, cb) {
// scope.args are the raw command line arguments.
//
// e.g. if someone runs:
// $ sails generate scaffold user find create update
// then `scope.args` would be `['user', 'find', 'create', 'update']`
if (!scope.args[0]) {
return cb( new Error('Please provide a name for this scaffold.') );
}
// scope.rootPath is the base path for this generator
//
// e.g. if this generator specified the target:
// './Foobar.md': { copy: 'Foobar.md' }
//
// And someone ran this generator from `/Users/dbowie/sailsStuff`,
// then `/Users/dbowie/sailsStuff/Foobar.md` would be created.
if (!scope.rootPath) {
return cb( INVALID_SCOPE_VARIABLE('rootPath') );
}
// Attach defaults
_.defaults(scope, {
// $sails generate scaffold user --> id returns User
id: _.str.capitalize(scope.args[0]),
// $sails generate scaffold user --> modelControllerName returns user
modelControllerName: scope.args[0],
actions: [],
// $sails generate scaffold user name:string email:email --> scope.args.slice(1) returns ['name:string','email:email']
attributes: scope.args.slice(1)
});
//Get the optional model attributes and validate them
var attributes = scope.attributes;
var invalidAttributes = [];
attributes = _.map(attributes, function(attribute, i) {
var parts = attribute.split(':');
if (parts[1] === undefined) parts[1] = 'string';
// Handle invalidAttributes
if (!parts[1] || !parts[0]) {
invalidAttributes.push(
'Invalid attribute notation: "' + attribute + '"');
return;
}
return {
name: parts[0],
type: parts[1]
};
});
// Add the optional model attributes to the scope
_.defaults(scope, {
modelAttributes: attributes
});
// Pluck just the name values
var modelAttributeNames = _.pluck(scope.modelAttributes, 'name');
// Add the optional model attribute names to the scope
_.defaults(scope, {
modelAttributeNames: modelAttributeNames
});
/**
__ ___ _______ _ _
\ \ / (_) |__ __| | | | |
_ __ _____ _\ \ / / _ _____ _| | ___ _ __ ___ _ __ | | __ _| |_ ___
| '_ \ / _ \ \ /\ / /\ \/ / | |/ _ \ \ /\ / / |/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
| | | | __/\ V V / \ / | | __/\ V V /| | __/ | | | | | |_) | | (_| | || __/
|_| |_|\___| \_/\_/ \/ |_|\___| \_/\_/ |_|\___|_| |_| |_| .__/|_|\__,_|\__\___|
| |
|_|
**/
// This generates a template using the newFormFields.template located in scaffold/templates
// combined with modelAttributeNames to produce form fields for the new view derived from the model attributes
var NEW_FORM_FIELDS_TEMPLATE = path.resolve(__dirname, './templates/newFormFields.template');
NEW_FORM_FIELDS_TEMPLATE = fs.readFileSync(NEW_FORM_FIELDS_TEMPLATE, 'utf8');
var compiledNewFormFields = _.template(NEW_FORM_FIELDS_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName
})
// Add the compiled new form fields to the scope
_.defaults(scope, {
compiledNewFormFields: compiledNewFormFields
});
//This generates a template using newFlash.template
var NEW_FLASH_TEMPLATE = path.resolve(__dirname, './templates/newFlash.template');
NEW_FLASH_TEMPLATE = fs.readFileSync(NEW_FLASH_TEMPLATE, 'utf8');
var compiledNewFlash = _.template(NEW_FLASH_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName
})
// This puts erb style delimeters
compiledNewFlash = compiledNewFlash.replace(/ERBstart/g, '<%')
compiledNewFlash = compiledNewFlash.replace(/ERBend/g, '%>')
// Add the compiled show form fields to the scope
_.defaults(scope, {
compiledNewFlash: compiledNewFlash
});
/**
_ __ ___ _______ _ _
| | \ \ / (_) |__ __| | | | |
___| |__ _____ _\ \ / / _ _____ _| | ___ _ __ ___ _ __ | | __ _| |_ ___
/ __| '_ \ / _ \ \ /\ / /\ \/ / | |/ _ \ \ /\ / / |/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
\__ \ | | | (_) \ V V / \ / | | __/\ V V /| | __/ | | | | | |_) | | (_| | || __/
|___/_| |_|\___/ \_/\_/ \/ |_|\___| \_/\_/ |_|\___|_| |_| |_| .__/|_|\__,_|\__\___|
| |
|_|
**/
// This generates a template using the showFormFields.template located in scaffold/templates
// combined with modelAttributeNames to produce form fields for the show view derived from the model attributes
var SHOW_FORM_FIELDS_TEMPLATE = path.resolve(__dirname, './templates/showFormFields.template');
SHOW_FORM_FIELDS_TEMPLATE = fs.readFileSync(SHOW_FORM_FIELDS_TEMPLATE, 'utf8');
var compiledShowFormFields = _.template(SHOW_FORM_FIELDS_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName
})
// This puts erb style delimeters
compiledShowFormFields = compiledShowFormFields.replace(/ERBstart/g, '<%=')
compiledShowFormFields = compiledShowFormFields.replace(/ERBend/g, '%>')
// Add the compiled show form fields to the scope
_.defaults(scope, {
compiledShowFormFields: compiledShowFormFields
});
// This generates a template using the showEditLink.template located in scaffold/templates
var SHOW_EDIT_LINK_TEMPLATE = path.resolve(__dirname, './templates/showEditLink.template');
SHOW_EDIT_LINK_TEMPLATE = fs.readFileSync(SHOW_EDIT_LINK_TEMPLATE, 'utf8');
var compiledShowEditLink = _.template(SHOW_EDIT_LINK_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName
})
// This puts erb style delimeters
compiledShowEditLink = compiledShowEditLink.replace(/ERBstart/g, '<%=')
compiledShowEditLink = compiledShowEditLink.replace(/ERBend/g, '%>')
_.defaults(scope, {
compiledShowEditLink: compiledShowEditLink
});
/**
_ _ __ ___ _______ _ _
(_) | | \ \ / (_) |__ __| | | | |
_ _ __ __| | _____ _\ \ / / _ _____ _| | ___ _ __ ___ _ __ | | __ _| |_ ___
| | '_ \ / _` |/ _ \ \/ /\ \/ / | |/ _ \ \ /\ / / |/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
| | | | | (_| | __/> < \ / | | __/\ V V /| | __/ | | | | | |_) | | (_| | || __/
|_|_| |_|\__,_|\___/_/\_\ \/ |_|\___| \_/\_/ |_|\___|_| |_| |_| .__/|_|\__,_|\__\___|
| |
|_|
**/
// This generates the table index headings using indexTableHeadings.template located in scaffold/templates
var INDEX_TABLE_HEADINGS_TEMPLATE = path.resolve(__dirname, './templates/indexTableHeadings.template');
INDEX_TABLE_HEADINGS_TEMPLATE = fs.readFileSync(INDEX_TABLE_HEADINGS_TEMPLATE, 'utf8');
var compiledIndexTableHeadings = _.template(INDEX_TABLE_HEADINGS_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName
})
_.defaults(scope, {
compiledIndexTableHeadings: compiledIndexTableHeadings
});
// This generates a template using the indexTableData.template located in scaffold/templates
// combined with modelAttributeNames to create an index.ejs view
var INDEX_TABLE_DATA_TEMPLATE = path.resolve(__dirname, './templates/indexTableData.template');
INDEX_TABLE_DATA_TEMPLATE = fs.readFileSync(INDEX_TABLE_DATA_TEMPLATE, 'utf8');
var compiledIndexTableData = _.template(INDEX_TABLE_DATA_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName,
compiledIndexTableHeadings: scope.compiledIndexTableHeadings
})
_.defaults(scope, {
modelControllerNamePluralized: scope.modelControllerName + 's'
});
// This puts erb style delimeters
compiledIndexTableData = compiledIndexTableData.replace(/ERBstart/g, '<%=')
compiledIndexTableData = compiledIndexTableData.replace(/ERBend/g, '%>')
// Add the compiled show form fields to the scope
_.defaults(scope, {
compiledIndexTableData: compiledIndexTableData
});
// This generates the indexForEach template
var INDEX_FOR_EACH_TEMPLATE = path.resolve(__dirname, './templates/indexForEach.template');
INDEX_FOR_EACH_TEMPLATE = fs.readFileSync(INDEX_FOR_EACH_TEMPLATE, 'utf8');
var compiledIndexForEach = _.template(INDEX_FOR_EACH_TEMPLATE, {
compiledIndexTableData: compiledIndexTableData,
modelControllerNamePluralized: scope.modelControllerNamePluralized,
modelControllerName: scope.modelControllerName
});
// This puts erb style delimeters
compiledIndexForEach = compiledIndexForEach.replace(/ERBstart/g, '<%')
compiledIndexForEach = compiledIndexForEach.replace(/ERBend/g, '%>')
_.defaults(scope, {
compiledIndexForEach: compiledIndexForEach
});
/**
_ _ ___ ___ _______ _ _
| (_) \ \ / (_) |__ __| | | | |
___ __| |_| |\ \ / / _ _____ _| | ___ _ __ ___ _ __ | | __ _| |_ ___
/ _ \/ _` | | __\ \/ / | |/ _ \ \ /\ / / |/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
| __/ (_| | | |_ \ / | | __/\ V V /| | __/ | | | | | |_) | | (_| | || __/
\___|\__,_|_|\__| \/ |_|\___| \_/\_/ |_|\___|_| |_| |_| .__/|_|\__,_|\__\___|
| |
|_|
**/
// This generates the editFormFields template
var EDIT_FORM_FIELDS_TEMPLATE = path.resolve(__dirname, './templates/editFormFields.template');
EDIT_FORM_FIELDS_TEMPLATE = fs.readFileSync(EDIT_FORM_FIELDS_TEMPLATE, 'utf8');
var compiledEditFormFields = _.template(EDIT_FORM_FIELDS_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
modelControllerName: scope.modelControllerName
});
// This puts erb style delimeters
compiledEditFormFields = compiledEditFormFields.replace(/ERBstart/g, '<%=')
compiledEditFormFields = compiledEditFormFields.replace(/ERBend/g, '%>')
_.defaults(scope, {
compiledEditFormFields: compiledEditFormFields
});
//This generates a template using editFlash.template
var EDIT_FLASH_TEMPLATE = path.resolve(__dirname, './templates/editFlash.template');
EDIT_FLASH_TEMPLATE = fs.readFileSync(EDIT_FLASH_TEMPLATE, 'utf8');
var compiledEditFlash = _.template(EDIT_FLASH_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName
})
// This puts erb style delimeters
compiledEditFlash = compiledEditFlash.replace(/ERBstart/g, '<%')
compiledEditFlash = compiledEditFlash.replace(/ERBend/g, '%>')
// Add the compiled show form fields to the scope
_.defaults(scope, {
compiledEditFlash: compiledEditFlash
});
// This generates the editFormAction template
var EDIT_FORM_ACTION_TEMPLATE = path.resolve(__dirname, './templates/editFormAction.template');
EDIT_FORM_ACTION_TEMPLATE = fs.readFileSync(EDIT_FORM_ACTION_TEMPLATE, 'utf8');
var compiledEditFormAction = _.template(EDIT_FORM_ACTION_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
modelControllerName: scope.modelControllerName
});
// This puts erb style delimeters
compiledEditFormAction = compiledEditFormAction.replace(/ERBstart/g, '<%=')
compiledEditFormAction = compiledEditFormAction.replace(/ERBend/g, '%>')
_.defaults(scope, {
compiledEditFormAction: compiledEditFormAction
});
// This generates a template using the editShowLink.template located in scaffold/templates
var EDIT_SHOW_LINK_TEMPLATE = path.resolve(__dirname, './templates/editShowLink.template');
EDIT_SHOW_LINK_TEMPLATE = fs.readFileSync(EDIT_SHOW_LINK_TEMPLATE, 'utf8');
var compiledEditShowLink = _.template(EDIT_SHOW_LINK_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName
})
// This puts erb style delimeters
compiledEditShowLink = compiledEditShowLink.replace(/ERBstart/g, '<%=')
compiledEditShowLink = compiledEditShowLink.replace(/ERBend/g, '%>')
_.defaults(scope, {
compiledEditShowLink: compiledEditShowLink
});
/**
_ _ _______ _ _
| | (_) |__ __| | | | |
__ _ ___| |_ _ ___ _ __ | | ___ _ __ ___ _ __ | | __ _| |_ ___
/ _` |/ __| __| |/ _ \| '_ \| |/ _ \ '_ ` _ \| '_ \| |/ _` | __/ _ \
| (_| | (__| |_| | (_) | | | | | __/ | | | | | |_) | | (_| | || __/
\__,_|\___|\__|_|\___/|_| |_|_|\___|_| |_| |_| .__/|_|\__,_|\__\___|
| |
|_|
**/
// This generates a template using the actionParamObject.template located in scaffold/templates
// to produce a the params to include.
var ACTION_PARAM_OBJECT_TEMPLATE = path.resolve(__dirname, './templates/actionParamObject.template');
ACTION_PARAM_OBJECT_TEMPLATE = fs.readFileSync(ACTION_PARAM_OBJECT_TEMPLATE, 'utf8');
var compliledActionParamObject = _.template(ACTION_PARAM_OBJECT_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName
})
// This generates a template using the actionUpdateParamObject.template
var ACTION_UPDATE_PARAM_OBJECT_TEMPLATE = path.resolve(__dirname, './templates/actionUpdateParamObject.template');
ACTION_UPDATE_PARAM_OBJECT_TEMPLATE = fs.readFileSync(ACTION_UPDATE_PARAM_OBJECT_TEMPLATE, 'utf8');
var compliledActionUpdateParamObject = _.template(ACTION_UPDATE_PARAM_OBJECT_TEMPLATE, {
modelAttributeNames: scope.modelAttributeNames,
id: scope.id,
modelControllerName: scope.modelControllerName
})
// This generates a template using the action.template located in scaffold/templates
// combined with CRUD actions to produce a controller.
var ACTION_TEMPLATE = path.resolve(__dirname, './templates/action.template');
ACTION_TEMPLATE = fs.readFileSync(ACTION_TEMPLATE, 'utf8');
var compliledActions = _.template(ACTION_TEMPLATE, {
compliledActionParamObject: compliledActionParamObject,
compliledActionUpdateParamObject: compliledActionUpdateParamObject,
id: scope.id,
modelControllerName: scope.modelControllerName,
})
scope.actionFns = [compliledActions]
// This generates a template using the homePageEJS.template located in scaffold/templates
var HOMEPAGE_EJS_TEMPLATE = path.resolve(__dirname, './templates/homePageEJS.template');
HOMEPAGE_EJS_TEMPLATE = fs.readFileSync(HOMEPAGE_EJS_TEMPLATE, 'utf8');
var compliledHomePageEJS = _.template(HOMEPAGE_EJS_TEMPLATE, {});
// This puts erb style delimeters
compliledHomePageEJS = compliledHomePageEJS.replace(/ERBstart=/g, '<%=')
compliledHomePageEJS = compliledHomePageEJS.replace(/ERBstart/g, '<%')
compliledHomePageEJS = compliledHomePageEJS.replace(/ERBend/g, '%>')
_.defaults(scope, {
compliledHomePageEJS: compliledHomePageEJS
});
// When finished, we trigger a callback with no error
// to begin generating files/folders as specified by
// the `targets` below.
cb();
},
/**
* The files/folders to generate.
* @type {Object}
*/
targets: {
// Usage:
// './path/to/destination.foo': { someHelper: opts }
// Build up the model and controller files
'./': ['model', 'controller'],
'./assets/styles/custom.css': {template: {templatePath: 'customCSS.template', force: true } },
'./assets/styles/bootstrapScaffold.css': {template: {templatePath: 'bootstrapScaffoldCSS.template', force: true } },
'./views/homepage.ejs': {template: {templatePath: 'homePage.template', force: true } },
'./views/:id/new.ejs': {template: {templatePath: 'new.template', force: true } },
'./views/:id/show.ejs': {template: {templatePath: 'show.template', force: true } },
'./views/:id/index.ejs': {template: {templatePath: 'index.template', force: true } },
'./views/:id/edit.ejs': {template: {templatePath: 'edit.template', force: true } },
'./api/policies/flash.js': {template: {templatePath: 'flashPolicy.template', force: true} },
'./config/scaffold-policies.js': {template: {templatePath: 'policies.template', force: true} }
},
/**
* The absolute path to the `templates` for this generator
* (for use with the `template` helper)
*
* @type {String}
*/
templatesDirectory: require('path').resolve(__dirname, './templates')
};
/**
* INVALID_SCOPE_VARIABLE()
*
* Helper method to put together a nice error about a missing or invalid
* scope variable. We should always validate any required scope variables
* to avoid inadvertently smashing someone's filesystem.
*
* @param {String} varname [the name of the missing/invalid scope variable]
* @param {String} details [optional - additional details to display on the console]
* @param {String} message [optional - override for the default message]
* @return {Error}
* @api private
*/
function INVALID_SCOPE_VARIABLE (varname, details, message) {
var DEFAULT_MESSAGE =
'Issue encountered in generator "scaffold":\n'+
'Missing required scope variable: `%s`"\n' +
'If you are the author of `sails-generate-scaffold`, please resolve this '+
'issue and publish a new patch release.';
message = (message || DEFAULT_MESSAGE) + (details ? '\n'+details : '');
message = util.inspect(message, varname);
return new Error(message);
}