-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
417 lines (363 loc) · 12.1 KB
/
index.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
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var genUtils = require('../util.js');
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var AngularFullstackGenerator = yeoman.generators.Base.extend({
initializing: {
init: function () {
this.argument('name', { type: String, required: false });
this.appname = this.name || path.basename(process.cwd());
this.appname = this._.camelize(this._.slugify(this._.humanize(this.appname)));
this.option('app-suffix', {
desc: 'Allow a custom suffix to be added to the module name',
type: String,
required: 'false'
});
this.scriptAppName = this.appname + genUtils.appName(this);
this.appPath = this.env.options.appPath;
this.pkg = require('../package.json');
this.filters = {};
// dynamic assertion statement
this.does = this.is = function(foo) {
if (this.filters.should) {
return foo + '.should';
} else {
return 'expect(' + foo + ').to';
}
}.bind(this);
},
info: function () {
this.log(this.welcome);
this.log('Out of the box I create an AngularJS app with an Express server.\n');
},
checkForConfig: function() {
var cb = this.async();
if(this.config.get('filters')) {
this.prompt([{
type: 'confirm',
name: 'skipConfig',
message: 'Existing .yo-rc configuration found, would you like to use it?',
default: true,
}], function (answers) {
this.skipConfig = answers.skipConfig;
this.filters = this._.defaults(this.config.get('filters'), {
bootstrap: true,
uibootstrap: true,
jasmine: true
});
// NOTE: temp(?) fix for #403
if(typeof this.filters.oauth === 'undefined') {
var strategies = Object.keys(this.filters).filter(function(key) {
return key.match(/Auth$/) && this.filters[key];
}.bind(this));
if(strategies.length) this.filters.oauth = true;
}
this.config.set('filters', this.filters);
this.config.forceSave();
cb();
}.bind(this));
} else {
cb();
}
}
},
prompting: {
clientPrompts: function() {
if(this.skipConfig) return;
var cb = this.async();
this.log('# Client\n');
this.prompt([{
type: 'list',
name: 'script',
message: 'What would you like to write scripts with?',
choices: [ 'JavaScript', 'JavaScript + Babel', 'CoffeeScript'],
filter: function( val ) {
return {
'JavaScript': 'js',
'JavaScript + Babel': 'babel',
'CoffeeScript': 'coffee'
}[val];
}
}, {
type: 'list',
name: 'markup',
message: 'What would you like to write markup with?',
choices: ['HTML', 'Jade'],
filter: function( val ) { return val.toLowerCase(); }
}, {
type: 'list',
name: 'stylesheet',
default: 1,
message: 'What would you like to write stylesheets with?',
choices: [ 'CSS', 'Sass', 'Stylus', 'Less'],
filter: function( val ) { return val.toLowerCase(); }
}, {
type: 'list',
name: 'router',
default: 1,
message: 'What Angular router would you like to use?',
choices: [ 'ngRoute', 'uiRouter'],
filter: function( val ) { return val.toLowerCase(); }
}, {
type: 'confirm',
name: 'bootstrap',
message: 'Would you like to include Bootstrap?'
}, {
type: 'confirm',
name: 'uibootstrap',
message: 'Would you like to include UI Bootstrap?',
when: function (answers) {
return answers.bootstrap;
}
}], function (answers) {
// also set 'js' to true if using babel
if(answers.script === 'babel') { this.filters.js = true; }
this.filters[answers.script] = true;
this.filters[answers.markup] = true;
this.filters[answers.stylesheet] = true;
this.filters[answers.router] = true;
this.filters.bootstrap = !!answers.bootstrap;
this.filters.uibootstrap = !!answers.uibootstrap;
cb();
}.bind(this));
},
serverPrompts: function() {
if(this.skipConfig) return;
var cb = this.async();
var self = this;
this.log('\n# Server\n');
this.prompt([{
type: 'checkbox',
name: 'odms',
message: 'What would you like to use for data modeling?',
choices: [
{
value: 'mongoose',
name: 'Mongoose (MongoDB)',
checked: true
},
{
value: 'sequelize',
name: 'Sequelize (MySQL, SQLite, MariaDB, PostgreSQL)',
checked: false
}
]
}, {
type: 'list',
name: 'models',
message: 'What would you like to use for the default models?',
choices: [ 'Mongoose', 'Sequelize' ],
filter: function( val ) {
return val.toLowerCase();
},
when: function(answers) {
return answers.odms && answers.odms.length > 1;
}
}, {
type: 'confirm',
name: 'auth',
message: 'Would you scaffold out an authentication boilerplate?',
when: function (answers) {
return answers.odms && answers.odms.length !== 0;
}
}, {
type: 'checkbox',
name: 'oauth',
message: 'Would you like to include additional oAuth strategies?',
when: function (answers) {
return answers.auth;
},
choices: [
{
value: 'googleAuth',
name: 'Google',
checked: false
},
{
value: 'facebookAuth',
name: 'Facebook',
checked: false
},
{
value: 'twitterAuth',
name: 'Twitter',
checked: false
}
]
}, {
type: 'confirm',
name: 'socketio',
message: 'Would you like to use socket.io?',
// to-do: should not be dependent on ODMs
when: function (answers) {
return answers.odms && answers.odms.length !== 0;
},
default: true
}], function (answers) {
if(answers.socketio) this.filters.socketio = true;
if(answers.auth) this.filters.auth = true;
if(answers.odms && answers.odms.length > 0) {
var models;
if(!answers.models) {
models = answers.odms[0];
} else {
models = answers.models;
}
this.filters.models = true;
this.filters[models + 'Models'] = true;
answers.odms.forEach(function(odm) {
this.filters[odm] = true;
}.bind(this));
} else {
this.filters.noModels = true;
}
if(answers.oauth) {
if(answers.oauth.length) this.filters.oauth = true;
answers.oauth.forEach(function(oauthStrategy) {
this.filters[oauthStrategy] = true;
}.bind(this));
}
cb();
}.bind(this));
},
projectPrompts: function() {
if(this.skipConfig) return;
var cb = this.async();
var self = this;
this.log('\n# Project\n');
this.prompt([{
type: 'list',
name: 'testing',
message: 'What would you like to write tests with?',
choices: [ 'Jasmine', 'Mocha + Chai + Sinon'],
filter: function( val ) {
var filterMap = {
'Jasmine': 'jasmine',
'Mocha + Chai + Sinon': 'mocha'
};
return filterMap[val];
}
}, {
type: 'list',
name: 'chai',
message: 'What would you like to write Chai assertions with?',
choices: ['Expect', 'Should'],
filter: function( val ) {
return val.toLowerCase();
},
when: function( answers ) {
return answers.testing === 'mocha';
}
}], function (answers) {
/**
* Default to grunt until gulp support is implemented
*/
this.filters.grunt = true;
this.filters[answers.testing] = true;
if (answers.testing === 'mocha') {
this.filters.jasmine = false;
this.filters.should = false;
this.filters.expect = false;
this.filters[answers.chai] = true;
}
if (answers.testing === 'jasmine') {
this.filters.mocha = false;
this.filters.should = false;
this.filters.expect = false;
}
cb();
}.bind(this));
}
},
configuring: {
saveSettings: function() {
if(this.skipConfig) return;
this.config.set('insertRoutes', true);
this.config.set('registerRoutesFile', 'server/routes.js');
this.config.set('routesNeedle', '// Insert routes below');
this.config.set('routesBase', '/api/');
this.config.set('pluralizeRoutes', true);
this.config.set('insertSockets', true);
this.config.set('registerSocketsFile', 'server/config/socketio.js');
this.config.set('socketsNeedle', '// Insert sockets below');
this.config.set('insertModels', true);
this.config.set('registerModelsFile', 'server/sqldb/index.js');
this.config.set('modelsNeedle', '// Insert models below');
this.config.set('filters', this.filters);
this.config.forceSave();
},
ngComponent: function() {
if(this.skipConfig) return;
var appPath = 'client/app/';
var extensions = [];
var filters = [
'ngroute',
'uirouter',
'jasmine',
'mocha',
'expect',
'should'
].filter(function(v) {return this.filters[v];}, this);
if(this.filters.ngroute) filters.push('ngroute');
if(this.filters.uirouter) filters.push('uirouter');
if(this.filters.babel) extensions.push('babel');
if(this.filters.coffee) extensions.push('coffee');
if(this.filters.js) extensions.push('js');
if(this.filters.html) extensions.push('html');
if(this.filters.jade) extensions.push('jade');
if(this.filters.css) extensions.push('css');
if(this.filters.stylus) extensions.push('styl');
if(this.filters.sass) extensions.push('scss');
if(this.filters.less) extensions.push('less');
this.composeWith('ng-component', {
options: {
'routeDirectory': appPath,
'directiveDirectory': appPath,
'filterDirectory': appPath,
'serviceDirectory': appPath,
'filters': filters,
'extensions': extensions,
'basePath': 'client'
}
}, { local: require.resolve('generator-ng-component/app/index.js') });
},
ngModules: function() {
var angModules = [
"'ngCookies'",
"'ngResource'",
"'ngSanitize'"
];
if(this.filters.ngroute) angModules.push("'ngRoute'");
if(this.filters.socketio) angModules.push("'btford.socket-io'");
if(this.filters.uirouter) angModules.push("'ui.router'");
if(this.filters.uibootstrap) angModules.push("'ui.bootstrap'");
this.angularModules = '\n ' + angModules.join(',\n ') +'\n';
}
},
default: {},
writing: {
generateProject: function() {
this.sourceRoot(path.join(__dirname, './templates'));
genUtils.processDirectory(this, '.', '.');
},
generateEndpoint: function() {
var name = this.name = this.cameledName = 'thing';
this.classedName = name.charAt(0).toUpperCase() + name.slice(1);
this.route = '/api/' + name + 's';
this.sourceRoot(path.join(__dirname, '..', 'endpoint', 'templates'));
genUtils.processDirectory(this, '.', 'server/api/' + name);
}
},
install: {
installDeps: function() {
this.installDependencies({
skipInstall: this.options['skip-install']
});
}
},
end: {}
});
module.exports = AngularFullstackGenerator;