有没有办法为提示的输入问题做一个 while 循环,绑定它们并在数组中提供所有答案?

is there a way to do a while loop for prompted input questions, bind them and have all answers available in an array?

我正在构建一个 Yeoman 生成器,它所需的依赖项来自 https://github.com/sboudrias/mem-fs-editor#copytplfrom-to-context-settings and https://github.com/SBoudrias/Inquirer.js/

我们的想法是能够向用户提出问题并重复相同的问题,即您想添加另一个问题...如果用户添加另一个问题,那么它将绑定并记录该答案,如果用户说'no' 或点击 return 提示将停止。

然后我想将所有答案绑定到一个数组,该数组可以传递给另一个对象函数,以便它可以将响应列为数组。

到目前为止,这是代码... 首先是提示:

askForTest1: function () {
    if (this.type == 'foundation5') {
        var cb = this.async();

        var prompts = {
            type: 'input',
            name: 'test1',
            message: chalk.yellow('  What is your favorite movie'),
            default: 'Star Wars!'
        };

        this.prompt(prompts, function (props) {
            this.templatedata.test1 = props.test1;

            cb();
        }.bind(this));
    }
},

然后是将绑定模板构建选项的 copyTpl 对象:这是我希望出现的所需输出...请记住,此副本 tpl 与提示。即这个模板...

       this.fs.copyTpl(
              this.templatePath('/index2.html'),
              this.destinationPath('app/index2.html'),
              { title: [this.templatedata.test1-a, this.templatedata.test1-b, this.templatedata.test1-c, ...], h1: this.applicationName }
            );

结果...带有此代码的模板...

使用

会产生这个...

using foo1
using foo2

这可能吗?我该怎么做。

这是相当简单的编程任务。

使用向用户提问的方法的递归。然后,如果用户回答 "yes add more",您只需再次调用相同的函数即可。

大致是这样的:

initializing: function () {
  this.movies = [];
},

askMovie: function (cb) {
  cb = cb || this.async();

  var prompts = [{
      type: 'input',
      name: 'movie',
      message: chalk.yellow('  What is your favorite movie'),
      default: 'Star Wars!'
  }, {
    type: 'confirm',
    name: 'askAgain',
    message: 'ask again?'
  }];

  this.prompt(prompts, function (props) {
    this.movies.push(props.movie)
    if (props.askAgain) {
      this.askMovie(cb);
    } else {
      cb();
    }
  }.bind(this));
  }
}

像这样的东西应该适合你:

function() {
    var answers = {
        times: [],
        title: undefined,
        type: undefined
    };

    function promptMe(prompt, cb) {
        self.prompt(prompt, function (props) {
            if(props.answer!= "done") {
                answers.times.push(props.time);
                promptMe(prompt, cb);
            } else {
                cb();
            }
        });
    }

    var cb = this.async(),
        self = this,
        movieTypePrompt = {
            type: 'input',
            name: 'movieType',
            message: chalk.yellow('What is your favorite type of movie?'),
            default: 'Action'
        },
        movieTitilePrompt = {
            type: 'input',
            name: 'movieTitle',
            message: chalk.yellow('What is your favorite movie?'),
            default: 'Tron: Legacy'
        }
        movieTimePrompt = {
            type: 'input',
            name: 'time',
            message: chalk.yellow('When can you watch a movie? (enter \'done\' when you are done entering times)'),
            default: '8PM'
        };

    //Prompt for movie type and title
    this.prompt([movieTypePrompt, movieTitlePrompt], function (props) {
        answers.title = props.movieTitle;
        answers.type = props.movieType;

        //Repeatedly prompt for movie times
        promptMe(moviePrompt, function() {
            console.log('done');

            cb();
        });
    }.bind(this));
}