Yeoman根据用户输入重复提示

时间:2019-07-19 19:30:20

标签: generator yeoman prompt

例如,我想询问用户面包的类型:

        {
            type: 'list',
            name: 'breadType',
            message: `What type of bread do you want?`,
            choices: response => {
                const breadOptions = [
                    {
                        value: 'wheat',
                        name: 'Wheat Bread'
                    },
                    {
                        value: 'white',
                        name: 'White Bread'
                    }
                ];
                return breadOptions;
            },
            default: 0
        }

然后,我将根据所需的浇头数量要求浇头:

    {
        when: response => response.breadType,
        type: 'input',
        name: 'numberOfToppings',
        message: 'Please enter how many toppings you want: '
    }

我如何多次提示用户输入浇头数量?:

 {
        when: response => response.numberOfToppings,
        type: 'input',
        name: 'toppingChoices',
        message: 'Please provide your topping(s): '
 }

样品输入

? Please enter how many toppings you want: 4
? Please provide your topping(s):cheese
? Please provide your topping(s):onions
? Please provide your topping(s):pickles
? Please provide your topping(s):tomatoes

我不熟悉yeoman语法,请帮忙。

1 个答案:

答案 0 :(得分:2)

我制作了一个简单的生成器来解决您的问题:

const Generator = require('yeoman-generator');
let toppings = [];

module.exports = class extends Generator {
    async prompting() {
        const answers = await this.prompt([
            {
                type: 'input',
                name: 'numberOfToppings',
                message: 'Please enter how many toppings you want: ',
            }
        ]);

        var length = answers.numberOfToppings;
        for(var i = 0; i < length; i++) {
            const answers2 = await this.prompt([
                {
                    type: 'input',
                    name: 'toppings',
                    message: 'Please provide your topping(s):',
                }
            ]);
            toppings.push(answers2.toppings);
        }
        console.log('Array: ' + toppings);
    }
};