用Yeoman发生器重复提示 [英] Repeating Prompts with a Yeoman Generator
问题描述
我正在创建一个Yeoman Generator来自动创建几个数据库表。我需要为用户提供添加多个列的提示(下面是ColumnName和DataType的组合)。
I am creating a Yeoman Generator to automate creation of few database tables. I need to provide users with a prompt to add multiple columns (combination of ColumnName and DataType below).
我在我的磁盘中保存了一个模板,我绑定了动态名称从用户输入并基于此模板,最终脚本由Yeoman Generator生成。你能建议如何提示用户输入他想要输入的ColumnName / DataType的重复组合吗?
I have a template saved in my disk where I bind the dynamic names from the user inputs and based upon this template, the final script is generated by the Yeoman Generator. Can you suggest how to prompt user to enter repetitive combinations of ColumnName/DataType he wants to enter?
var prompts = [{
type: 'input',
name: 'name',
message: 'The Table Name?'
}, {
type: 'input',
name: 'attributeName',
message: 'Define your Schema - ColumnName?',
default: 'ID'
},{
type: 'input',
name: 'attributeType',
message: 'Define your Schema - DataType?',
default: 'S'
}
];
return this.prompt(prompts).then(function (props) {
this.props = props;
}.bind(this));
模板内容 -
用户可以输入1/2/3/4或更多列,一旦他这样做,下面的模板应足够智能,以创建许多列键组合。
Template content -- User can enter details of 1/2/3/4 or more columns and once he does that the template below should be intelligent enough to create that many column key combinations.
{
"Type" : "AWS::Table",
"Properties" : {
"AttributeDefinitions" :
{
"AttributeName" : "<%= attributeName %>",
"AttributeType" : "<%= attributeType %>"
},
{
"AttributeName" : "<%= attributeName %>",
"AttributeType" : "<%= attributeType %>"
},
"TableName" : <%= name %>,
}
}
推荐答案
您可以在提示中添加递归函数()
钩。必须确保递归函数返回 this.prompts
否则执行可能会停止。
You can add a recursive function inside the prompting()
hook. It has to be made sure that the recursive function returns this.prompts
else the execution might stop.
我的策略如下工作
-
声明一个基于输入之一重复的递归函数
Declare a recursive function that repeats based on one of the input
在 this.columns中递归时填充道具
将此实例变量传递给中的模板写入()
hook
Pass this instance variable to the template in writing()
hook
迭代<$ c $模板中的c> this.columns 并填充列
this.columns中的第一个条目
将有表名和第一列详细信息
First entry in this.columns
will have the table name and the first column details
检查下面的代码,你可以只要按预期调用递归函数,就可以根据需要调整它。
Check the code below, you can tweak this to your needs as long as the recursive function is invoked as expected.
还有一个额外的提示,询问是否重复。它也可以用一些逻辑丢弃,但这取决于你。
There is an extra prompt that asks whether to repeat or not. It can also be discarded with some logic but that is up to you.
提示()
prompting() {
// Have Yeoman greet the user.
this.log(yosay(
'Welcome to the remarkable ' + chalk.red('generator-react-starter-kit-relay-container') + ' generator!'
));
const tableNamePrompt = [{
type: 'input',
name: 'name',
message: 'The Table Name?'
}];
const columnPrompts = [{
type: 'input',
name: 'attributeName',
message: 'Define your Schema - ColumnName?',
default: 'ID'
}, {
type: 'input',
name: 'attributeType',
message: 'Define your Schema - DataType?',
default: 'S'
}, {
type: 'confirm',
name: 'repeat',
message: 'Do you want to add more columns?',
default: 'Y'
}]
this.columns = [];
const loop = (relevantPrompts) => {
return this.prompt(relevantPrompts).then(props => {
this.columns.push(props);
return props.repeat ? loop(columnPrompts) : this.prompt([]);
})
}
return loop([...tableNamePrompt, ...columnPrompts]);
}
然后在写()
hook传递您之前填充的列
实例变量。
And then in the writing()
hook pass the columns
instance variable that you populated earlier.
writing()
writing() {
this.fs.copyTpl(
this.templatePath('Schema.json'),
this.destinationPath('./Schema.json'),
{
columns: this.columns
}
);
}
模板
{
"Type" : "AWS::Table",
"Properties" : {
"AttributeDefinitions" : [
<% for (let i=0; i<columns.length; i++) { %>
{
"AttributeName": "<%= columns[i].attributeName %>",
"AttributeType": "<%= columns[i].attributeType %>"
}
<% } %>
],
"TableName" : "<%= (columns[0] || {}).name %>"
}
}
SAMPLE INPUT
_-----_ ╭──────────────────────────╮
| | │ Welcome to the │
|--(o)--| │ remarkable │
`---------´ │ generator-react-starter- │
( _´U`_ ) │ kit-relay-container │
/___A___\ /│ generator! │
| ~ | ╰──────────────────────────╯
__'.___.'__
´ ` |° ´ Y `
? The Table Name? User
? Define your Schema - ColumnName? ID
? Define your Schema - DataType? Bigint
? Do you want to add more columns? Yes
? Define your Schema - ColumnName? Email
? Define your Schema - DataType? String
? Do you want to add more columns? Yes
? Define your Schema - ColumnName? Password
? Define your Schema - DataType? Text
? Do you want to add more columns? No
输出
{
"Type" : "AWS::Table",
"Properties" : {
"AttributeDefinitions" : [
{
"AttributeName": "ID",
"AttributeType": "Bigint"
}
{
"AttributeName": "Email",
"AttributeType": "String"
}
{
"AttributeName": "Password",
"AttributeType": "Text"
}
],
"TableName" : "User"
}
}
这篇关于用Yeoman发生器重复提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!