Yeoman Generator: CLI+File Instead of Prompt

2019-05-10 18:31发布

问题:

I've been using a few Yeoman Generators that prompt me for user input. I'd prefer to put my inputs in a JSON file though. I can see that yo-rc.json gets generated afterwards, but I'd like to use that (or a file like it) as an input to Yeoman.

Example using JHipster:

Current Behavior

$ yo jhipster

Welcome to the JHipster Generator v2.16.1
? (1/15) What is the base name of your application? (jhipster) helpme
? (2/15) What is your default Java package name? com.mycompany.helpme
...

# Yeoman Generator creates project via user inputs

Desired Behavior

$ cat my-custom.json
{
  "generator-jhipster": {
    "baseName": "helpme",
    "packageName": "com.mycompany.helpme",
    ...

$ yo jhipster --file my-custom.json
...

# Yeoman Generator creates project via input file

It sounds like I should be able to leverage the Yeoman Storage API, but I haven't personally succeeded with that route, nor can I find any similar examples.

[Edit] Next Steps

Next I wanted to generate entities, unprompted, with complex relationships per (https://jhipster.github.io/managing_relationships.html). I found this to be a 2-step process:

  1. Create ./.jhipster/MyEntity.json
  2. yo jhipster:entity MyEntity.json
  3. Profit

回答1:

Jhipster already does that see my comment on your question. Below is where jhipster reads the .yo-rc.json, if you really want any other name it can be done as well, you just need to read that file using file api, but I would suggest you keep your json named .yo-rc.json for compatibility

Code from app/index.js

this.baseName = this.config.get('baseName');
   this.packageName = this.config.get('packageName');
   this.authenticationType =  this.config.get('authenticationType');
   this.clusteredHttpSession = this.config.get('clusteredHttpSession');
   this.searchEngine = this.config.get('searchEngine');
   this.websocket = this.config.get('websocket');
   this.databaseType = this.config.get('databaseType');
   if (this.databaseType == 'mongodb') {
       this.devDatabaseType = 'mongodb';
       this.prodDatabaseType = 'mongodb';
       this.hibernateCache = 'no';
   } else if (this.databaseType == 'cassandra') {
       this.devDatabaseType = 'cassandra';
       this.prodDatabaseType = 'cassandra';
       this.hibernateCache = 'no';
   } else { // sql
       this.devDatabaseType = this.config.get('devDatabaseType');
       this.prodDatabaseType = this.config.get('prodDatabaseType');
       this.hibernateCache = this.config.get('hibernateCache');
}
   this.useCompass = this.config.get('useCompass');
   this.javaVersion = this.config.get('javaVersion');
   this.buildTool = this.config.get('buildTool');
   this.frontendBuilder =   this.config.get('frontendBuilder');
   this.rememberMeKey = this.config.get('rememberMeKey');
   this.enableTranslation = this.config.get('enableTranslation'); // this is enabled by default to avoid conflicts for existing applications
   this.packagejs = packagejs;