Skip to content

Commit 6bc4dbe

Browse files
author
Cristian Escudero
committed
Dynamically change the routes.js when a new controller is generated
Summary: 1. Created initial handling of the view route. 2. Added a working updater of the routes.js 3. Added some handling if the routes.js file doesnt exists. Test Plan: Checked by creating my own test controllers using the generator Reviewers: jmsotuyo Reviewed By: jmsotuyo Differential Revision: http://ph.monits.com/D12382
1 parent f84429d commit 6bc4dbe

File tree

4 files changed

+175
-33
lines changed

4 files changed

+175
-33
lines changed

app/templates/scripts/_routes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
'use strict';
2+
13
define([], function() {
2-
'use strict';
34
return {
45
defaultRoutePath: '/',
56
routes: {
67
'/': {
78
templateUrl: '/views/home.html',
89
controller: 'HomeCtrl'
910
}
11+
/* ===== yeoman hook ===== */
12+
/* Do not remove these commented lines! Needed for auto-generation */
1013
}
1114
};
1215
});
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
'use strict';
12
define([], function() {
2-
'use strict';
33

44
return {
5-
WELCOME_MESSAGE: 'Always a pleasure scaffolding your apps'
5+
WELCOME_MESSAGE: 'Always a pleasure scaffolding your apps',
6+
7+
WELCOME_CONTROLLER: 'A new and shiny controller has been made!',
8+
WELCOME_CONTROLLER_FOLLOWUP: 'This is the default view for your controlller. Change it (if you want, of course)!'
69
};
710
});

controller/index.js

