-
-
Notifications
You must be signed in to change notification settings - Fork 4k
/
import-jdl.js
556 lines (502 loc) · 19.8 KB
/
import-jdl.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
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/**
* Copyright 2013-2022 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.
*/
const chalk = require('chalk');
const fs = require('fs');
const _ = require('lodash');
const path = require('path');
const pluralize = require('pluralize');
const { fork: forkProcess } = require('child_process');
const EnvironmentBuilder = require('./environment-builder');
const { CLI_NAME, GENERATOR_NAME, logger, toString, printSuccess, getOptionAsArgs } = require('./utils');
const { createImporterFromContent, createImporterFromFiles } = require('../jdl/jdl-importer');
const packagejs = require('../package.json');
const statistics = require('../generators/statistics');
const { JHIPSTER_CONFIG_DIR } = require('../generators/generator-constants');
const jhipsterCli = require.resolve('./cli.js');
const { writeConfigFile } = require('../jdl/exporters/export-utils');
const { createFolderIfItDoesNotExist } = require('../jdl/utils/file-utils');
const getDeploymentType = deployment => deployment && deployment[GENERATOR_NAME] && deployment[GENERATOR_NAME].deploymentType;
/**
* Check if .yo-rc.json exists inside baseName folder.
* @param {string} baseName
* @return {boolean}
*/
const baseNameConfigExists = baseName => fs.existsSync(baseName === undefined ? '.yo-rc.json' : path.join(baseName, '.yo-rc.json'));
const multiplesApplications = processor => {
return Object.values(processor.importState.exportedApplicationsWithEntities).length > 1;
};
/**
* When importing multiples applications, we should import each of them at it's own baseName folder.
* @param {JDLProcessor} processor
* @return {boolean}
*/
const shouldRunInFolder = processor => {
return multiplesApplications(processor);
};
/**
* Check if every application is new.
* @param {JDLProcessor} processor
* @return {boolean}
*/
const allNewApplications = processor => {
const applications = Object.values(processor.importState.exportedApplicationsWithEntities);
if (applications.length === 1) return !baseNameConfigExists();
return !applications.find(application => baseNameConfigExists(application.config.baseName));
};
/**
* Check if the generation should be forced.
* @param {JDLProcessor} processor
* @return {boolean}
*/
const shouldForce = processor => {
if (processor.options.force !== undefined) {
return processor.options.force;
}
if (Object.values(processor.importState.exportedApplicationsWithEntities).length === 0) {
return undefined;
}
return allNewApplications(processor) ? true : undefined;
};
/**
* JHipster will use fork by default only when generating new applications, otherwise it can conflict with incremental changelog.
* @param {JDLProcessor} processor
* @return {boolean}
*/
const shouldFork = processor => {
if (processor.options.fork !== undefined) {
return processor.options.fork;
}
if (Object.values(processor.importState.exportedApplicationsWithEntities).length > 1 && allNewApplications(processor)) {
return true;
}
return undefined;
};
/**
* When regenerating applications we should run interactively, so prompts will be shown by default.
* @param {JDLProcessor} processor
* @return {boolean} true if generation should be executed interactively
*/
const shouldRunInteractively = processor => {
if (processor.options.interactive !== undefined) {
return processor.options.interactive;
}
return !shouldFork(processor);
};
/**
* Write entity config to disk.
* @param {any} entity
* @param {string} basePath
*/
function writeEntityConfig(entity, basePath) {
const entitiesPath = path.join(basePath, JHIPSTER_CONFIG_DIR);
createFolderIfItDoesNotExist(entitiesPath);
const filePath = path.join(entitiesPath, `${_.upperFirst(entity.name)}.json`);
fs.writeFileSync(filePath, JSON.stringify(entity, null, 2).concat('\n'));
}
/**
* Write application config to disk.
* @param {any} applicationWithEntities
* @param {string} basePath
*/
function writeApplicationConfig(applicationWithEntities, basePath) {
createFolderIfItDoesNotExist(basePath);
writeConfigFile({ 'generator-jhipster': applicationWithEntities.config }, path.join(basePath, '.yo-rc.json'));
applicationWithEntities.entities.forEach(entity => writeEntityConfig(entity, basePath));
}
/**
* Run the generator.
* @param {string} command
* @param {Object} options
* @param {string} options.cwd
* @param {boolean} options.fork
* @param {Environment} options.env
* @param {Object} [generatorOptions]
*/
function runGenerator(command, { cwd, fork, env, createEnvBuilder }, generatorOptions = {}) {
const { workspaces } = generatorOptions;
generatorOptions = {
...generatorOptions,
// Remove jdl command exclusive options
fork: undefined,
interactive: undefined,
jsonOnly: undefined,
ignoreApplication: undefined,
ignoreDeployments: undefined,
inline: undefined,
skipSampleRepository: undefined,
workspaces: undefined,
forceNoFiltering: undefined,
unidirectionalRelationships: undefined,
localConfigOnly: undefined,
commandName: undefined,
fromJdl: true,
};
if (workspaces) {
generatorOptions.skipInstall = true;
}
if (!generatorOptions.blueprints) {
delete generatorOptions.blueprints;
}
if (!fork) {
const oldCwd = process.cwd();
process.chdir(cwd);
env = env || createEnvBuilder(undefined, { cwd }).getEnvironment();
return env
.run(`${CLI_NAME}:${command}`, generatorOptions)
.then(
() => {
logger.info(`Generator ${command} succeed`);
},
error => {
logger.error(`Error running generator ${command}: ${error}`, error);
return Promise.reject(error);
}
)
.finally(() => {
process.chdir(oldCwd);
});
}
logger.debug(`Child process will be triggered for ${command} with cwd: ${cwd}`);
const args = [command, ...getOptionAsArgs(generatorOptions)];
const childProc = forkProcess(jhipsterCli, args, {
cwd,
});
return new Promise((resolve, reject) => {
childProc.on('exit', code => {
logger.debug(`Process ${args} exited with code ${code}`);
logger.info(`Generator ${command} child process exited with code ${code}`);
if (code !== 0) {
process.exitCode = code;
reject(new Error(`Error executing ${args.join(' ')}`));
return;
}
resolve();
});
});
}
/**
* Imports the Applications and Entities defined in JDL
* The app .yo-rc.json files and entity json files are written to disk
*/
function importJDL(jdlImporter) {
logger.info('The JDL is being parsed.');
try {
const importState = jdlImporter.import();
logger.debug(`importState exportedEntities: ${importState.exportedEntities.length}`);
logger.debug(`importState exportedApplications: ${importState.exportedApplications.length}`);
logger.debug(`importState exportedDeployments: ${importState.exportedDeployments.length}`);
if (importState.exportedEntities.length > 0) {
const entityNames = _.uniq(importState.exportedEntities.map(exportedEntity => exportedEntity.name)).join(', ');
logger.info(`Found entities: ${chalk.yellow(entityNames)}.`);
} else {
logger.info(chalk.yellow('No change in entity configurations, no entities were updated.'));
}
logger.info('The JDL has been successfully parsed');
return importState;
} catch (error) {
logger.debug('Error:', error);
if (error) {
const errorName = `${error.name}:` || '';
const errorMessage = error.message || '';
logger.log(chalk.red(`${errorName} ${errorMessage}`));
}
logger.error(`Error while parsing applications and entities from the JDL ${error}`, error);
throw error;
}
}
/**
* Check if application needs to be generated
* @param {any} processor
*/
const shouldGenerateApplications = processor =>
!processor.options.ignoreApplication && !processor.options.jsonOnly && processor.importState.exportedApplications.length !== 0;
/**
* Check if deployments needs to be generated
* @param {any} processor
*/
const shouldGenerateDeployments = processor =>
!processor.options.ignoreDeployments && !processor.options.jsonOnly && processor.importState.exportedDeployments.length !== 0;
/**
* Generate deployment source code for JDL deployments defined.
* @param {any} config
* @returns Promise
*/
const generateDeploymentFiles = ({ processor, deployment }) => {
const deploymentType = getDeploymentType(deployment);
logger.info(`Generating deployment ${deploymentType} in a new parallel process`);
logger.debug(`Generating deployment: ${JSON.stringify(deployment[GENERATOR_NAME], null, 2)}`);
const { pwd, createEnvBuilder } = processor;
const cwd = path.join(pwd, deploymentType);
logger.debug(`Child process will be triggered for ${jhipsterCli} with cwd: ${cwd}`);
return runGenerator(deploymentType, { cwd, fork: false, createEnvBuilder }, { force: true, ...processor.options, skipPrompts: true });
};
/**
* Generate application source code for JDL apps defined.
* @param {any} config
* @returns Promise
*/
const generateApplicationFiles = ({ processor, applicationWithEntities }) => {
logger.debug(`Generating application: ${JSON.stringify(applicationWithEntities.config, null, 2)}`);
const { inFolder, fork, force, reproducible, createEnvBuilder } = processor;
const baseName = applicationWithEntities.config.baseName;
const cwd = inFolder ? path.join(processor.pwd, baseName) : processor.pwd;
if (processor.options.jsonOnly) {
writeApplicationConfig(applicationWithEntities, cwd);
return Promise.resolve();
}
if (fork) {
writeApplicationConfig(applicationWithEntities, cwd);
}
const withEntities = applicationWithEntities.entities.length > 0 ? true : undefined;
const generatorOptions = { reproducible, force, withEntities, ...processor.options };
if (!fork) {
generatorOptions.applicationWithEntities = applicationWithEntities;
}
return runGenerator('app', { cwd, fork, createEnvBuilder }, generatorOptions);
};
/**
* Generate entities for the applications
* @param {any} processor
* @param {any} exportedEntities
* @param {any} env
* @return Promise
*/
const generateEntityFiles = (processor, exportedEntities, env) => {
const { fork, inFolder, force, createEnvBuilder } = processor;
const generatorOptions = {
force,
...processor.options,
};
const callGenerator = baseName => {
const cwd = inFolder && baseName ? path.join(processor.pwd, baseName) : processor.pwd;
if (processor.options.jsonOnly || baseName) {
exportedEntities
.filter(entity => !baseName || entity.applications.includes(baseName))
.forEach(entity => writeEntityConfig(entity, cwd));
if (processor.options.jsonOnly) {
logger.info('Entity JSON files created. Entity generation skipped.');
return Promise.resolve();
}
} else {
generatorOptions.entitiesToImport = exportedEntities;
}
logger.info(`Generating entities for application ${baseName} in a new parallel process`);
logger.debug(`Child process will be triggered for ${jhipsterCli} with cwd: ${cwd}`);
return runGenerator('entities', { cwd, env, fork, createEnvBuilder }, generatorOptions);
};
if (fork) {
/* Generating entities inside multiple apps */
const baseNames = [...new Set(exportedEntities.flatMap(entity => entity.applications))];
if (processor.interactive) {
return baseNames.reduce((promise, baseName) => {
return promise.then(() => callGenerator(baseName));
}, Promise.resolve());
}
return Promise.all(baseNames.map(baseName => callGenerator(baseName)));
}
return callGenerator();
};
class JDLProcessor {
constructor(jdlFiles, jdlContent, options, createEnvBuilder) {
logger.debug(
`JDLProcessor started with ${jdlContent ? `content: ${jdlContent}` : `files: ${jdlFiles}`} and options: ${toString(options)}`
);
this.jdlFiles = jdlFiles;
this.jdlContent = jdlContent;
this.options = options;
this.pwd = process.cwd();
this.createEnvBuilder = createEnvBuilder;
}
importJDL() {
const configuration = {
applicationName: this.options.baseName,
databaseType: this.options.db,
applicationType: this.options.applicationType,
skipUserManagement: this.options.skipUserManagement,
unidirectionalRelationships: this.options.unidirectionalRelationships,
forceNoFiltering: this.options.forceNoFiltering,
generatorVersion: packagejs.version,
skipFileGeneration: true,
};
let importer;
if (this.jdlContent) {
importer = createImporterFromContent(this.jdlContent, configuration);
} else {
importer = createImporterFromFiles(this.jdlFiles, configuration);
}
this.importState = importJDL.call(this, importer);
}
config() {
this.interactive = shouldRunInteractively(this);
this.fork = shouldFork(this);
this.reproducible = allNewApplications(this);
this.inFolder = shouldRunInFolder(this);
this.force = shouldForce(this);
return this;
}
sendInsight() {
statistics.sendSubGenEvent('generator', 'import-jdl');
}
generateWorkspaces(options, generateJdl) {
return this.createEnvBuilder()
.getEnvironment()
.run('jhipster:workspaces', { workspaces: false, ...options, importState: this.importState, generateJdl });
}
generateApplications() {
if (this.options.ignoreApplication) {
logger.debug('Applications not generated');
return Promise.resolve();
}
const callGenerator = applicationWithEntities => {
try {
return generateApplicationFiles({
processor: this,
applicationWithEntities,
});
} catch (error) {
logger.error(`Error while generating applications from the parsed JDL\n${error}`, error);
throw error;
}
};
const applicationsWithEntities = Object.values(this.importState.exportedApplicationsWithEntities);
logger.info(`Generating ${applicationsWithEntities.length} ${pluralize('application', applicationsWithEntities.length)}.`);
if (applicationsWithEntities.length === 0) {
return Promise.resolve();
}
const allApplications = Object.fromEntries(
applicationsWithEntities.map((applicationWithEntities, applicationIndex) => {
applicationWithEntities.config.applicationIndex = applicationIndex;
return [applicationWithEntities.config.baseName, applicationWithEntities.config];
})
);
applicationsWithEntities.forEach(applicationWithEntities => {
const microfrontends = applicationWithEntities.config.microfrontends || [];
const relatedApplications = Object.entries(allApplications).filter(
([baseName]) =>
applicationWithEntities.config.baseName !== baseName &&
(applicationWithEntities.entities.find(entity => entity.microserviceName === baseName) || microfrontends.includes(baseName))
);
const { serverPort: gatewayServerPort, clientFramework: gatewayClientFramework } = applicationWithEntities.config;
if (relatedApplications.length > 0) {
applicationWithEntities.config.applications = Object.fromEntries(
relatedApplications.map(([baseName, config]) => {
config.gatewayServerPort = config.gatewayServerPort || gatewayServerPort;
const { clientFramework, serverPort, applicationIndex, devServerPort } = config;
return [baseName, { clientFramework, serverPort, applicationIndex, devServerPort }];
})
);
const differentClientFrameworks = relatedApplications.filter(
([_baseName, { clientFramework }]) => clientFramework && clientFramework !== 'no' && clientFramework !== gatewayClientFramework
);
if (differentClientFrameworks.length > 0) {
throw Error(
`Using different client frameworks in microfrontends is not supported. Tried to use: ${gatewayClientFramework} with ${differentClientFrameworks
.map(([baseName, { clientFramework }]) => `${clientFramework} (${baseName})`)
.join(', ')}`
);
}
}
});
if (this.interactive) {
return applicationsWithEntities.reduce((promise, applicationWithEntities) => {
return promise.then(() => callGenerator(applicationWithEntities));
}, Promise.resolve());
}
return Promise.all(applicationsWithEntities.map(applicationWithEntities => callGenerator(applicationWithEntities)));
}
generateDeployments() {
if (!shouldGenerateDeployments(this)) {
logger.debug('Deployments not generated');
return Promise.resolve();
}
logger.info(
`Generating ${this.importState.exportedDeployments.length} ` +
`${pluralize('deployment', this.importState.exportedDeployments.length)}.`
);
const callDeploymentGenerator = () => {
const callGenerator = deployment => {
try {
return generateDeploymentFiles({
processor: this,
deployment,
});
} catch (error) {
logger.error(`Error while generating deployments from the parsed JDL\n${error}`, error);
throw error;
}
};
// Queue callGenerator in chain
return this.importState.exportedDeployments.reduce((promise, deployment) => {
return promise.then(() => callGenerator(deployment));
}, Promise.resolve());
};
return callDeploymentGenerator();
}
generateEntities(env) {
if (this.importState.exportedEntities.length === 0 || shouldGenerateApplications(this)) {
logger.debug('Entities not generated');
return Promise.resolve();
}
try {
logger.info(
`Generating ${this.importState.exportedEntities.length} ${pluralize('entity', this.importState.exportedEntities.length)}.`
);
return generateEntityFiles(this, this.importState.exportedEntities, env);
} catch (error) {
logger.error(`Error while generating entities from the parsed JDL\n${error}`, error);
throw error;
}
}
}
/**
* Import-JDL sub generator
* @param {any} jdlFiles jdl files
* @param {any} [options] options passed from CLI
* @param {any} [env] the yeoman environment
*/
module.exports = (jdlFiles, options = {}, env, _envBuilder, createEnvBuilder = EnvironmentBuilder.createDefaultBuilder) => {
logger.info(chalk.yellow(`Executing import-jdl ${options.inline ? 'with inline content' : jdlFiles.join(' ')}`));
logger.debug(chalk.yellow(`Options: ${toString({ ...options, inline: options.inline ? 'inline content' : '' })}`));
try {
const jdlImporter = new JDLProcessor(jdlFiles, options.inline, options, createEnvBuilder);
jdlImporter.importJDL();
jdlImporter.sendInsight();
jdlImporter.config();
const generateJdl = () =>
jdlImporter
.generateApplications()
.then(() => jdlImporter.generateEntities(env))
.then(() => jdlImporter.generateDeployments());
let generation;
if (!options.workspaces || !multiplesApplications(jdlImporter)) {
generation = generateJdl();
} else {
// Wrap generation inside workspaces root generation.
// generate applications after git initialization and before root npm install
generation = jdlImporter.generateWorkspaces(options, generateJdl);
}
return generation.then(() => {
printSuccess();
return jdlFiles;
});
} catch (e) {
logger.error(`Error during import-jdl: ${e}`, e);
return Promise.reject(new Error(`Error during import-jdl: ${e.message}`));
}
};