带有选项 yeoman 的输入提示

Input prompt with options yeoman

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


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');
    });
}
]}


这是它可以采用的格式(来自他们的示例页面 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);
  }
};