输入提示和选项yeoman

时间:2019-09-10 23:07:14

标签: javascript yeoman yeoman-generator

我是编程新手,并且正在使用yeoman-generator创建一个生成器。如何遍历对象数组并选择我选择的选项?我尝试过,但是没有用。你能帮我吗?


prompting() {

const prompts = [
 {
   type: 'checkbox',
   name: 'database',
   message: 'Select Database support:',
   choices: [
                {
                    name: 'H2',
                    value: 'h2',
                }, {
                    name: 'HSQLDB',
                    value: 'hsqldb'

                }, {
                    name: 'Apache Derby',
                    value: 'derby'
                },
    ]
    return this.prompt(prompts).then(answers => {
       this.database = answers.database;
       const hasDataBase = db => this.database.indexOf(db) !== -1;
       this.h2 = hasDataBase('h2');
       this.hsqldb = hasDataBase('hsql');
       this.derby = hasDataBase('derby');
    });
}
]}


1 个答案:

答案 0 :(得分:0)

这是它可以采用的格式(来自示例页面https://yeoman.io/authoring/user-interactions.html)。我自己对此进行了测试,并且可以正常工作。

var Generator = require('yeoman-generator');

module.exports = class extends Generator {
  async prompting() {
    const prompts = await this.prompt([
      {
        type: 'checkbox',
        name: 'database',
        message: 'Select Database support:',
        choices: [
          {
            name: 'H2',
            value: 'h2',
          }, {
            name: 'HSQLDB',
            value: 'hsqldb'

          }, {
            name: 'Apache Derby',
            value: 'derby'
          }
        ]
      }]);

    this.log("database", prompts.database);
  }
};