Yeoman Generator:CLI+文件而不是提示
Yeoman Generator: CLI+File Instead of Prompt
我一直在使用一些提示我进行用户输入的 Yeoman 生成器。不过,我更愿意将我的输入放在 JSON 文件中。我可以看到 yo-rc.json
随后生成,但我想使用它(或类似的文件)作为 Yeoman 的输入。
示例使用 JHipster:
当前行为
$ 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
期望的行为
$ 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
听起来我应该可以利用 Yeoman Storage API,但我个人并没有成功使用该路线,也找不到任何类似的例子。
[编辑]后续步骤
接下来,我想自动生成具有复杂关系的实体 (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 = '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 生成器。不过,我更愿意将我的输入放在 JSON 文件中。我可以看到 yo-rc.json
随后生成,但我想使用它(或类似的文件)作为 Yeoman 的输入。
示例使用 JHipster:
当前行为
$ 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
期望的行为
$ 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
听起来我应该可以利用 Yeoman Storage API,但我个人并没有成功使用该路线,也找不到任何类似的例子。
[编辑]后续步骤
接下来,我想自动生成具有复杂关系的实体 (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 = '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;