@@ -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
0 commit comments