Skip to content

Commit 5227ef0

Browse files
committed
fix(gen): endpoint accepts path as name
1 parent 3fcd8e9 commit 5227ef0

File tree

10 files changed

+124
-93
lines changed

10 files changed

+124
-93
lines changed

app/index.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,19 @@ var AngularFullstackGenerator = yeoman.generators.Base.extend({
400400
},
401401

402402
generateEndpoint: function() {
403-
var name = this.name = this.cameledName = 'thing';
404-
this.classedName = name.charAt(0).toUpperCase() + name.slice(1);
405-
this.route = '/api/' + name + 's';
406-
this.sourceRoot(path.join(__dirname, '..', 'endpoint', 'templates'));
407-
genUtils.processDirectory(this, '.', 'server/api/' + name);
403+
var models;
404+
if (this.filters.mongooseModels) {
405+
models = 'mongoose';
406+
} else if (this.filters.sequelizeModels) {
407+
models = 'sequelize';
408+
}
409+
this.composeWith('angular-fullstack:endpoint', {
410+
options: {
411+
route: '/api/things',
412+
models: models
413+
},
414+
args: ['thing']
415+
});
408416
}
409417

410418
},

endpoint/index.js

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ var ScriptBase = require('../script-base.js');
88
var 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

1929
Generator.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

