-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
151 lines (127 loc) · 4.15 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'use strict';
import path from 'path';
import {Base} from 'yeoman-generator';
import {genNamedBase} from '../generator-base';
export class Generator extends Base {
constructor(...args) {
super(...args);
this.argument('name', { type: String, required: true });
this.option('route', {
desc: 'URL for the endpoint',
type: String
});
this.option('models', {
desc: 'Specify which model(s) to use',
type: String
});
this.option('endpointDirectory', {
desc: 'Parent directory for enpoints',
type: String
});
}
initializing() {
// init shared generator properies and methods
genNamedBase(this);
}
prompting() {
var done = this.async();
var promptCb = (props) => {
if(props.route.charAt(0) !== '/') {
props.route = '/' + props.route;
}
this.route = props.route;
if (props.models) {
delete this.filters.mongoose;
delete this.filters.mongooseModels;
delete this.filters.sequelize;
delete this.filters.sequelizeModels;
this.filters[props.models] = true;
this.filters[props.models + 'Models'] = true;
}
done();
};
if (this.options.route) {
if (this.filters.mongoose && this.filters.sequelize) {
if (this.options.models) {
return promptCb(this.options);
}
} else {
if (this.filters.mongooseModels) { this.options.models = 'mongoose'; }
else if (this.filters.sequelizeModels) { this.options.models = 'sequelize'; }
else { delete this.options.models; }
return promptCb(this.options);
}
}
var name = this.name;
var base = this.config.get('routesBase') || '/api/';
if(base.charAt(base.length-1) !== '/') {
base = base + '/';
}
// pluralization defaults to true for backwards compat
if (this.config.get('pluralizeRoutes') !== false) {
name = name + 's';
}
var prompts = [{
name: 'route',
message: 'What will the url of your endpoint be?',
default: base + name
}, {
type: 'list',
name: 'models',
message: 'What would you like to use for the endpoint\'s models?',
choices: [ 'Mongoose', 'Sequelize' ],
default: this.filters.sequelizeModels ? 1 : 0,
filter: (val) => val.toLowerCase(),
when: () => this.filters.mongoose && this.filters.sequelize
}];
this.prompt(prompts, promptCb);
}
configuring() {
this.routeDest = path.join(this.options.endpointDirectory ||
this.config.get('endpointDirectory') || 'server/api/', this.name);
}
writing() {
this.sourceRoot(path.join(__dirname, '../../templates/endpoint'));
this.processDirectory('.', this.routeDest);
}
end() {
if(this.config.get('insertRoutes')) {
var routesFile = this.config.get('registerRoutesFile');
var reqPath = this.relativeRequire(this.routeDest, routesFile);
var routeConfig = {
file: routesFile,
needle: this.config.get('routesNeedle'),
splicable: [
"app.use(\'" + this.route +"\', require(\'" + reqPath + "\'));"
]
};
this.rewriteFile(routeConfig);
}
if (this.filters.socketio && this.config.get('insertSockets')) {
var socketsFile = this.config.get('registerSocketsFile');
var reqPath = this.relativeRequire(this.routeDest + '/' + this.basename +
'.socket', socketsFile);
var socketConfig = {
file: socketsFile,
needle: this.config.get('socketsNeedle'),
splicable: [
"require(\'" + reqPath + "\').register(socket);"
]
};
this.rewriteFile(socketConfig);
}
if (this.filters.sequelize && this.config.get('insertModels')) {
var modelsFile = this.config.get('registerModelsFile');
var reqPath = this.relativeRequire(this.routeDest + '/' + this.basename + '.model', modelsFile);
var modelConfig = {
file: modelsFile,
needle: this.config.get('modelsNeedle'),
splicable: [
"db." + this.classedName + " = db.sequelize.import(\'" + reqPath +"\');"
]
};
this.rewriteFile(modelConfig);
}
}
}
module.exports = Generator;