From 1cfb3f06e1f87fb12200ca593d88c8f7df7efb23 Mon Sep 17 00:00:00 2001 From: Jonathan Ruddell Date: Sat, 17 Feb 2018 14:56:10 -0500 Subject: [PATCH] add import-jdl subgenerator Fix #31 --- boilerplate/ignite.json.ejs | 1 + commands/import-jdl.js | 2 ++ docs/generators-and-plugins.md | 10 ++++-- docs/upgrading.md | 2 ++ package.json | 1 + src/entity/index.js | 2 +- src/import-jdl/index.js | 62 ++++++++++++++++++++++++++++++++++ 7 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 commands/import-jdl.js create mode 100644 src/import-jdl/index.js diff --git a/boilerplate/ignite.json.ejs b/boilerplate/ignite.json.ejs index 53444ecbd..c7cbac4a4 100644 --- a/boilerplate/ignite.json.ejs +++ b/boilerplate/ignite.json.ejs @@ -18,5 +18,6 @@ "redux": "ignite-ir-boilerplate", "saga": "ignite-ir-boilerplate", "screen": "ignite-ir-boilerplate" + "import-jdl": "ignite-jhipster", } } diff --git a/commands/import-jdl.js b/commands/import-jdl.js new file mode 100644 index 000000000..6a9de2620 --- /dev/null +++ b/commands/import-jdl.js @@ -0,0 +1,2 @@ +// @cliDescription Imports a JHipster JDL file and generates the entities within the file +module.exports = require('../src/import-jdl') diff --git a/docs/generators-and-plugins.md b/docs/generators-and-plugins.md index affdd8f8a..16d2c4032 100644 --- a/docs/generators-and-plugins.md +++ b/docs/generators-and-plugins.md @@ -1,10 +1,16 @@ ### Generators -##### JHipster Entity Generator +##### Custom Generators - Entity - `ignite generate entity ` - Prompts for the path to the entity's config (`.jhipster` folder in your app) - - Generates all files needed for fetching and displaying the entity. + - Generates all files needed for fetching and displaying the entity - Includes the API endpoints, redux/saga config, and the user interface + - Import JDL - `ignite generate import-jdl ` + - Import several entities at once using JDL + - Runs the entity generator for each entity present in the JDL + - Upgrade - `ignite generate upgrade` + - Upgrades your generated app to the latest template code + - Use a branch to merge just the updates into your code ##### Ignite Generators This generator adds Ignite's usual generators to the mix as well. We have access to: diff --git a/docs/upgrading.md b/docs/upgrading.md index f66d5a202..fa223aff8 100644 --- a/docs/upgrading.md +++ b/docs/upgrading.md @@ -5,3 +5,5 @@ - Make sure to upgrade ignite-jhipster by running `npm upgrade ignite-jhipster` - Use `ignite g upgrade` to upgrade any template files to their updated boilerplate code. - It's recommended to use `git` and branches to merge changes into your code. + - The command below will keep all of your changes while merging any updates. If there are conflicts, you will need to manually merge the changes. + - `git merge -s recursive -Xours ` diff --git a/package.json b/package.json index f9fa88a25..f28116cf8 100644 --- a/package.json +++ b/package.json @@ -48,6 +48,7 @@ "babel-eslint": "7.2.3", "fs-jetpack": "1.0.0", "jest": "21.0.2", + "jhipster-core": "1.4.6", "np": "2.15.0", "sinon": "2.3.1", "standard": "10.0.2", diff --git a/src/entity/index.js b/src/entity/index.js index 463651d22..6c09bcbc2 100644 --- a/src/entity/index.js +++ b/src/entity/index.js @@ -32,7 +32,7 @@ module.exports = async function (context) { // if the file exists, skip loading it if (fs.existsSync(localEntityFilePath)) { - print.success(`Found the entity config locally in .jhipster`) + print.success(`Found the ${this.name} entity config locally in .jhipster`) } else if (jhDirectoryFlag) { if (!fs.existsSync(`${jhDirectoryFlag}/${localEntityFilePath}`)) { print.error(`No entity configuration file found at ${jhDirectoryFlag}/${localEntityFilePath}`) diff --git a/src/import-jdl/index.js b/src/import-jdl/index.js new file mode 100644 index 000000000..74de36943 --- /dev/null +++ b/src/import-jdl/index.js @@ -0,0 +1,62 @@ +const fs = require('fs-extra') +const Insight = require('../lib/insight') +const jhiCore = require('jhipster-core') + +module.exports = async function (context) { + // grab some features + const { parameters, print, strings, system } = context + const { isBlank } = strings + + // validation + if (isBlank(parameters.first)) { + print.info(`${context.runtime.brand} generate import-jdl \n`) + print.info('A JDL filename is required.') + return + } + + // load the ignite config and set the default jhipster directory + const jdlFiles = parameters.array + print.info('The jdl is being parsed.') + const jhipsterConfig = await fs.readJson(`.jhipster/yo-rc.json`) + const prodDatabaseType = jhipsterConfig['generator-jhipster'].prodDatabaseType + const applicationType = jhipsterConfig['generator-jhipster'].applicationType + const baseName = jhipsterConfig['generator-jhipster'].baseName + try { + const jdlObject = jhiCore.convertToJDLFromConfigurationObject({ + document: jhiCore.parseFromFiles(jdlFiles), + databaseType: prodDatabaseType, + applicationType: applicationType, + applicationName: baseName + }) + const entities = jhiCore.convertToJHipsterJSON({ + jdlObject, + databaseType: prodDatabaseType, + applicationType: applicationType + }) + print.info('Writing entity JSON files.') + this.changedEntities = jhiCore.exportEntities({ + entities, + forceNoFiltering: false + }) + + this.updatedKeys = Object.keys(this.changedEntities) + if (this.updatedKeys.length > 0) { + print.info(`Updated entities: ${this.updatedKeys}`) + } else { + print.info('No change in entity configurations. No entities were updated') + } + + // generate update entities + for (let i = 0; i < this.updatedKeys.length; i++) { + await system.spawn(`ignite g entity ${this.updatedKeys[i]}`, { stdio: 'inherit' }) + } + + print.success(`JDL successfully imported!`) + } catch (e) { + print.error('\nError while parsing entities from JDL\n') + if (e && e.message) { + print.error(`${e.name || ''}: ${e.message}`) + } + } + Insight.trackGenerator(context, 'import-jdl') +}