Yeoman Generator:CLI +文件而不是提示 [英] Yeoman Generator: CLI+File Instead of Prompt
问题描述
我一直在使用一些Yeoman Generators来提示我输入用户信息。不过,我希望将输入内容放入JSON文件中。我可以看到 yo-rc.json
之后会生成,但是我想使用它(或类似的文件)作为Yeoman的输入。
使用 JHipster 的示例:
当前行为
$ yo jhipster
欢迎使用JHipster Generator v2 .16.1
? (1/15)您的应用程序的基本名称是什么? (jhipster)helpme
吗? (2/15)您的默认Java软件包名称是什么? com.mycompany.helpme
...
#Yeoman Generator通过用户输入创建项目
期望的行为
$ cat my-custom.json
{
generator-jhipster:{
baseName: helpme,
packageName: com.mycompany.helpme,
...
$ yo jhipster --file my-custom.json
...
#Yeoman Generator通过输入文件
听起来我应该能够利用 Yeoman Storage API ,但我个人还没有成功完成这条路线,也找不到任何类似的例子。
[Edit]后续步骤
接下来,我想生成没有提示的实体,每个实体具有复杂的关系( https: //jhipster.github.io/managing_relationships.html )。我发现这是一个两步过程:
- 创建
./。jhipster / MyEntity.json
-
yo jhipster:entity MyEntity.json
- 利润
Jhipster已经做到了,可以看到我对您的问题的评论。
下面是jhipster读取.yo-rc.json的地方,如果您确实想要其他名称,也只需使用文件api读取该文件,但我建议您保留json命名为.yo-rc.json以实现兼容性
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 =否;
}否则,如果(this.databaseType ==‘cassandra’){
this.devDatabaseType =‘cassandra’;
this.prodDatabaseType ='cassandra';
this.hibernateCache =否;
} 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.packagejs = packagejs;
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:
- Create
./.jhipster/MyEntity.json
yo jhipster:entity MyEntity.json
- Profit
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;
这篇关于Yeoman Generator:CLI +文件而不是提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!