-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
generator.ts
471 lines (419 loc) · 18.4 KB
/
generator.ts
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
/**
* Copyright 2013-2024 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
*
* https://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.
*/
import assert from 'assert';
import os from 'os';
import { lowerFirst } from 'lodash-es';
import chalk from 'chalk';
import { passthrough } from '@yeoman/transform';
import { isFileStateModified } from 'mem-fs-editor/state';
import BaseApplicationGenerator from '../base-application/index.js';
import {
addFakerToEntity,
loadEntitiesAnnotations,
loadEntitiesOtherSide,
stringifyApplicationData,
prepareEntity as prepareEntityForTemplates,
prepareField as prepareFieldForTemplates,
prepareRelationship,
} from '../base-application/support/index.js';
import { createAuthorityEntity, createUserEntity, createUserManagementEntity } from './utils.js';
import { JAVA_DOCKER_DIR } from '../generator-constants.js';
import { GENERATOR_BOOTSTRAP, GENERATOR_COMMON, GENERATOR_PROJECT_NAME } from '../generator-list.js';
import { packageJson } from '../../lib/index.js';
import { loadLanguagesConfig } from '../languages/support/index.js';
import { loadAppConfig, loadDerivedAppConfig, loadStoredAppOptions } from '../app/support/index.js';
import { exportJDLTransform, importJDLTransform } from './support/index.js';
import command from './command.js';
const isWin32 = os.platform() === 'win32';
export default class BootstrapApplicationBase extends BaseApplicationGenerator {
constructor(args: any, options: any, features: any) {
super(args, options, { jhipsterBootstrap: false, ...features });
if (this.options.help) return;
loadStoredAppOptions.call(this);
}
async beforeQueue() {
if (!this.fromBlueprint) {
await this.composeWithBlueprints();
}
if (this.delegateToBlueprint) {
throw new Error('Only sbs blueprint is supported');
}
const projectNameGenerator = (await this.dependsOnJHipster(GENERATOR_PROJECT_NAME)) as any;
projectNameGenerator.javaApplication = true;
await this.composeWithJHipster(GENERATOR_BOOTSTRAP);
}
get initializing() {
return this.asInitializingTaskGroup({
displayLogo() {
this.printDestinationInfo();
},
parseOptions() {
this.parseJHipsterCommand(command);
},
async jdlStore() {
if (this.jhipsterConfig.jdlStore) {
this.logger.warn('Storing configuration inside a JDL file is experimental');
this.logger.info(`Using JDL store ${this.jhipsterConfig.jdlStore}`);
const destinationPath = this.destinationPath();
const jdlStorePath = this.destinationPath(this.jhipsterConfig.jdlStore);
this.features.commitTransformFactory = () => exportJDLTransform({ destinationPath, jdlStorePath });
await this.pipeline({ refresh: true, pendingFiles: false }, importJDLTransform({ destinationPath, jdlStorePath }));
}
},
});
}
get [BaseApplicationGenerator.INITIALIZING]() {
return this.initializing;
}
get configuring() {
return this.asConfiguringTaskGroup({
configuring() {
if (this.jhipsterConfig.baseName === undefined) {
this.jhipsterConfig.baseName = 'jhipster';
}
},
});
}
get [BaseApplicationGenerator.CONFIGURING]() {
return this.configuring;
}
get loading() {
return this.asLoadingTaskGroup({
loadApplication({ application, control, applicationDefaults }) {
loadAppConfig({
config: this.jhipsterConfigWithDefaults,
application,
useVersionPlaceholders: (this as any).useVersionPlaceholders,
});
loadLanguagesConfig({ application, config: this.jhipsterConfigWithDefaults, control });
applicationDefaults({
backendType: this.jhipsterConfig.backendType ?? 'Java',
syncUserWithIdp: this.jhipsterConfig.syncUserWithIdp,
});
},
loadNodeDependencies({ application }) {
this.loadNodeDependencies(application.nodeDependencies, {
prettier: packageJson.dependencies.prettier,
'prettier-plugin-java': packageJson.dependencies['prettier-plugin-java'],
'prettier-plugin-packagejson': packageJson.dependencies['prettier-plugin-packagejson'],
});
this.loadNodeDependenciesFromPackageJson(
application.nodeDependencies,
this.fetchFromInstalledJHipster(GENERATOR_COMMON, 'resources', 'package.json'),
);
},
});
}
get [BaseApplicationGenerator.LOADING]() {
return this.loading;
}
get preparing() {
return this.asPreparingTaskGroup({
prepareApplication({ application, applicationDefaults }) {
loadDerivedAppConfig({ application });
applicationDefaults({
__override__: false,
nodePackageManager: 'npm',
dockerServicesDir: JAVA_DOCKER_DIR,
// TODO drop clientPackageManager
clientPackageManager: ({ nodePackageManager }) => nodePackageManager,
hipsterName: 'Java Hipster',
hipsterProductName: 'JHipster',
hipsterHomePageProductName: 'JHipster',
hipsterStackOverflowProductName: 'JHipster',
hipsterBugTrackerProductName: 'JHipster',
hipsterChatProductName: 'JHipster',
hipsterTwitterUsername: '@jhipster',
hipsterDocumentationLink: 'https://www.jhipster.tech/',
hipsterTwitterLink: 'https://twitter.com/jhipster',
hipsterProjectLink: 'https://github.com/jhipster/generator-jhipster',
hipsterStackoverflowLink: 'https://stackoverflow.com/tags/jhipster/info',
hipsterBugTrackerLink: 'https://github.com/jhipster/generator-jhipster/issues?state=open',
hipsterChatLink: 'https://gitter.im/jhipster/generator-jhipster',
backendTypeSpringBoot: ({ backendType }) => backendType === 'Java',
backendTypeJavaAny: ({ backendTypeSpringBoot }) => backendTypeSpringBoot,
});
},
syncUserWithIdp({ application, applicationDefaults }) {
if (!application.backendTypeSpringBoot) return;
if (application.syncUserWithIdp === undefined && application.authenticationType === 'oauth2') {
applicationDefaults({
__override__: false,
syncUserWithIdp: data =>
data.databaseType !== 'no' &&
(data.applicationType === 'gateway' ||
this.getExistingEntities().some(entity =>
(entity.definition.relationships ?? []).some(relationship => relationship.otherEntityName.toLowerCase() === 'user'),
)),
});
} else if (application.syncUserWithIdp && application.authenticationType !== 'oauth2') {
throw new Error('syncUserWithIdp is only supported with oauth2 authenticationType');
}
},
userManagement({ applicationDefaults }) {
applicationDefaults({
generateBuiltInUserEntity: ({ generateUserManagement, syncUserWithIdp }) => generateUserManagement || syncUserWithIdp,
generateBuiltInAuthorityEntity: ({ generateBuiltInUserEntity, databaseType }) =>
generateBuiltInUserEntity && databaseType !== 'cassandra',
});
},
});
}
get [BaseApplicationGenerator.PREPARING]() {
return this.preparing;
}
get configuringEachEntity() {
return this.asConfiguringEachEntityTaskGroup({
configureEntity({ entityStorage, entityConfig }) {
entityStorage.defaults({ fields: [], relationships: [], annotations: {} });
if (entityConfig.changelogDate) {
entityConfig.annotations.changelogDate = entityConfig.changelogDate;
delete entityConfig.changelogDate;
}
if (!entityConfig.annotations.changelogDate) {
entityConfig.annotations.changelogDate = this.dateFormatForLiquibase();
entityStorage.save();
}
},
configureRelationships({ entityName, entityStorage, entityConfig }) {
// Validate entity json relationship content
entityConfig.relationships.forEach((relationship: any) => {
const { otherEntityName, relationshipType } = relationship;
assert(
otherEntityName,
`otherEntityName is missing in .jhipster/${entityName}.json for relationship ${stringifyApplicationData(relationship)}`,
);
assert(
relationshipType,
`relationshipType is missing in .jhipster/${entityName}.json for relationship ${stringifyApplicationData(relationship)}`,
);
if (!relationship.relationshipSide) {
// Try to create relationshipSide based on best bet.
if (relationship.ownerSide !== undefined) {
relationship.relationshipSide = relationship.ownerSide ? 'left' : 'right';
} else {
// Missing ownerSide (one-to-many/many-to-one relationships) depends on the otherSide existence.
const unidirectionalRelationship = !relationship.otherEntityRelationshipName;
const bidirectionalOneToManySide = !unidirectionalRelationship && relationship.relationshipType === 'one-to-many';
relationship.relationshipSide = unidirectionalRelationship || bidirectionalOneToManySide ? 'left' : 'right';
}
}
relationship.otherEntityName = lowerFirst(otherEntityName);
if (relationship.relationshipName === undefined) {
relationship.relationshipName = relationship.otherEntityName;
this.log.warn(
`relationshipName is missing in .jhipster/${entityName}.json for relationship ${stringifyApplicationData(
relationship,
)}, using ${relationship.otherEntityName} as fallback`,
);
}
});
entityStorage.save();
},
});
}
get [BaseApplicationGenerator.CONFIGURING_EACH_ENTITY]() {
return this.configuringEachEntity;
}
get loadingEntities() {
return this.asLoadingEntitiesTaskGroup({
loadUser({ application, entitiesToLoad }) {
if (application.generateBuiltInUserEntity) {
const User = 'User';
const customUser = entitiesToLoad.find(entityToLoad => entityToLoad.entityName === User);
const bootstrap = customUser?.entityBootstrap;
if (!bootstrap) {
throw new Error('User entity should already be passed.');
}
const customUserData: any = customUser?.entityStorage.getAll() ?? {};
Object.assign(bootstrap!, createUserEntity.call(this, { ...customUserData, ...customUserData.annotations }, application));
application.user = bootstrap;
}
},
loadUserManagement({ application, entitiesToLoad }) {
if (application.generateBuiltInUserEntity && application.generateUserManagement) {
const UserManagement = 'UserManagement';
const customUserManagement = entitiesToLoad.find(entityToLoad => entityToLoad.entityName === UserManagement);
const bootstrap = customUserManagement?.entityBootstrap;
if (!bootstrap) {
throw new Error('UserManagement entity should already be passed.');
}
const customUserManagementData: any = customUserManagement?.entityStorage.getAll() ?? {};
Object.assign(
bootstrap!,
createUserManagementEntity.call(this, { ...customUserManagementData, ...customUserManagementData.annotations }, application),
);
application.userManagement = bootstrap;
}
},
loadAuthority({ application, entitiesToLoad }) {
if (application.generateBuiltInAuthorityEntity) {
const authority = 'Authority';
const customEntity = entitiesToLoad.find(entityToLoad => entityToLoad.entityName === authority);
const bootstrap = customEntity?.entityBootstrap;
if (!bootstrap) {
throw new Error('Authority entity should already be passed.');
}
const customEntityData: any = customEntity?.entityStorage.getAll() ?? {};
Object.assign(
bootstrap!,
createAuthorityEntity.call(this, { ...customEntityData, ...customEntityData.annotations }, application),
);
application.authority = bootstrap;
}
},
loadingEntities({ application, entitiesToLoad }) {
for (const { entityName, entityBootstrap, entityStorage } of entitiesToLoad) {
if (!entityBootstrap.builtIn) {
let entity = entityStorage.getAll() as any;
entity.name = entity.name ?? entityName;
entity = { ...entity, ...entity.annotations };
Object.assign(entityBootstrap, entity);
}
}
const entities = this.sharedData.getEntities().map(({ entity }) => entity);
loadEntitiesAnnotations(entities);
this.validateResult(loadEntitiesOtherSide(entities, { application }));
for (const entity of entities) {
for (const relationship of entity.relationships) {
if (relationship.ownerSide === undefined) {
// ownerSide backward compatibility
relationship.ownerSide =
relationship.relationshipType === 'many-to-one' ||
(relationship.relationshipType !== 'one-to-many' && relationship.relationshipSide === 'left');
}
}
}
},
});
}
get [BaseApplicationGenerator.LOADING_ENTITIES]() {
return this.loadingEntities;
}
get preparingEachEntity() {
return this.asPreparingEachEntityTaskGroup({
async preparingEachEntity({ application, entity }) {
await addFakerToEntity(entity, application.nativeLanguage);
prepareEntityForTemplates(entity, this, application);
},
});
}
get [BaseApplicationGenerator.PREPARING_EACH_ENTITY]() {
return this.preparingEachEntity;
}
get preparingEachEntityField() {
return this.asPreparingEachEntityFieldTaskGroup({
prepareFieldsForTemplates({ entity, field }) {
prepareFieldForTemplates(entity, field, this);
},
});
}
get [BaseApplicationGenerator.PREPARING_EACH_ENTITY_FIELD]() {
return this.preparingEachEntityField;
}
get preparingEachEntityRelationship() {
return this.asPreparingEachEntityRelationshipTaskGroup({
prepareRelationshipsForTemplates({ entity, relationship }) {
prepareRelationship(entity, relationship, this);
},
});
}
get [BaseApplicationGenerator.PREPARING_EACH_ENTITY_RELATIONSHIP]() {
return this.preparingEachEntityRelationship;
}
get postPreparingEachEntity() {
return this.asPostPreparingEachEntityTaskGroup({
checkForCircularRelationships({ entity }) {
const detectCyclicRequiredRelationship = (entity, relatedEntities) => {
if (relatedEntities.has(entity)) return true;
relatedEntities.add(entity);
return entity.relationships
?.filter(rel => rel.relationshipRequired || rel.id)
.some(rel => detectCyclicRequiredRelationship(rel.otherEntity, new Set([...relatedEntities])));
};
entity.hasCyclicRequiredRelationship = detectCyclicRequiredRelationship(entity, new Set());
},
});
}
get [BaseApplicationGenerator.POST_PREPARING_EACH_ENTITY]() {
return this.delegateTasksToBlueprint(() => this.postPreparingEachEntity);
}
get default() {
return this.asDefaultTaskGroup({
task({ application }) {
const packageJsonFiles = [this.destinationPath('package.json')];
if (application.clientRootDir) {
packageJsonFiles.push(this.destinationPath(`${application.clientRootDir}package.json`));
}
const isPackageJson = file => packageJsonFiles.includes(file.path);
const populateNullValues = dependencies => {
if (!dependencies) return;
for (const key of Object.keys(dependencies)) {
if (dependencies[key] === null && application.nodeDependencies[key]) {
dependencies[key] = application.nodeDependencies[key];
}
}
};
this.queueTransformStream(
{
name: 'updating package.json dependencies versions',
filter: file => isFileStateModified(file) && file.path.startsWith(this.destinationPath()) && isPackageJson(file),
refresh: false,
},
passthrough(file => {
const contents = file.contents.toString();
if (contents.includes('null')) {
const content = JSON.parse(file.contents.toString());
populateNullValues(content.dependencies);
populateNullValues(content.devDependencies);
populateNullValues(content.peerDependencies);
file.contents = Buffer.from(`${JSON.stringify(content, null, 2)}\n`);
}
}),
);
},
});
}
get [BaseApplicationGenerator.DEFAULT]() {
return this.default;
}
/**
* Return the user home
*/
getUserHome() {
return process.env[isWin32 ? 'USERPROFILE' : 'HOME'];
}
printDestinationInfo(cwd = this.destinationPath()) {
this.log.log(
chalk.green(' _______________________________________________________________________________________________________________\n'),
);
this.log.log(
chalk.white(` Documentation for creating an application is at ${chalk.yellow('https://www.jhipster.tech/creating-an-app/')}
Application files will be generated in folder: ${chalk.yellow(cwd)}`),
);
if (process.cwd() === this.getUserHome()) {
this.log.log(chalk.red.bold('\n️⚠️ WARNING ⚠️ You are in your HOME folder!'));
this.log.log(chalk.red('This can cause problems, you should always create a new directory and run the jhipster command from here.'));
this.log.log(chalk.white(`See the troubleshooting section at ${chalk.yellow('https://www.jhipster.tech/installation/')}`));
}
this.log.log(
chalk.green(' _______________________________________________________________________________________________________________\n'),
);
}
}