Yeoman 根据用户输入重复提示
Yeoman repeat prompt based on user input
比如我想问用户面包的种类:
{
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 语法,请帮忙。
我做了一个简单的生成器来解决你的问题:
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);
}
};
比如我想问用户面包的种类:
{
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 语法,请帮忙。
我做了一个简单的生成器来解决你的问题:
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);
}
};