最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Yeoman Generator Prompts - Stack Overflow

programmeradmin0浏览0评论

I am building a yeoman generator, and need to assign an additional value/answer when a prompt is answered.

I have found a way to prompt user with another question, but what I need is to assign a predefined answer automatically in the background - so user wont see it happening, not ask the user another question. Here's an example below. Also need to do this for a list of 12+ values so the 'when' mand below isn't ideal as I would have to have the when statement 12+ times

   this.prompt([{
      type: 'list',
      name: 'redWhite',
      message: 'what colour',
      choices: ['red', 'white', 'blue', 'black', 'green', 'yellow', 'purple', 'cyan', 'magenta', 'brown']
    }, {
      when: 'redWhite.red',
      type: 'confirm',
      name: 'blue',
      message: 'Red is nice, but how about blue instead?'
    }, 

  /*So instead of prompting user again, just need to assign a predefined value here
   , {
      when: 'redWhite.red',
      answer: redFooBar
    }, */

    {
      when: 'redWhite.white',
      type: 'confirm',
      name: 'green',
      message: 'White is nice, but how about green instead?'
    }, {
      name: 'otherColors',
      message: 'What other colors do you like?'
    }], function (answer) {
      // answer = {
      //   redWhite: 'red',
      //   blue: false,
      //   green: false,
      //   otherColors: 'pink, purple-ish'
      // };
    }); 

I am building a yeoman generator, and need to assign an additional value/answer when a prompt is answered.

I have found a way to prompt user with another question, but what I need is to assign a predefined answer automatically in the background - so user wont see it happening, not ask the user another question. Here's an example below. Also need to do this for a list of 12+ values so the 'when' mand below isn't ideal as I would have to have the when statement 12+ times

   this.prompt([{
      type: 'list',
      name: 'redWhite',
      message: 'what colour',
      choices: ['red', 'white', 'blue', 'black', 'green', 'yellow', 'purple', 'cyan', 'magenta', 'brown']
    }, {
      when: 'redWhite.red',
      type: 'confirm',
      name: 'blue',
      message: 'Red is nice, but how about blue instead?'
    }, 

  /*So instead of prompting user again, just need to assign a predefined value here
   , {
      when: 'redWhite.red',
      answer: redFooBar
    }, */

    {
      when: 'redWhite.white',
      type: 'confirm',
      name: 'green',
      message: 'White is nice, but how about green instead?'
    }, {
      name: 'otherColors',
      message: 'What other colors do you like?'
    }], function (answer) {
      // answer = {
      //   redWhite: 'red',
      //   blue: false,
      //   green: false,
      //   otherColors: 'pink, purple-ish'
      // };
    }); 
Share Improve this question asked Aug 25, 2014 at 17:30 SeakDigitalSeakDigital 731 silver badge7 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

You dont need the when function here, as it just prompts the user to answer another question.

You can simply use arrays in the prompts callback function to get multiple values for one prompted answer, like so

//prompt user to answer questions
this.prompt([{
        type: 'list',
        name: "fruit",
        message: "What is your favourite fruit",
        choices: [{
            name: 'Apple',
            value: ['apple', 'Apple Juice', 'Apple Pie' ]
        }, {
            name: 'Banana',
            value: ['banana','Banana Juice','Banana Bread']
        }, {
            name: 'Oranges',
            value: ['oranges','Orange Juice','Orange Pudding']
        }]
    }]);

//confirming the prompts and storing answers - pull required value from array number
this.prompt(prompts, function(answers) {
  this.fruitChoice = answers.fruit[0];
  this.fruitDrink = answers.fruit[1];
  this.fruitDessert = answers.fruit[2];
}
发布评论

评论列表(0)

  1. 暂无评论