@@ -7,12 +7,59 @@ var ScriptBase = require('../script-base.js');
77
88var Generator = module . exports = function Generator ( ) {
99 ScriptBase . apply ( this , arguments ) ;
10+
11+ this . option ( 'route' , {
12+ desc : 'URL for the endpoint' ,
13+ type : String
14+ } ) ;
15+
16+ this . option ( 'models' , {
17+ desc : 'Specify which model(s) to use' ,
18+ type : String
19+ } ) ;
20+
21+ this . option ( 'endpointDirectory' , {
22+ desc : 'Parent directory for enpoints' ,
23+ type : String
24+ } ) ;
1025} ;
1126
1227util . inherits ( Generator , ScriptBase ) ;
1328
1429Generator . prototype . prompting = function askFor ( ) {
1530 var done = this . async ( ) ;
31+ var promptCb = function ( props ) {
32+ if ( props . route . charAt ( 0 ) !== '/' ) {
33+ props . route = '/' + props . route ;
34+ }
35+
36+ this . route = props . route ;
37+
38+ if ( props . models ) {
39+ delete this . filters . mongoose ;
40+ delete this . filters . mongooseModels ;
41+ delete this . filters . sequelize ;
42+ delete this . filters . sequelizeModels ;
43+
44+ this . filters [ props . models ] = true ;
45+ this . filters [ props . models + 'Models' ] = true ;
46+ }
47+ done ( ) ;
48+ } . bind ( this ) ;
49+
50+ if ( this . options . route ) {
51+ if ( this . filters . mongoose && this . filters . sequelize ) {
52+ if ( this . options . models ) {
53+ return promptCb ( this . options ) ;
54+ }
55+ } else {
56+ if ( this . filters . mongooseModels ) { this . options . models = 'mongoose' ; }
57+ else if ( this . filters . sequelizeModels ) { this . options . models = 'sequelize' ; }
58+ else { delete this . options . models ; }
59+ return promptCb ( this . options ) ;
60+ }
61+ }
62+
1663 var name = this . name ;
1764
1865 var base = this . config . get ( 'routesBase' ) || '/api/' ;
@@ -46,73 +93,58 @@ Generator.prototype.prompting = function askFor() {
4693 }
4794 ] ;
4895
49- this . prompt ( prompts , function ( props ) {
50- if ( props . route . charAt ( 0 ) !== '/' ) {
51- props . route = '/' + props . route ;
52- }
53-
54- this . route = props . route ;
55-
56- if ( props . models ) {
57- delete this . filters . mongoose ;
58- delete this . filters . mongooseModels ;
59- delete this . filters . sequelize ;
60- delete this . filters . sequelizeModels ;
96+ this . prompt ( prompts , promptCb ) ;
97+ } ;
6198
62- this . filters [ props . models ] = true ;
63- this . filters [ props . models + 'Models' ] = true ;
64- }
65- done ( ) ;
66- } . bind ( this ) ) ;
99+ Generator . prototype . configuring = function config ( ) {
100+ this . routeDest = path . join ( this . options . endpointDirectory ||
101+ this . config . get ( 'endpointDirectory' ) || 'server/api/' , this . name ) ;
67102} ;
68103
69104Generator . prototype . writing = function createFiles ( ) {
70- var dest = this . config . get ( 'endpointDirectory' ) || 'server/api/' + this . name ;
71105 this . sourceRoot ( path . join ( __dirname , './templates' ) ) ;
72- ngUtil . processDirectory ( this , '.' , dest ) ;
106+ ngUtil . processDirectory ( this , '.' , this . routeDest ) ;
73107} ;
74108
75109Generator . prototype . end = function registerEndpoint ( ) {
76110 if ( this . config . get ( 'insertRoutes' ) ) {
111+ var routesFile = this . config . get ( 'registerRoutesFile' ) ;
112+ var reqPath = this . relativeRequire ( this . routeDest , routesFile ) ;
77113 var routeConfig = {
78- file : this . config . get ( 'registerRoutesFile' ) ,
114+ file : routesFile ,
79115 needle : this . config . get ( 'routesNeedle' ) ,
80116 splicable : [
81- "app.use(\'" + this . route + "\', require(\'./api/ " + this . name + "\'));"
117+ "app.use(\'" + this . route + "\', require(\'" + reqPath + "\'));"
82118 ]
83119 } ;
84120 ngUtil . rewriteFile ( routeConfig ) ;
85121 }
86122
87- if ( this . filters . socketio ) {
88- if ( this . config . get ( 'insertSockets' ) ) {
89- var socketConfig = {
90- file : this . config . get ( 'registerSocketsFile' ) ,
91- needle : this . config . get ( 'socketsNeedle' ) ,
92- splicable : [
93- "require(\'../api/" + this . name + '/' + this . name + ".socket\').register(socket);"
94- ]
95- } ;
96- ngUtil . rewriteFile ( socketConfig ) ;
97- }
123+ if ( this . filters . socketio && this . config . get ( 'insertSockets' ) ) {
124+ var socketsFile = this . config . get ( 'registerSocketsFile' ) ;
125+ var reqPath = this . relativeRequire ( this . routeDest + '/' + this . basename +
126+ '.socket' , socketsFile ) ;
127+ var socketConfig = {
128+ file : socketsFile ,
129+ needle : this . config . get ( 'socketsNeedle' ) ,
130+ splicable : [
131+ "require(\'" + reqPath + "\').register(socket);"
132+ ]
133+ } ;
134+ ngUtil . rewriteFile ( socketConfig ) ;
98135 }
99136
100- if ( this . filters . sequelize ) {
101- if ( this . config . get ( 'insertModels' ) ) {
102- var modelConfig = {
103- file : this . config . get ( 'registerModelsFile' ) ,
104- needle : this . config . get ( 'modelsNeedle' ) ,
105- splicable : [
106- "db." + this . classedName + " = db.sequelize.import(path.join(\n" +
107- " config.root,\n" +
108- " 'server',\n" +
109- " 'api',\n" +
110- " '" + this . name + "',\n" +
111- " '" + this . name + ".model'\n" +
112- "));"
113- ]
114- } ;
115- ngUtil . rewriteFile ( modelConfig ) ;
116- }
137+ if ( this . filters . sequelize && this . config . get ( 'insertModels' ) ) {
138+ var modelsFile = this . config . get ( 'registerModelsFile' ) ;
139+ var reqPath = this . relativeRequire ( this . routeDest + '/' + this . basename +
140+ '.model' , modelsFile ) ;
141+ var modelConfig = {
142+ file : modelsFile ,
143+ needle : this . config . get ( 'modelsNeedle' ) ,
144+ splicable : [
145+ "db." + this . classedName + " = db.sequelize.import(\'" + reqPath + "\');"
146+ ]
147+ } ;
148+ ngUtil . rewriteFile ( modelConfig ) ;
117149 }
118150} ;
0 commit comments