-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
279 lines (254 loc) · 7.94 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright IBM Corp. and LoopBack contributors 2017,2020. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
// no translation: Controller
'use strict';
const _ = require('lodash');
const ArtifactGenerator = require('../../lib/artifact-generator');
const debug = require('../../lib/debug')('controller-generator');
const inspect = require('util').inspect;
const path = require('path');
const chalk = require('chalk');
const utils = require('../../lib/utils');
const g = require('../../lib/globalize');
// Exportable constants
module.exports = class ControllerGenerator extends ArtifactGenerator {
// Note: arguments and options should be defined in the constructor.
constructor(args, opts) {
super(args, opts);
}
static get BASIC() {
return g.f('Empty Controller');
}
static get REST() {
return g.f('REST Controller with CRUD functions');
}
_setupGenerator() {
this.artifactInfo = {
type: 'controller',
rootDir: 'src',
};
// XXX(kjdelisle): These should be more extensible to allow custom paths
// for each artifact type.
this.artifactInfo.outDir = path.resolve(
this.artifactInfo.rootDir,
'controllers',
);
this.artifactInfo.modelDir = path.resolve(
this.artifactInfo.rootDir,
'models',
);
this.artifactInfo.repositoryDir = path.resolve(
this.artifactInfo.rootDir,
'repositories',
);
this.option('controllerType', {
type: String,
required: false,
description: g.f('Type for the %s', this.artifactInfo.type),
});
return super._setupGenerator();
}
setOptions() {
return super.setOptions();
}
checkLoopBackProject() {
if (this.shouldExit()) return;
return super.checkLoopBackProject();
}
promptArtifactName() {
if (this.shouldExit()) return;
return super.promptArtifactName();
}
promptArtifactType() {
debug('Prompting for controller type');
if (this.shouldExit()) return;
super.promptWarningMsgForName();
// inform user what controller/file names will be created
super.promptClassFileName(
'controller',
'controllers',
utils.toClassName(this.artifactInfo.name),
);
if (this.options.controllerType) {
Object.assign(this.artifactInfo, {
controllerType: this.options.controllerType,
});
return;
}
return this.prompt([
{
type: 'list',
name: 'controllerType',
message: g.f('What kind of controller would you like to generate?'),
when: this.artifactInfo.controllerType === undefined,
choices: [ControllerGenerator.BASIC, ControllerGenerator.REST],
default: ControllerGenerator.BASIC,
},
])
.then(props => {
Object.assign(this.artifactInfo, props);
return props;
})
.catch(err => {
debug(`Error during controller type prompt: ${err.stack}`);
return this.exit(err);
});
}
async promptArtifactCrudVars() {
if (this.shouldExit()) return;
if (
!this.artifactInfo.controllerType ||
this.artifactInfo.controllerType === ControllerGenerator.BASIC
) {
return;
}
let modelList, repositoryList;
try {
modelList = await utils.getArtifactList(
this.artifactInfo.modelDir,
'model',
);
repositoryList = await utils.getArtifactList(
this.artifactInfo.repositoryDir,
'repository',
true,
);
} catch (err) {
return this.exit(err);
}
if (_.isEmpty(modelList)) {
const file = g.f('No models found in %s. ', this.artifactInfo.modelDir);
const site = g.f(
'Please visit http://loopback.io/doc/en/lb4/Controller-generator.html for information on how models are discovered.',
);
return this.exit(file + chalk.yellow(site));
}
if (_.isEmpty(repositoryList)) {
const file = g.f(
'No repositories found in %s. ',
this.artifactInfo.repositoryDir,
);
const site = g.f(
'Please visit http://loopback.io/doc/en/lb4/Controller-generator.html for information on how repositories are discovered.',
);
return this.exit(file + chalk.yellow(site));
}
return this.prompt([
{
type: 'list',
name: 'modelName',
message: g.f(
'What is the name of the model to use with this CRUD repository?',
),
choices: modelList,
when: this.artifactInfo.modelName === undefined,
default: modelList[0],
validate: utils.validateClassName,
},
{
type: 'list',
name: 'repositoryName',
message: g.f('What is the name of your CRUD repository?'),
choices: repositoryList,
when: this.artifactInfo.repositoryName === undefined,
default: repositoryList[0],
validate: utils.validateClassName,
},
{
type: 'input',
name: 'id',
message: g.f('What is the name of ID property?'),
when: this.artifactInfo.id === undefined,
default: 'id',
},
{
type: 'list',
name: 'idType',
message: g.f('What is the type of your ID?'),
choices: ['number', 'string', 'object'],
when: this.artifactInfo.idType === undefined,
default: 'number',
},
{
type: 'confirm',
name: 'idOmitted',
message: g.f('Is the id omitted when creating a new instance?'),
default: true,
},
{
type: 'input',
name: 'httpPathName',
message: g.f('What is the base HTTP path name of the CRUD operations?'),
when: this.artifactInfo.httpPathName === undefined,
default: answers =>
utils.prependBackslash(
utils.pluralize(utils.urlSlug(answers.modelName)),
),
validate: utils.validateUrlSlug,
filter: utils.prependBackslash,
},
])
.then(props => {
debug(`props: ${inspect(props)}`);
Object.assign(this.artifactInfo, props);
// Ensure that the artifact names are valid.
[
this.artifactInfo.name,
this.artifactInfo.modelName,
this.artifactInfo.repositoryName,
].forEach(item => {
item = utils.toClassName(item);
});
// Create camel-case names for variables.
this.artifactInfo.repositoryNameCamel = utils.camelCase(
this.artifactInfo.repositoryName,
);
return props;
})
.catch(err => {
debug(`Error during prompt for controller variables: ${err}`);
return this.exit(err);
});
}
scaffold() {
// We don't want to call the base scaffold function since it copies
// all of the templates!
if (this.shouldExit()) return false;
this.artifactInfo.className = utils.toClassName(this.artifactInfo.name);
this.artifactInfo.outFile =
utils.toFileName(this.artifactInfo.name) + '.controller.ts';
if (debug.enabled) {
debug(`Artifact output filename set to: ${this.artifactInfo.outFile}`);
}
this.artifactInfo.modelVariableName = utils.toVarName(
this.artifactInfo.modelName || '',
);
// renames the file
let template = 'controller-template.ts.ejs';
switch (this.artifactInfo.controllerType) {
case ControllerGenerator.REST:
template = 'controller-rest-template.ts.ejs';
break;
default:
break;
}
const source = this.templatePath(path.join('src', 'controllers', template));
if (debug.enabled) {
debug(`Using template at: ${source}`);
}
const dest = this.destinationPath(
path.join(this.artifactInfo.outDir, this.artifactInfo.outFile),
);
if (debug.enabled) {
debug(`artifactInfo: ${inspect(this.artifactInfo)}`);
debug(`Copying artifact to: ${dest}`);
}
this.copyTemplatedFiles(source, dest, this.artifactInfo);
return;
}
async end() {
await super.end();
}
};