-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
prompts.js
418 lines (391 loc) · 15.8 KB
/
prompts.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
/**
* Copyright 2013-2020 the original author or authors from the JHipster project.
*
* This file is part of the JHipster project, see https://www.jhipster.tech/
* for more information.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const chalk = require('chalk');
const constants = require('generator-jhipster/generators/generator-constants');
const { getBase64Secret, getRandomHex } = require('generator-jhipster/generators/utils');
const { logger } = require('generator-jhipster/generators/utils');
module.exports = {
askForModuleName,
askForServerSideOpts,
askForOptionalItems,
askFori18n
};
function askForModuleName() {
if (this.baseName) return;
this.askModuleName(this);
}
function askForServerSideOpts(meta) {
if (!meta && this.existingProject) return;
const applicationType = this.applicationType;
if (applicationType !== 'monolith') {
logger.error('Application should be only monolith for this blueprint');
return;
}
const reactive = this.reactive;
let defaultPort = applicationType === 'gateway' ? '8080' : '8081';
if (applicationType === 'uaa') {
defaultPort = '9999';
}
const prompts = [
{
when: response => applicationType === 'gateway' || applicationType === 'microservice' || applicationType === 'uaa',
type: 'input',
name: 'serverPort',
validate: input => (/^([0-9]*)$/.test(input) ? true : 'This is not a valid port number.'),
message:
'As you are running in a microservice architecture, on which port would like your server to run? It should be unique to avoid port conflicts.',
default: defaultPort
},
{
type: 'input',
name: 'packageName',
validate: input =>
/^([a-z_]{1}[a-z0-9_]*(\.[a-z_]{1}[a-z0-9_]*)*)$/.test(input)
? true
: 'The package name you have provided is not a valid Java package name.',
message: 'What is your default Java package name?',
default: 'com.mycompany.myapp',
store: true
},
// TODO enable it when we allow other apps than monoliths
// {
// when: response => applicationType === 'gateway' || applicationType === 'microservice' || applicationType === 'uaa',
// type: 'list',
// name: 'serviceDiscoveryType',
// message: 'Which service discovery server do you want to use?',
// choices: [
// {
// value: 'eureka',
// name: 'JHipster Registry (uses Eureka, provides Spring Cloud Config support and monitoring dashboards)'
// },
// {
// value: 'consul',
// name: 'Consul'
// },
// {
// value: false,
// name: 'No service discovery'
// }
// ],
// default: 'eureka'
// },
// {
// when: applicationType === 'monolith',
// type: 'list',
// name: 'serviceDiscoveryType',
// message: 'Do you want to use the JHipster Registry to configure, monitor and scale your application?',
// choices: [
// {
// value: false,
// name: 'No'
// },
// {
// value: 'eureka',
// name: 'Yes'
// }
// ],
// default: false
// },
{
when: response =>
(applicationType === 'monolith' && response.serviceDiscoveryType !== 'eureka') ||
['gateway', 'microservice'].includes(applicationType),
type: 'list',
name: 'authenticationType',
message: `Which ${chalk.yellow('*type*')} of authentication would you like to use?`,
choices: response => {
const opts = [
{
value: 'jwt',
name: 'JWT authentication (stateless, with a token)'
}
];
if (applicationType === 'monolith' && response.serviceDiscoveryType !== 'eureka') {
opts.push({
value: 'session',
name: 'HTTP Session Authentication (stateful, default Spring Security mechanism)'
});
}
if (!reactive) {
opts.push({
value: 'oauth2',
name: 'OAuth 2.0 / OIDC Authentication (stateful, works with Keycloak and Okta)'
});
if (['gateway', 'microservice'].includes(applicationType)) {
opts.push({
value: 'uaa',
name: 'Authentication with JHipster UAA server (the server must be generated separately)'
});
}
}
return opts;
},
default: 0
},
{
when: response =>
(applicationType === 'gateway' || applicationType === 'microservice') && response.authenticationType === 'uaa',
type: 'input',
name: 'uaaBaseName',
message: 'What is the folder path of your UAA application?',
default: '../uaa',
validate: input => {
const uaaAppData = this.getUaaAppName(input);
if (uaaAppData && uaaAppData.baseName && uaaAppData.applicationType === 'uaa') {
return true;
}
return `Could not find a valid JHipster UAA server in path "${input}"`;
}
},
{
type: 'list',
name: 'databaseType',
message: `Which ${chalk.yellow('*type*')} of database would you like to use?`,
choices: response => {
const opts = [];
if (!reactive) {
opts.push({
value: 'sql',
name: 'SQL (H2, MySQL, MariaDB, PostgreSQL, Oracle, MSSQL)'
});
}
// TODO enable when we support these things
// opts.push({
// value: 'mongodb',
// name: 'MongoDB'
// });
// if (response.authenticationType !== 'oauth2') {
// opts.push({
// value: 'cassandra',
// name: 'Cassandra'
// });
// }
// opts.push({
// value: 'couchbase',
// name: 'Couchbase'
// });
// if (!reactive) {
// opts.push({
// value: 'no',
// name: 'No database'
// });
// }
return opts;
},
default: 0
},
{
when: response => response.databaseType === 'sql',
type: 'list',
name: 'prodDatabaseType',
message: `Which ${chalk.yellow('*production*')} database would you like to use?`,
choices: constants.SQL_DB_OPTIONS,
default: 0
},
{
when: response => response.databaseType === 'sql',
type: 'list',
name: 'devDatabaseType',
message: `Which ${chalk.yellow('*development*')} database would you like to use?`,
choices: response =>
[
{
value: 'h2Disk',
name: 'H2 with disk-based persistence'
},
{
value: 'h2Memory',
name: 'H2 with in-memory persistence'
}
].concat(constants.SQL_DB_OPTIONS.find(it => it.value === response.prodDatabaseType)),
default: 0
},
{
when: () => !reactive,
type: 'list',
name: 'cacheProvider',
message: 'Do you want to use the Spring cache abstraction?',
choices: [
{
value: 'ehcache',
name: 'Yes, with the Ehcache implementation (local cache, for a single node)'
},
{
value: 'hazelcast',
name:
'Yes, with the Hazelcast implementation (distributed cache, for multiple nodes, supports rate-limiting for gateway applications)'
},
{
value: 'infinispan',
name: '[BETA] Yes, with the Infinispan implementation (hybrid cache, for multiple nodes)'
},
{
value: 'memcached',
name:
'Yes, with Memcached (distributed cache) - Warning, when using an SQL database, this will disable the Hibernate 2nd level cache!'
},
{
value: 'no',
name: 'No - Warning, when using an SQL database, this will disable the Hibernate 2nd level cache!'
}
],
default: applicationType === 'microservice' || applicationType === 'uaa' ? 1 : 0
},
{
when: response =>
((response.cacheProvider !== 'no' && response.cacheProvider !== 'memcached') || applicationType === 'gateway') &&
response.databaseType === 'sql',
type: 'confirm',
name: 'enableHibernateCache',
message: 'Do you want to use Hibernate 2nd level cache?',
default: true
},
{
type: 'list',
name: 'buildTool',
message: 'Would you like to use Maven or Gradle for building the backend?',
choices: [
{
value: 'maven',
name: 'Maven'
},
{
value: 'gradle',
name: 'Gradle'
}
],
default: 'maven'
}
];
if (meta) return prompts; // eslint-disable-line consistent-return
const done = this.async();
this.prompt(prompts).then(props => {
this.serviceDiscoveryType = props.serviceDiscoveryType;
this.authenticationType = props.authenticationType;
// JWT authentication is mandatory with Eureka, so the JHipster Registry
// can control the applications
if (this.serviceDiscoveryType === 'eureka' && this.authenticationType !== 'uaa' && this.authenticationType !== 'oauth2') {
this.authenticationType = 'jwt';
}
if (this.authenticationType === 'session') {
this.rememberMeKey = getRandomHex();
}
if (this.authenticationType === 'jwt' || this.applicationType === 'microservice') {
this.jwtSecretKey = getBase64Secret(null, 64);
}
// user-management will be handled by UAA app, oauth expects users to be managed in IpP
if ((this.applicationType === 'gateway' && this.authenticationType === 'uaa') || this.authenticationType === 'oauth2') {
this.skipUserManagement = true;
}
if (this.applicationType === 'uaa') {
this.authenticationType = 'uaa';
}
this.packageName = props.packageName;
this.serverPort = props.serverPort;
if (this.serverPort === undefined) {
this.serverPort = '8080';
}
this.cacheProvider = !reactive ? props.cacheProvider : 'no';
this.enableHibernateCache = props.enableHibernateCache;
this.databaseType = props.databaseType;
this.devDatabaseType = props.devDatabaseType;
this.prodDatabaseType = props.prodDatabaseType;
this.searchEngine = props.searchEngine;
this.buildTool = props.buildTool;
this.uaaBaseName = this.getUaaAppName(props.uaaBaseName).baseName;
if (this.databaseType === 'no') {
this.devDatabaseType = 'no';
this.prodDatabaseType = 'no';
this.enableHibernateCache = false;
if (this.authenticationType !== 'uaa') {
this.skipUserManagement = true;
}
} else if (this.databaseType === 'mongodb') {
this.devDatabaseType = 'mongodb';
this.prodDatabaseType = 'mongodb';
this.enableHibernateCache = false;
} else if (this.databaseType === 'couchbase') {
this.devDatabaseType = 'couchbase';
this.prodDatabaseType = 'couchbase';
this.enableHibernateCache = false;
} else if (this.databaseType === 'cassandra') {
this.devDatabaseType = 'cassandra';
this.prodDatabaseType = 'cassandra';
this.enableHibernateCache = false;
}
done();
});
}
function askForOptionalItems(meta) {
if (!meta && this.existingProject) return;
const applicationType = this.applicationType;
const choices = [];
const defaultChoice = [];
if (!this.reactive) {
if (this.databaseType === 'sql' || this.databaseType === 'mongodb') {
choices.push({
name: 'Search engine using Elasticsearch',
value: 'searchEngine:elasticsearch'
});
}
if (applicationType === 'monolith' || applicationType === 'gateway') {
choices.push({
name: 'WebSockets using Spring Websocket',
value: 'websocket:spring-websocket'
});
}
choices.push({
name: 'Asynchronous messages using Apache Kafka',
value: 'messageBroker:kafka'
});
}
choices.push({
name: 'API first development using OpenAPI-generator',
value: 'enableSwaggerCodegen:true'
});
const PROMPTS = {
type: 'checkbox',
name: 'serverSideOptions',
message: 'Which other technologies would you like to use?',
choices,
default: defaultChoice
};
if (meta) return PROMPTS; // eslint-disable-line consistent-return
const done = this.async();
if (choices.length > 0) {
this.prompt(PROMPTS).then(prompt => {
this.serverSideOptions = prompt.serverSideOptions;
this.websocket = this.getOptionFromArray(this.serverSideOptions, 'websocket');
this.searchEngine = this.getOptionFromArray(this.serverSideOptions, 'searchEngine');
this.messageBroker = this.getOptionFromArray(this.serverSideOptions, 'messageBroker');
this.enableSwaggerCodegen = this.getOptionFromArray(this.serverSideOptions, 'enableSwaggerCodegen');
// Only set this option if it hasn't been set in a previous question, as it's only optional for monoliths
if (!this.serviceDiscoveryType) {
this.serviceDiscoveryType = this.getOptionFromArray(this.serverSideOptions, 'serviceDiscoveryType');
}
done();
});
} else {
done();
}
}
function askFori18n() {
if (this.existingProject || this.configOptions.skipI18nQuestion) return;
this.aski18n(this);
}