7499
Generator.prototype.configuring = function config() {
@@ -83,45 +108,43 @@ Generator.prototype.writing = function createFiles() {
83108

84109
Generator.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
};

endpoint/templates/name.controller.js renamed to endpoint/templates/basename.controller.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
'use strict';<% if (filters.models) { %>
1111

1212
var _ = require('lodash');<% if (filters.mongooseModels) { %>
13-
var <%= classedName %> = require('./<%= name %>.model');<% } if (filters.sequelizeModels) { %>
14-
var sqldb = require('../../sqldb');
13+
var <%= classedName %> = require('./<%= basename %>.model');<% } if (filters.sequelizeModels) { %>
14+
var sqldb = require('<%= relativeRequire(config.get('registerModelsFile')) %>');
1515
var <%= classedName %> = sqldb.<%= classedName %>;<% } %>
1616

1717
function handleError(res, statusCode) {
@@ -64,7 +64,7 @@ function removeEntity(res) {
6464
};
6565
}<% } %>
6666

67-
// Gets a list of <%= name %>s
67+
// Gets a list of <%= classedName %>s
6868
exports.index = function(req, res) {<% if (!filters.models) { %>
6969
res.json([]);<% } else { %>
7070
<% if (filters.mongooseModels) { %><%= classedName %>.findAsync()<% }
@@ -73,7 +73,7 @@ exports.index = function(req, res) {<% if (!filters.models) { %>
7373
.catch(handleError(res));<% } %>
7474
};<% if (filters.models) { %>
7575

76-
// Gets a single <%= name %> from the DB
76+
// Gets a single <%= classedName %> from the DB
7777
exports.show = function(req, res) {
7878
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
7979
if (filters.sequelizeModels) { %><%= classedName %>.find({
@@ -86,15 +86,15 @@ exports.show = function(req, res) {
8686
.catch(handleError(res));
8787
};
8888

89-
// Creates a new <%= name %> in the DB
89+
// Creates a new <%= classedName %> in the DB
9090
exports.create = function(req, res) {
9191
<% if (filters.mongooseModels) { %><%= classedName %>.createAsync(req.body)<% }
9292
if (filters.sequelizeModels) { %><%= classedName %>.create(req.body)<% } %>
9393
.then(responseWithResult(res, 201))
9494
.catch(handleError(res));
9595
};
9696

97-
// Updates an existing <%= name %> in the DB
97+
// Updates an existing <%= classedName %> in the DB
9898
exports.update = function(req, res) {
9999
if (req.body._id) {
100100
delete req.body._id;
@@ -111,7 +111,7 @@ exports.update = function(req, res) {
111111
.catch(handleError(res));
112112
};
113113

114-
// Deletes a <%= name %> from the DB
114+
// Deletes a <%= classedName %> from the DB
115115
exports.destroy = function(req, res) {
116116
<% if (filters.mongooseModels) { %><%= classedName %>.findByIdAsync(req.params.id)<% }
117117
if (filters.sequelizeModels) { %><%= classedName %>.find({

endpoint/templates/name.events(models).js renamed to endpoint/templates/basename.events(models).js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
'use strict';
66

77
var EventEmitter = require('events').EventEmitter;<% if (filters.mongooseModels) { %>
8-
var <%= classedName %> = require('./<%= name %>.model');<% } if (filters.sequelizeModels) { %>
9-
var <%= classedName %> = require('../../sqldb').<%= classedName %>;<% } %>
8+
var <%= classedName %> = require('./<%= basename %>.model');<% } if (filters.sequelizeModels) { %>
9+
var <%= classedName %> = require('<%= relativeRequire(config.get('registerModelsFile')) %>').<%= classedName %>;<% } %>
1010
var <%= classedName %>Events = new EventEmitter();
1111

1212
// Set max event listeners (0 == unlimited)

endpoint/templates/name.integration.js renamed to endpoint/templates/basename.integration.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
var app = require('../../app');
3+
var app = require('<%= relativeRequire('server/app') %>');
44
var request = require('supertest');<% if(filters.models) { %>
55

66
var new<%= classedName %>;<% } %>
@@ -36,7 +36,7 @@ describe('<%= classedName %> API:', function() {
3636
.post('<%= route %>')
3737
.send({
3838
name: 'New <%= classedName %>',
39-
info: 'This is the brand new <%= name %>!!!'
39+
info: 'This is the brand new <%= cameledName %>!!!'
4040
})
4141
.expect(201)
4242
.expect('Content-Type', /json/)
@@ -49,9 +49,9 @@ describe('<%= classedName %> API:', function() {
4949
});
5050
});
5151

52-
it('should respond with the newly created <%= name %>', function() {
52+
it('should respond with the newly created <%= cameledName %>', function() {
5353
new<%= classedName %>.name.should.equal('New <%= classedName %>');
54-
new<%= classedName %>.info.should.equal('This is the brand new <%= name %>!!!');
54+
new<%= classedName %>.info.should.equal('This is the brand new <%= cameledName %>!!!');
5555
});
5656

5757
});
@@ -77,9 +77,9 @@ describe('<%= classedName %> API:', function() {
7777
<%= cameledName %> = {};
7878
});
7979

80-
it('should respond with the requested <%= name %>', function() {
80+
it('should respond with the requested <%= cameledName %>', function() {
8181
<%= cameledName %>.name.should.equal('New <%= classedName %>');
82-
<%= cameledName %>.info.should.equal('This is the brand new <%= name %>!!!');
82+
<%= cameledName %>.info.should.equal('This is the brand new <%= cameledName %>!!!');
8383
});
8484

8585
});
@@ -92,7 +92,7 @@ describe('<%= classedName %> API:', function() {
9292
.put('<%= route %>/' + new<%= classedName %>._id)
9393
.send({
9494
name: 'Updated <%= classedName %>',
95-
info: 'This is the updated <%= name %>!!!'
95+
info: 'This is the updated <%= cameledName %>!!!'
9696
})
9797
.expect(200)
9898
.expect('Content-Type', /json/)
@@ -109,9 +109,9 @@ describe('<%= classedName %> API:', function() {
109109
updated<%= classedName %> = {};
110110
});
111111

112-
it('should respond with the updated <%= name %>', function() {
112+
it('should respond with the updated <%= cameledName %>', function() {
113113
updated<%= classedName %>.name.should.equal('Updated <%= classedName %>');
114-
updated<%= classedName %>.info.should.equal('This is the updated <%= name %>!!!');
114+
updated<%= classedName %>.info.should.equal('This is the updated <%= cameledName %>!!!');
115115
});
116116

117117
});
@@ -130,7 +130,7 @@ describe('<%= classedName %> API:', function() {
130130
});
131131
});
132132

133-
it('should respond with 404 when <%= name %> does not exist', function(done) {
133+
it('should respond with 404 when <%= cameledName %> does not exist', function(done) {
134134
request(app)
135135
.delete('<%= route %>/' + new<%= classedName %>._id)
136136
.expect(404)

endpoint/templates/name.socket(socketio).js renamed to endpoint/templates/basename.socket(socketio).js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
'use strict';
66

7-
var <%= classedName %>Events = require('./<%= name %>.events');
7+
var <%= classedName %>Events = require('./<%= basename %>.events');
88

99
// Model events to emit
1010
var events = ['save', 'remove'];
@@ -13,7 +13,7 @@ exports.register = function(socket) {
1313
// Bind model events to socket events
1414
for (var i = 0, eventsLength = events.length; i < eventsLength; i++) {
1515
var event = events[i];
16-
var listener = createListener('<%= name %>:' + event, socket);
16+
var listener = createListener('<%= cameledName %>:' + event, socket);
1717

1818
<%= classedName %>Events.on(event, listener);
1919
socket.on('disconnect', removeListener(event, listener));

endpoint/templates/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
var express = require('express');
4-
var controller = require('./<%= name %>.controller');
4+
var controller = require('./<%= basename %>.controller');
55

66
var router = express.Router();
77

0 commit comments

Comments
 (0)