This repository was archived by the owner on Mar 26, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathindex.js
398 lines (340 loc) · 10.3 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
'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 yosay = require('yosay');
var wiredep = require('wiredep');
var chalk = require('chalk');
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
});
this.env.options['app-suffix'] = this.options['app-suffix'];
this.scriptAppName = this.appname + angularUtils.appName(this);
args = ['main'];
if (typeof this.env.options.appPath === 'undefined') {
this.option('appPath', {
desc: 'Allow to choose where to write the files'
});
this.env.options.appPath = this.options.appPath;
if (!this.env.options.appPath) {
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.options.appPath = this.env.options.appPath;
}
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.typescript === 'undefined') {
this.option('typescript', {
desc: 'Generate TypeScript instead of JavaScript'
});
// attempt to detect if user is using TS or not
// if cml arg provided, use that; else look for the existence of ts
if (!this.options.typescript &&
this.expandFiles(path.join(this.appPath, '/scripts/**/*.ts'), {}).length > 0) {
this.options.typescript = true;
}
this.env.options.typescript = this.options.typescript;
}
this.hookFor('angular:common', {
args: args
});
this.hookFor('angular:main', {
args: args
});
this.hookFor('angular:controller', {
args: args
});
this.on('end', function () {
var jsExt = this.options.coffee ? 'coffee' : 'js';
var bowerComments = [
'bower:js',
'endbower'
];
if (this.options.coffee) {
bowerComments.push('bower:coffee');
bowerComments.push('endbower');
}
this.invoke('karma:app', {
options: {
'skip-install': this.options['skip-install'],
'base-path': '../',
'coffee': this.options.coffee,
'travis': true,
'files-comments': bowerComments.join(','),
'app-files': 'app/scripts/**/*.' + jsExt,
'test-files': [
'test/mock/**/*.' + jsExt,
'test/spec/**/*.' + jsExt
].join(','),
'bower-components-path': 'bower_components'
}
});
this.installDependencies({
skipInstall: this.options['skip-install'],
skipMessage: this.options['skip-message'],
callback: this._injectDependencies.bind(this)
});
if (this.env.options.ngRoute) {
this.invoke('angular:route', {
args: ['about']
});
}
if (this.env.options.uiRouter) {
this.invoke('angular:state', {
args: ['about']
});
}
});
this.pkg = require('../package.json');
this.sourceRoot(path.join(__dirname, '../templates/common'));
};
util.inherits(Generator, yeoman.generators.Base);
Generator.prototype.welcome = function welcome() {
if (!this.options['skip-welcome-message']) {
this.log(yosay());
this.log(
chalk.magenta(
'Out of the box I include Bootstrap and some AngularJS recommended modules.' +
'\n'
)
);
}
if (this.options.minsafe) {
this.log.error(
'The --minsafe flag has been removed. For more information, see' +
'\nhttps://github.com/yeoman/generator-angular#minification-safe.' +
'\n'
);
}
};
Generator.prototype.askForGulp = function askForGulp() {
var cb = this.async();
this.prompt([{
type: 'confirm',
name: 'gulp',
message: 'Would you like to use Gulp (experimental) instead of Grunt?',
default: false
}], function (props) {
this.gulp = props.gulp;
cb();
}.bind(this));
};
Generator.prototype.askForStyles = function askForStyles() {
var gulp = this.gulp;
var cb = this.async();
this.prompt([{
type: 'confirm',
name: 'sass',
message: 'Would you like to use Sass?',
default: true,
when: function () {
return gulp;
}
}, {
type: 'confirm',
name: 'compass',
message: 'Would you like to use Sass (with Compass)?',
default: true,
when: function () {
return !gulp;
}
}], function (props) {
this.sass = props.sass;
this.compass = props.compass;
cb();
}.bind(this));
};
Generator.prototype.askForBootstrap = function askForBootstrap() {
var compass = this.compass;
var gulp = this.gulp;
var cb = this.async();
this.prompt([{
type: 'confirm',
name: 'bootstrap',
message: 'Would you like to include Bootstrap?',
default: true
}, {
type: 'confirm',
name: 'compassBootstrap',
message: 'Would you like to use the Sass version of Bootstrap?',
default: true,
when: function (props) {
return !gulp && (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: 'animateModule',
name: 'angular-animate.js',
checked: true
}, {
value: 'ariaModule',
name: 'angular-aria.js',
checked: false
}, {
value: 'cookiesModule',
name: 'angular-cookies.js',
checked: true
}, {
value: 'resourceModule',
name: 'angular-resource.js',
checked: true
}, {
value: 'messagesModule',
name: 'angular-messages.js',
checked: false
}, {
value: 'routeModule',
name: 'angular-route.js',
checked: false
}, {
value: 'uiRouterModule',
name: 'angular-ui-router.js',
checked: true
}, {
value: 'sanitizeModule',
name: 'angular-sanitize.js',
checked: true
}, {
value: 'touchModule',
name: 'angular-touch.js',
checked: true
}
]
}];
this.prompt(prompts, function (props) {
var hasMod = function (mod) { return props.modules.indexOf(mod) !== -1; };
this.animateModule = hasMod('animateModule');
this.ariaModule = hasMod('ariaModule');
this.cookiesModule = hasMod('cookiesModule');
this.messagesModule = hasMod('messagesModule');
this.resourceModule = hasMod('resourceModule');
this.routeModule = hasMod('routeModule');
this.uiRouterModule = hasMod('uiRouterModule');
this.sanitizeModule = hasMod('sanitizeModule');
this.touchModule = hasMod('touchModule');
var angMods = [];
if (this.animateModule) {
angMods.push("'ngAnimate'");
}
if (this.ariaModule) {
angMods.push("'ngAria'");
}
if (this.cookiesModule) {
angMods.push("'ngCookies'");
}
if (this.messagesModule) {
angMods.push("'ngMessages'");
}
if (this.resourceModule) {
angMods.push("'ngResource'");
}
if (this.uiRouterModule) {
angMods.push("'ui.router'");
this.env.options.uiRouter = true;
}
if (this.routeModule) {
angMods.push("'ngRoute'");
this.env.options.ngRoute = true;
}
if (this.sanitizeModule) {
angMods.push("'ngSanitize'");
}
if (this.touchModule) {
angMods.push("'ngTouch'");
}
if (angMods.length) {
this.env.options.angularDeps = '\n ' + angMods.join(',\n ') + '\n ';
}
cb();
}.bind(this));
};
Generator.prototype.readIndex = function readIndex() {
this.ngRoute = this.env.options.ngRoute;
this.uiRouter = this.env.options.uiRouter;
this.indexFile = this.engine(this.read('app/index.html'), this);
};
Generator.prototype.bootstrapFiles = function bootstrapFiles() {
var sass = this.compass || this.sass;
var cssFile = 'styles/main.' + (sass ? 's' : '') + 'css';
this.copy(
path.join('app', cssFile),
path.join(this.appPath, cssFile)
);
};
Generator.prototype.appJs = function appJs() {
this.indexFile = this.appendFiles({
html: this.indexFile,
fileType: 'js',
optimizedPath: 'scripts/scripts.js',
sourceFileList: ['scripts/app.js', 'scripts/controllers/main.js'],
searchPath: ['.tmp', this.appPath]
});
};
Generator.prototype.createIndexHtml = function createIndexHtml() {
this.indexFile = this.indexFile.replace(/'/g, "'");
this.write(path.join(this.appPath, 'index.html'), this.indexFile);
};
Generator.prototype.packageFiles = function packageFiles() {
this.coffee = this.env.options.coffee;
this.typescript = this.env.options.typescript;
this.uiRouter = this.env.options.uiRouter;
this.template('root/_bower.json', 'bower.json');
this.template('root/_bowerrc', '.bowerrc');
this.template('root/_package.json', 'package.json');
if (this.gulp) {
this.template('root/_gulpfile.js', 'gulpfile.js');
} else {
this.template('root/_Gruntfile.js', 'Gruntfile.js');
}
if (this.typescript) {
this.template('root/_tsd.json', 'tsd.json');
}
this.template('root/README.md', 'README.md');
};
Generator.prototype._injectDependencies = function _injectDependencies() {
var taskRunner = this.gulp ? 'gulp' : 'grunt';
if (this.options['skip-install']) {
this.log(
'After running `npm install & bower install`, inject your front end dependencies' +
'\ninto your source code by running:' +
'\n' +
'\n' + chalk.yellow.bold(taskRunner + ' wiredep')
);
} else {
this.spawnCommand(taskRunner, ['wiredep']);
}
};