jhipster / jhipster-core

JHipster Domain Language, used by JHipster UML and JDL-Studio to generate entities
Apache License 2.0
346 stars 116 forks source link

Add command-line tools for converting from JDL to JSON and JSON to JDL #359

Closed mrts closed 4 years ago

mrts commented 5 years ago
Overview of the feature request

Add two command-line tools

The tools would be available after npm install jhipster-core (although jhipster-jdl-tools would be a more apt name for the package).

Motivation for or Use Case

This would make it possible to use JDL and JDL Studio for modelling in other frameworks besides JHipster; see for example a proposal of using JDL with the Django web framework here: https://github.com/django-extensions/django-extensions/issues/1416. Other frameworks would use JHipster JSON files as input, as JSON is easy to parse, so a command-line tool is needed for JDL to JSON conversion.

Conversion from JSON to JDL would help to convert models from other frameworks to JSON and then to JDL. It would also simplify importing JSON from existing JHipster projects to JDL Studio.

Implementation

Implementation is straightforward, here are draft blueprints (just to get the idea through, better error checking and configurability required):

const jhiCore = require('jhipster-core');

const files = process.argv.slice(2); const jdlImporter = new jhiCore.JDLImporter(files); const entities = jdlImporter.import().exportedEntities; console.log(JSON.stringify(entities);

- `json2jdl` (quoting [@cbornet](https://github.com/jhipster/jhipster-core/issues/64#issuecomment-251114625)):
```js
'use strict';

const parseJsonFromDir = require('jhipster-core').parseJsonFromDir;

let jdl = parseJsonFromDir('/path/to/jhipster/app/root/dir');
console.log(jdl.toString());
MathieuAA commented 5 years ago

Hi! Interesting idea that you have! What would the JSON content look like?

mrts commented 5 years ago

Thanks :)!

I think the JSON content could be simply an array of entities that have the existing JSON structure with added name field for the name of the entity, something like this:

[
  {
    "name": "BankAccount",
    "relationships": [
      ...
    ],
    "fields": [
      ...
    ],
    ...
  },
  {
    "name": "Operation",
    "relationships": [
      ...
    ],
    "fields": [
      ...
    ],
    ...
  }
]

And the result should be interoperable with existing .jhipster convention, so I would envision a command-line option that would either output a single file as above or multiple files according to .jhipster convention.

MathieuAA commented 4 years ago

Hi! I've read the entire thread here and in the django-extensions repo, and I believe that such CL tools shouldn't be there as JCore only is a lib (a provider).

Here are my points:

You've already written the code to perform what you need. You just need to create a project that performs such tasks, or include in another project. I'd be available to help if needed.