Lines changed: 154 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,168 @@ module.exports = yeoman.generators.Base.extend({
1212
yeoman.generators.Base.apply(this, arguments);
1313
},
1414

15-
prompting: function () {
16-
var done = this.async();
17-
18-
// Have Yeoman greet the user.
19-
this.log(yosay(
20-
'Welcome to the brilliant ' + chalk.red('AngularRequireFullstack') + ' generator! by ' + chalk.blue('MONITS')
21-
));
22-
23-
this.log(
24-
'I am going to scaffold a controller for you.'
25-
);
26-
27-
var prompts = [{
28-
type: 'input',
29-
name: 'controllerName',
30-
message: 'Controller name:',
31-
default: this.appname
32-
}];
33-
34-
this.prompt(prompts, function (props) {
35-
this.props = props;
36-
37-
this.controllerName = this.props.controllerName;
38-
// To access props later use this.props.someOption;
39-
40-
done();
41-
}.bind(this));
15+
prompting: {
16+
17+
// First we ask for the controller's name
18+
promptingForName: function() {
19+
20+
var done = this.async();
21+
22+
// Have Yeoman greet the user.
23+
this.log(yosay(
24+
'Welcome to the brilliant ' + chalk.red('AngularRequireFullstack') + ' generator! by ' + chalk.blue('MONITS')
25+
));
26+
27+
this.log(
28+
'I am going to scaffold a controller for you.'
29+
);
30+
31+
var prompts = [{
32+
type: 'input',
33+
name: 'controllerName',
34+
message: 'Controller name:',
35+
default: this.appname
36+
}];
37+
38+
this.prompt(prompts, function (props) {
39+
this.props = props;
40+
41+
this.controllerName = this.props.controllerName;
42+
// To access props later use this.props.someOption;
43+
44+
done();
45+
}.bind(this));
46+
47+
},
48+
49+
// We need to ask the user for the route of this controller before procceding
50+
promptingForRoute: function() {
51+
52+
var done = this.async();
53+
54+
this.prompt({
55+
type: 'input',
56+
name: 'controllerViewRoute',
57+
message: 'The route for the view in this controller',
58+
validate: function (routeName) {
59+
if (routeName.length > 0) {
60+
return true;
61+
}
62+
63+
return 'You need to enter a valid path';
64+
}
65+
66+
}, function (answers) {
67+
68+
var tempRoute = answers.controllerViewRoute.trim();
69+
70+
// Lets add slashes at the front and remove the slash from the back of the route (if there arent)
71+
if (tempRoute[0] !== '/') {
72+
tempRoute = '/' + tempRoute;
73+
}
74+
75+
if (tempRoute[tempRoute.length - 1] === '/') {
76+
tempRoute = tempRoute.substr(0, tempRoute.length - 1);
77+
}
78+
79+
this.controllerViewRoute = tempRoute;
80+
this.controllerViewRoutePartial = '/views' + this.controllerViewRoute + '/' + this.controllerName + '.html';
81+
this.controllerViewRouteFull = '/app' + this.controllerViewRoutePartial;
82+
83+
this.log(
84+
chalk.gray(' (btw, your default view will be located in: ' + chalk.bold(this.controllerViewRouteFull) + ')')
85+
);
86+
87+
done();
88+
}.bind(this));
89+
90+
}
91+
4292
},
4393

4494
writing: {
45-
controller: function () {
46-
//Controllers
95+
controller: function () {
96+
// Controllers
4797
this.fs.copyTpl(
4898
this.templatePath('/_controllerTemplate.js'),
4999
this.destinationPath('/app/scripts/controllers/' + this.controllerName + '.js'),
50100
this
51101
);
52-
102+
103+
// Controller View Route
104+
this.fs.copyTpl(
105+
this.templatePath('/views/_viewControllerTemplate.html'),
106+
this.destinationPath(this.controllerViewRouteFull),
107+
this
108+
);
109+
110+
},
111+
112+
updatingRoutesJs: function() {
113+
114+
var hook = '/* ===== yeoman hook ===== */';
115+
var path = this.destinationPath('/app/scripts/routes.js');
116+
117+
// route.js exists? If not, we can't update it
118+
if (!this.fs.exists(path)) {
119+
this.log(
120+
chalk.red(
121+
chalk.bold(
122+
'Yikes! I can\'t find ' +
123+
chalk.underline('/app/scripts/routes.js') + '. ' +
124+
'Thus I can\'t make my splendid update :(\n'
125+
)
126+
)
127+
);
128+
129+
return;
130+
}
131+
132+
// If exists, we read the file
133+
var file = this.fs.read(path);
134+
135+
// We need a healthy hook
136+
if (file.indexOf(hook) === -1) {
137+
this.log(
138+
chalk.red('Oops... I couldn\'t update routes.js because I can\'t find my hook :(')
139+
);
140+
141+
this.log(
142+
chalk.gray(' (try adding \'' + hook + '\' after the last item inside the routes object in routes.js)') + '\n'
143+
);
144+
145+
return;
146+
}
147+
148+
// If the key doesnt exist, we add our view to routes.js
149+
if (file.indexOf(this.controllerViewRoute) === -1) {
150+
151+
var tab = ' ';
152+
153+
// We want a block with the following format:
154+
// 'controllerViewRoute': {
155+
// templateUrl: 'controllerViewRoutePartial',
156+
// controller: 'controllerName',
157+
// }
158+
// /* ===== yeoman hook ===== */
159+
//
160+
// (the last line -hook- is added once again to enable further uses)
161+
var newBlockToAppend =
162+
'\'' + this.controllerViewRoute + '\': {\n' +
163+
tab + tab + tab + tab + 'templateUrl: \'' + this.controllerViewRoutePartial + '\',\n' +
164+
tab + tab + tab + tab + 'controller: \'' + this.controllerName + '\'\n' +
165+
tab + tab + tab + '}\n' +
166+
tab + tab + tab + hook;
167+
168+
// Let's add a comma in the previous block before adding a new one
169+
var positionOfLastParenthesis = file.lastIndexOf('}', file.indexOf(hook));
170+
file = file.substr(0, positionOfLastParenthesis + 1) + ',' + file.substr(positionOfLastParenthesis + 1);
171+
172+
// And we replace the content with the new block
173+
this.fs.write(path, file.replace(hook, newBlockToAppend));
174+
175+
}
176+
53177
}
54178
},
55179

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<div class="jumbotron">
2+
<h1>'Allo, 'Allo!</h1>
3+
<p class="lead">
4+
<img src="images/yeoman.png" alt="I'm Yeoman"><br>
5+
{{'WELCOME_CONTROLLER' | translate}}
6+
</p>
7+
<p><a class="btn btn-lg btn-success" ng-href="#">Incredible!<span class="glyphicon glyphicon-ok"></span></a></p>
8+
</div>
9+
10+
<div class="row marketing">
11+
<p>{{'WELCOME_CONTROLLER_FOLLOWUP' | translate}}</p>
12+
</div>

0 commit comments

Comments
 (0)