@@ -8,6 +8,16 @@ var ScriptBase = require('../script-base.js');
88var Generator = module . exports = function Generator ( ) {
99 ScriptBase . apply ( this , arguments ) ;
1010
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+
1121 this . option ( 'endpointDirectory' , {
1222 desc : 'Parent directory for enpoints' ,
1323 type : String
@@ -18,6 +28,38 @@ util.inherits(Generator, ScriptBase);
1828
1929Generator . prototype . prompting = function askFor ( ) {
2030 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+
2163 var name = this . name ;
2264
2365 var base = this . config . get ( 'routesBase' ) || '/api/' ;
@@ -51,24 +93,7 @@ Generator.prototype.prompting = function askFor() {
5193 }
5294 ] ;
5395
54- this . prompt ( prompts , function ( props ) {
55- if ( props . route . charAt ( 0 ) !== '/' ) {
56- props . route = '/' + props . route ;
57- }
58-
59- this . route = props . route ;
60-
61- if ( props . models ) {
62- delete this . filters . mongoose ;
63- delete this . filters . mongooseModels ;
64- delete this . filters . sequelize ;
65- delete this . filters . sequelizeModels ;
66-
67- this . filters [ props . models ] = true ;
68- this . filters [ props . models + 'Models' ] = true ;
69- }
70- done ( ) ;
71- } . bind ( this ) ) ;
96+ this . prompt ( prompts , promptCb ) ;
7297} ;
7398
7499Generator . prototype . configuring = function config ( ) {
@@ -83,45 +108,43 @@ Generator.prototype.writing = function createFiles() {
83108
84109Generator . prototype . end = function registerEndpoint ( ) {
85110 if ( this . config . get ( 'insertRoutes' ) ) {
111+ var routesFile = this . config . get ( 'registerRoutesFile' ) ;
112+ var reqPath = this . relativeRequire ( this . routeDest , routesFile ) ;
86113 var routeConfig = {
87- file : this . config . get ( 'registerRoutesFile' ) ,
114+ file : routesFile ,
88115 needle : this . config . get ( 'routesNeedle' ) ,
89116 splicable : [
90- "app.use(\'" + this . route + "\', require(\'./api/ " + this . name + "\'));"
117+ "app.use(\'" + this . route + "\', require(\'" + reqPath + "\'));"
91118 ]
92119 } ;
93120 ngUtil . rewriteFile ( routeConfig ) ;
94121 }
95122
96- if ( this . filters . socketio ) {
97- if ( this . config . get ( 'insertSockets' ) ) {
98- var socketConfig = {
99- file : this . config . get ( 'registerSocketsFile' ) ,
100- needle : this . config . get ( 'socketsNeedle' ) ,
101- splicable : [
102- "require(\'../api/" + this . name + '/' + this . name + ".socket\').register(socket);"
103- ]
104- } ;
105- ngUtil . rewriteFile ( socketConfig ) ;
106- }
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 ) ;
107135 }
108136
109- if ( this . filters . sequelize ) {
110- if ( this . config . get ( 'insertModels' ) ) {
111- var modelConfig = {
112- file : this . config . get ( 'registerModelsFile' ) ,
113- needle : this . config . get ( 'modelsNeedle' ) ,
114- splicable : [
115- "db." + this . classedName + " = db.sequelize.import(path.join(\n" +
116- " config.root,\n" +
117- " 'server',\n" +
118- " 'api',\n" +
119- " '" + this . name + "',\n" +
120- " '" + this . name + ".model'\n" +
121- "));"
122- ]
123- } ;
124- ngUtil . rewriteFile ( modelConfig ) ;
125- }
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 ) ;
126149 }
127150} ;
0 commit comments