-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
549 lines (460 loc) · 17.7 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
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
'use strict';
var fs = require('fs');
var path = require('path');
var util = require('util');
var angularUtils = require('../util.js');
var yeoman = require('yeoman-generator');
var chalk = require('chalk');
var wiredep = require('wiredep');
var Generator = module.exports = function Generator(args, options) {
yeoman.generators.Base.apply(this, arguments);
this.argument('appname', { type: String, required: false });
this.appname = this.appname || 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 + angularUtils.appName(this);
args = ['main'];
if (typeof this.env.options.appPath === 'undefined') {
try {
this.env.options.appPath = require(path.join(process.cwd(), 'bower.json')).appPath;
} catch (e) {}
this.env.options.appPath = this.env.options.appPath || 'app';
}
this.appPath = this.env.options.appPath;
if (typeof this.env.options.coffee === 'undefined') {
this.option('coffee', {
desc: 'Generate CoffeeScript instead of JavaScript'
});
// attempt to detect if user is using CS or not
// if cml arg provided, use that; else look for the existence of cs
if (!this.options.coffee &&
this.expandFiles(path.join(this.appPath, '/scripts/**/*.coffee'), {}).length > 0) {
this.options.coffee = true;
}
this.env.options.coffee = this.options.coffee;
}
if (typeof this.env.options.minsafe === 'undefined') {
this.option('minsafe', {
desc: 'Generate AngularJS minification safe code'
});
this.env.options.minsafe = this.options.minsafe;
args.push('--minsafe');
}
if (typeof this.env.options.jade === 'undefined') {
this.option('jade', {
desc: 'Generate views using Jade templates'
});
// attempt to detect if user is using jade or not
// if cml arg provided, use that; else look for the existence of cs
if (!this.options.coffee &&
this.expandFiles(path.join(this.appPath, '/views/**/*.jade'), {}).length > 0) {
this.options.jade = true;
}
this.env.options.jade = this.options.jade;
}
this.hookFor('angular-fullstack:common', {
args: args
});
this.hookFor('angular-fullstack:main', {
args: args
});
this.hookFor('angular-fullstack:controller', {
args: args
});
this.on('end', function () {
this.installDependencies({
skipInstall: this.options['skip-install'],
callback: this._injectDependencies.bind(this)
});
var enabledComponents = [];
if (this.resourceModule) {
enabledComponents.push('angular-resource/angular-resource.js');
}
if (this.cookiesModule) {
enabledComponents.push('angular-cookies/angular-cookies.js');
}
if (this.sanitizeModule) {
enabledComponents.push('angular-sanitize/angular-sanitize.js');
}
if (this.routeModule) {
enabledComponents.push('angular-route/angular-route.js');
}
this.invoke('karma:app', {
options: {
coffee: this.options.coffee,
testPath: 'test/client',
travis: true,
'skip-install': true,
components: [
'angular/angular.js',
'angular-mocks/angular-mocks.js'
].concat(enabledComponents)
}
});
});
this.pkg = require('../package.json');
};
util.inherits(Generator, yeoman.generators.Base);
Generator.prototype.welcome = function welcome() {
// welcome message
if (!this.options['skip-welcome-message']) {
console.log(this.yeoman);
console.log(
'Out of the box I include Bootstrap and some AngularJS recommended modules.\n'
);
// Deprecation notice for minsafe
if (this.options.minsafe) {
console.warn(
'\n** The --minsafe flag is being deprecated in 0.7.0 and removed in ' +
'0.8.0. For more information, see ' +
'https://github.com/yeoman/generator-angular#minification-safe. **\n'
);
}
}
};
Generator.prototype.askForCompass = function askForCompass() {
var cb = this.async();
this.prompt([{
type: 'confirm',
name: 'compass',
message: 'Would you like to use Sass (with Compass)?',
default: true
}], function (props) {
this.compass = props.compass;
cb();
}.bind(this));
};
Generator.prototype.askForBootstrap = function askForBootstrap() {
var compass = this.compass;
var cb = this.async();
this.prompt([{
type: 'confirm',
name: 'bootstrap',
message: 'Would you like to include Twitter Bootstrap?',
default: true
}, {
type: 'confirm',
name: 'compassBootstrap',
message: 'Would you like to use the Sass version of Twitter Bootstrap?',
default: true,
when: function (props) {
return props.bootstrap && compass;
}
}], function (props) {
this.bootstrap = props.bootstrap;
this.compassBootstrap = props.compassBootstrap;
cb();
}.bind(this));
};
Generator.prototype.askForModules = function askForModules() {
var cb = this.async();
var prompts = [{
type: 'checkbox',
name: 'modules',
message: 'Which modules would you like to include?',
choices: [{
value: 'resourceModule',
name: 'angular-resource.js',
checked: true
}, {
value: 'cookiesModule',
name: 'angular-cookies.js',
checked: true
}, {
value: 'sanitizeModule',
name: 'angular-sanitize.js',
checked: true
}, {
value: 'routeModule',
name: 'angular-route.js',
checked: true
}]
}];
this.prompt(prompts, function (props) {
var hasMod = function (mod) { return props.modules.indexOf(mod) !== -1; };
this.resourceModule = hasMod('resourceModule');
this.cookiesModule = hasMod('cookiesModule');
this.sanitizeModule = hasMod('sanitizeModule');
this.routeModule = hasMod('routeModule');
var angMods = [];
if (this.cookiesModule) {
angMods.push("'ngCookies'");
}
if (this.resourceModule) {
angMods.push("'ngResource'");
}
if (this.sanitizeModule) {
angMods.push("'ngSanitize'");
}
if (this.routeModule) {
angMods.push("'ngRoute'");
this.env.options.ngRoute = true;
}
if (angMods.length) {
this.env.options.angularDeps = "\n " + angMods.join(",\n ") +"\n";
}
cb();
}.bind(this));
};
Generator.prototype.askForMongo = function askForMongo() {
var cb = this.async();
this.prompt([{
type: 'confirm',
name: 'mongo',
message: 'Would you like to include MongoDB with Mongoose?',
default: false
}, {
type: 'confirm',
name: 'mongoPassportUser',
message: 'Would you like to include a Passport authentication boilerplate?',
default: false,
when: function (props) {
return props.mongo;
}
}], function (props) {
this.mongo = props.mongo;
this.mongoPassportUser = props.mongoPassportUser;
cb();
}.bind(this));
};
Generator.prototype.readIndex = function readIndex() {
this.ngRoute = this.env.options.ngRoute;
this.jade = this.env.options.jade;
if(this.jade) {
this.indexFile = this.engine(this.read('../../templates/views/jade/index.jade'), this);
} else {
this.indexFile = this.engine(this.read('../../templates/views/html/index.html'), this);
}
};
Generator.prototype.bootstrapFiles = function bootstrapFiles() {
var sass = this.compass;
var mainFile = 'main.' + (sass ? 's' : '') + 'css';
if (this.bootstrap && !sass) {
this.copy('fonts/glyphicons-halflings-regular.eot', 'app/fonts/glyphicons-halflings-regular.eot');
this.copy('fonts/glyphicons-halflings-regular.ttf', 'app/fonts/glyphicons-halflings-regular.ttf');
this.copy('fonts/glyphicons-halflings-regular.svg', 'app/fonts/glyphicons-halflings-regular.svg');
this.copy('fonts/glyphicons-halflings-regular.woff', 'app/fonts/glyphicons-halflings-regular.woff');
}
this.copy('styles/' + mainFile, 'app/styles/' + mainFile);
};
function generateJadeBlock(blockType, optimizedPath, filesBlock, searchPath, prefix) {
var blockStart, blockEnd;
var blockSearchPath = '';
if (searchPath !== undefined) {
if (util.isArray(searchPath)) {
searchPath = '{' + searchPath.join(',') + '}';
}
blockSearchPath = '(' + searchPath + ')';
}
blockStart = '\n' + prefix + '<!-- build:' + blockType + blockSearchPath + ' ' + optimizedPath + ' -->\n';
blockEnd = prefix + '<!-- endbuild -->\n' + prefix;
return blockStart + filesBlock + blockEnd;
}
function appendJade(jade, tag, blocks){
var mark = "//- build:" + tag,
position = jade.indexOf(mark);
return [jade.slice(0, position), blocks, jade.slice(position)].join('');
}
function appendFilesToJade(jadeOrOptions, fileType, optimizedPath, sourceFileList, attrs, searchPath) {
var blocks, updatedContent,
jade = jadeOrOptions,
prefix = ' ',
files = '';
if (typeof jadeOrOptions === 'object') {
jade = jadeOrOptions.html;
fileType = jadeOrOptions.fileType;
optimizedPath = jadeOrOptions.optimizedPath;
sourceFileList = jadeOrOptions.sourceFileList;
attrs = jadeOrOptions.attrs;
searchPath = jadeOrOptions.searchPath;
}
if (fileType === 'js') {
sourceFileList.forEach(function (el) {
files += prefix + '<script ' + (attrs||'') + 'src="' + el + '"></script>\n';
});
blocks = generateJadeBlock('js', optimizedPath, files, searchPath, prefix);
updatedContent = appendJade(jade, 'body', blocks);
} else if (fileType === 'css') {
sourceFileList.forEach(function (el) {
files += prefix + '<link ' + (attrs||'') + 'rel="stylesheet" href="' + el + '">\n';
});
blocks = generateJadeBlock('css', optimizedPath, files, searchPath, prefix);
updatedContent = appendJade(jade, 'head', blocks);
}
return updatedContent;
}
var copyScriptWithEnvOptions = function copyScriptWithEnvOptions(that, fileToCopy, destinationFolder) {
var ext = 'js',
minsafe = '',
sourceFolder = 'javascript';
if(that.env.options.coffee) {
ext = 'coffee';
sourceFolder = 'coffeescript';
}
if(that.env.options.minsafe) {
minsafe = '-min';
}
that.copy('../../templates/' + sourceFolder + minsafe + '/' + fileToCopy + '.' + ext, destinationFolder + fileToCopy + '.' + ext);
};
Generator.prototype.navBarScript = function navBarScript() {
copyScriptWithEnvOptions(this, 'controllers/navbar', 'app/scripts/');
};
Generator.prototype.appJs = function appJs() {
var appendOptions = {
html: this.indexFile,
fileType: 'js',
optimizedPath: 'scripts/scripts.js',
sourceFileList: ['scripts/app.js', 'scripts/controllers/main.js', 'scripts/controllers/navbar.js'],
searchPath: ['.tmp', 'app']
};
// only reference authentication controllers when required
if (this.mongoPassportUser) {
appendOptions.sourceFileList.push('scripts/controllers/login.js');
appendOptions.sourceFileList.push('scripts/controllers/signup.js');
appendOptions.sourceFileList.push('scripts/controllers/settings.js');
appendOptions.sourceFileList.push('scripts/services/auth.js');
appendOptions.sourceFileList.push('scripts/services/session.js');
appendOptions.sourceFileList.push('scripts/services/user.js');
appendOptions.sourceFileList.push('scripts/directives/mongooseError.js');
}
if (this.jade) {
this.indexFile = appendFilesToJade(appendOptions);
} else {
this.indexFile = this.appendFiles(appendOptions);
}
};
Generator.prototype.createIndex = function createIndex() {
this.indexFile = this.indexFile.replace(/'/g, "'");
if (this.jade) {
this.write(path.join(this.appPath, 'views', 'index.jade'), this.indexFile);
} else {
this.write(path.join(this.appPath, 'views', 'index.html'), this.indexFile);
}
};
Generator.prototype.addJadeViews = function addHtmlJade() {
if(this.jade) {
this.copy('../../templates/views/jade/partials/main.jade', 'app/views/partials/main.jade');
this.copy('../../templates/views/jade/partials/navbar.jade', 'app/views/partials/navbar.jade');
if(this.mongoPassportUser) {
this.copy('../../templates/views/jade/partials/login.jade', 'app/views/partials/login.jade');
this.copy('../../templates/views/jade/partials/signup.jade', 'app/views/partials/signup.jade');
this.copy('../../templates/views/jade/partials/settings.jade', 'app/views/partials/settings.jade');
}
this.copy('../../templates/views/jade/404.jade', 'app/views/404.jade');
}
};
Generator.prototype.addHtmlViews = function addHtmlViews() {
if(!this.jade) {
this.copy('../../templates/views/html/partials/main.html', 'app/views/partials/main.html');
this.copy('../../templates/views/html/partials/navbar.html', 'app/views/partials/navbar.html');
if(this.mongoPassportUser) {
this.copy('../../templates/views/html/partials/login.html', 'app/views/partials/login.html');
this.copy('../../templates/views/html/partials/signup.html', 'app/views/partials/signup.html');
this.copy('../../templates/views/html/partials/settings.html', 'app/views/partials/settings.html');
}
this.copy('../../templates/views/html/404.html', 'app/views/404.html');
}
};
Generator.prototype.packageFiles = function () {
this.coffee = this.env.options.coffee;
this.jade = this.env.options.jade;
this.template('../../templates/common/_bower.json', 'bower.json');
this.template('../../templates/common/_package.json', 'package.json');
if (this.coffee) {
this.template('../../templates/common/Gruntfile.coffee', 'Gruntfile.coffee');
} else {
this.template('../../templates/common/Gruntfile.js', 'Gruntfile.js');
}
};
Generator.prototype.imageFiles = function () {
this.sourceRoot(path.join(__dirname, 'templates'));
this.directory('images', 'app/images', true);
};
Generator.prototype._injectDependencies = function _injectDependencies() {
var howToInstall =
'\nAfter running `npm install & bower install`, inject your front end dependencies into' +
'\nyour HTML by running:' +
'\n' +
chalk.yellow.bold('\n grunt bower-install');
var wireDepConfig = {
directory: 'app/bower_components',
bowerJson: JSON.parse(fs.readFileSync('./bower.json')),
ignorePath: 'app/',
htmlFile: 'app/views/index.html',
cssPattern: '<link rel="stylesheet" href="{{filePath}}">'
};
if (this.jade) {
wireDepConfig.htmlFile = 'app/views/index.jade';
}
if (this.compass && this.bootstrap) {
wireDepConfig.exclude = ['sass-bootstrap'];
}
if (this.options['skip-install']) {
console.log(howToInstall);
} else {
wiredep(wireDepConfig);
}
};
Generator.prototype.serverFiles = function () {
var sourceRoot = '../../templates/express',
scriptSuffix = '.js';
if (this.env.options.coffee) {
sourceRoot = '../../templates/express-coffeescript';
scriptSuffix = '.coffee';
}
if (!this.env.options.coffee) {
this.copy(sourceRoot + '/jshintrc', 'lib/.jshintrc');
}
this.template(sourceRoot + '/server' + scriptSuffix, 'server' + scriptSuffix);
this.template(sourceRoot + '/controllers/api' + scriptSuffix, 'lib/controllers/api' + scriptSuffix);
this.template(sourceRoot + '/controllers/index' + scriptSuffix, 'lib/controllers/index' + scriptSuffix);
this.template(sourceRoot + '/routes' + scriptSuffix, 'lib/routes' + scriptSuffix);
this.template(sourceRoot + '/test/thing/api' + scriptSuffix, 'test/server/thing/api' + scriptSuffix);
this.template(sourceRoot + '/config/express' + scriptSuffix, 'lib/config/express' + scriptSuffix);
this.template(sourceRoot + '/config/config' + scriptSuffix, 'lib/config/config' + scriptSuffix);
this.template(sourceRoot + '/config/env/all' + scriptSuffix, 'lib/config/env/all' + scriptSuffix);
this.template(sourceRoot + '/config/env/development' + scriptSuffix, 'lib/config/env/development' + scriptSuffix);
this.template(sourceRoot + '/config/env/production' + scriptSuffix, 'lib/config/env/production' + scriptSuffix);
this.template(sourceRoot + '/config/env/test' + scriptSuffix, 'lib/config/env/test' + scriptSuffix);
};
Generator.prototype.mongoFiles = function () {
var sourceRoot = '../../templates/express',
scriptSuffix = '.js';
if (this.env.options.coffee) {
sourceRoot = '../../templates/express-coffeescript';
scriptSuffix = '.coffee';
}
if (!this.mongo) {
return; // Skip if disabled.
}
this.env.options.mongo = this.mongo;
this.template(sourceRoot + '/config/dummydata' + scriptSuffix, 'lib/config/dummydata' + scriptSuffix);
this.template(sourceRoot + '/models/thing' + scriptSuffix, 'lib/models/thing' + scriptSuffix);
if(!this.mongoPassportUser) {
return; // Skip if disabled.
}
this.env.options.mongoPassportUser = this.mongoPassportUser;
// frontend
copyScriptWithEnvOptions(this, 'controllers/login', 'app/scripts/');
copyScriptWithEnvOptions(this, 'controllers/signup', 'app/scripts/');
copyScriptWithEnvOptions(this, 'controllers/settings', 'app/scripts/');
copyScriptWithEnvOptions(this, 'services/auth', 'app/scripts/');
copyScriptWithEnvOptions(this, 'services/session', 'app/scripts/');
copyScriptWithEnvOptions(this, 'services/user', 'app/scripts/');
copyScriptWithEnvOptions(this, 'directives/mongooseError', 'app/scripts/');
// middleware
this.template(sourceRoot + '/middleware' + scriptSuffix, 'lib/middleware' + scriptSuffix);
// config
this.template(sourceRoot + '/config/passport' + scriptSuffix, 'lib/config/passport' + scriptSuffix);
// models
this.template(sourceRoot + '/models/user' + scriptSuffix, 'lib/models/user' + scriptSuffix);
// controllers
this.template(sourceRoot + '/controllers/session' + scriptSuffix, 'lib/controllers/session' + scriptSuffix);
this.template(sourceRoot + '/controllers/users' + scriptSuffix, 'lib/controllers/users' + scriptSuffix);
// tests
this.template(sourceRoot + '/test/user/model' + scriptSuffix, 'test/server/user/model' + scriptSuffix);
};