[英]Repeating Prompts with a Yeoman Generator
我正在创建一个Yeoman Generator,以自动创建一些数据库表。 我需要向用户提供添加多个列的提示(下面是ColumnName和DataType的组合)。
我有一个保存在磁盘中的模板,在其中绑定用户输入的动态名称,并基于此模板,由Yeoman Generator生成最终脚本。 您能否建议如何提示用户输入要输入的ColumnName / DataType的重复组合?
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或更多列的详细信息,一旦这样做,下面的模板应该足够智能,可以创建那么多列键组合。
{
"Type" : "AWS::Table",
"Properties" : {
"AttributeDefinitions" :
{
"AttributeName" : "<%= attributeName %>",
"AttributeType" : "<%= attributeType %>"
},
{
"AttributeName" : "<%= attributeName %>",
"AttributeType" : "<%= attributeType %>"
},
"TableName" : <%= name %>,
}
}
您可以在prompting()
挂钩内添加一个递归函数。 必须确保递归函数返回this.prompts
。提示运行可能会停止。
我的策略如下
声明一个基于输入之一重复的递归函数
在此this.columns
递归时填充道具。
将此实例变量传递给writing()
挂钩中的模板
this.columns
模板中的this.columns
并填充您的列
在第一个条目this.columns
将有表名和第一列细节
检查下面的代码,只要按预期调用递归函数,就可以根据需要进行调整。
有一个额外的提示,询问是否重复。 也可以通过某种逻辑将其丢弃,但这取决于您。
提示()
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]);
}
然后在writing()
挂钩中传递您先前填充的columns
实例变量。
写作()
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 %>"
}
}
样品输入
_-----_ ╭──────────────────────────╮
| | │ 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
OUTPUT
{
"Type" : "AWS::Table",
"Properties" : {
"AttributeDefinitions" : [
{
"AttributeName": "ID",
"AttributeType": "Bigint"
}
{
"AttributeName": "Email",
"AttributeType": "String"
}
{
"AttributeName": "Password",
"AttributeType": "Text"
}
],
"TableName" : "User"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.