/
index.js
87 lines (74 loc) · 3.14 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
'use strict';
var chalk = require('chalk');
var changeCase = require('change-case');
var helpers = require('../../lib/helpers');
var _ = require('lodash');
var path = require('path');
module.exports = helpers.NamedBase.extend({
// The name `constructor` is important here
constructor: function () {
// Calling the super constructor is important so our generator is correctly set up
helpers.NamedBase.apply(this, arguments);
this.choices = {
name: changeCase.camelCase(this.name),
nameParamCase: changeCase.paramCase(this.name),
namePascalCase: changeCase.pascalCase(this.name)
};
},
prompting: function () {
this.log('Creating a controller: ' + chalk.green(this.name) + '...');
this.choices.moduleName = changeCase.camelCase(this.options.moduleName);
var done = this.async();
var customModules = _.filter(helpers.getAllAngularModules(this.config.get('clientSideFolder') + this.config.get('appSubFolder') + 'app.module.js'), function (m) {
return _.startsWith(m, this.config.get('appName') + '.') && m !== (this.config.get('appName') + '.core');
}, this).sort();
if (!customModules.length) {
this.env.error(chalk.red("There are no features in this app. Please generate a feature before generating a controller. \nFor example, 'ae feature myFeature'."));
} else {
var prompts = [
{
type: 'list',
name: 'moduleName',
message: 'Which module does this controller belong in?',
choices: customModules
}];
this.prompt(prompts, function (choices) {
this.choices.moduleName = choices.moduleName.replace(this.config.get('appName') + '.', '');
this.choices.moduleNameParamCase = changeCase.paramCase(this.choices.moduleName);
var routeFilePrefix = this.destinationPath(
this.config.get('clientSideFolder') + this.config.get('appSubFolder')
+ changeCase.paramCase(this.choices.moduleName) + '/'
);
var fileType = ((this.config.get('uirouter') && 'state') || 'route');
this.injectTemplatePartial(
this,
this.templatePath('partial.' + fileType + '.js'),
'INJECT:ROUTES',
routeFilePrefix + changeCase.paramCase(this.choices.moduleName) + '.' + fileType + '.js'
);
this.injectTemplatePartial(
this,
this.templatePath('partial.' + fileType + '.spec.js'),
'INJECT:ROUTE_TESTS',
routeFilePrefix + changeCase.paramCase(this.choices.moduleName) + '.' + fileType + '.spec.js'
);
this.injectTemplatePartial(
this,
this.templatePath('partial.route-view.js'),
'INJECT:ROUTE_TEST_TEMPLATES',
routeFilePrefix + changeCase.paramCase(this.choices.moduleName) + '.' + fileType + '.spec.js'
);
done();
}.bind(this));
}
},
writing: {
files: function () {
this.installTemplateFolder({
generator: this,
destination: path.join(this.config.get('clientSideFolder'), this.config.get('appSubFolder'), this.choices.moduleNameParamCase),
fileMacros: {NAME: this.choices.nameParamCase}
});
}
}
});