Javascript Yeoman生成器-如何根据提示的参数更改某些文件的内容
我正在编写一个生成器,其中会提示用户一个参数,我们称之为Javascript Yeoman生成器-如何根据提示的参数更改某些文件的内容,javascript,node.js,yeoman,yeoman-generator,Javascript,Node.js,Yeoman,Yeoman Generator,我正在编写一个生成器,其中会提示用户一个参数,我们称之为选项。根据其答案,我想更改其中一个输出文件: SomeClass.java public class SomeClass{ //if option=x I don't want to include this attribute: private String field1; //if option=x I want to generate an attribute with the value of the p
选项
。根据其答案,我想更改其中一个输出文件:
SomeClass.java
public class SomeClass{
//if option=x I don't want to include this attribute:
private String field1;
//if option=x I want to generate an attribute with the value of the promted attribute
private String ${info};
public class MyClass{
<% if (x) { %>
private String <%= info %>;
<% } else { %>
private String field1;
<% } %>
}
如何执行上述评论中描述的操作?index.js 在
提示()
方法中,您将声明所有提示以及包含名称的提示类型。然后在writing()
中,您将把它们传递给模板,这里是MyClass.java
module.exports = class extends Generator {
prompting() {
// Have Yeoman greet the user.
this.log(yosay(
'Welcome to the remarkable ' + chalk.red('generator-react-starter-kit-relay-container') + ' generator!'
));
const prompts = [{
type: 'input',
name: 'info',
message: 'INFO IN YOUR CLASS'
}, {
type: 'confirm',
name: 'x',
message: 'YOUR OPTION X'
}];
return this.prompt(prompts).then(props => {
this.props = props;
});
}
writing() {
this.fs.copyTpl(
this.templatePath('MyClass.js'),
this.destinationPath(<YOUR DESTINATION PATH>),
{
info: this.props.info,
x: this.props.x
}
);
}
};
module.exports=类扩展生成器{
提示(){
//让Yeoman问候用户。
这个.log(yosay(
“欢迎来到非凡的”+粉笔红('generator-react-starter-kit-relay-container')+“generator!”
));
常量提示=[{
键入:“输入”,
名称:“信息”,
信息:“你班上的信息”
}, {
键入:“确认”,
名称:“x”,
信息:“您的选项X”
}];
返回此.prompt(提示)。然后(props=>{
this.props=props;
});
}
写作(){
这个是.fs.copyTpl(
this.templatePath('MyClass.js'),
此.destinationPath(),
{
信息:this.props.info,
x:这是道具
}
);
}
};
模板/MyClass.java
public class SomeClass{
//if option=x I don't want to include this attribute:
private String field1;
//if option=x I want to generate an attribute with the value of the promted attribute
private String ${info};
public class MyClass{
<% if (x) { %>
private String <%= info %>;
<% } else { %>
private String field1;
<% } %>
}
公共类MyClass{
私有字符串;
私有字符串字段1;
}
我就是这样解决的:
SomeClass.java:
public class SomeClass{
<%_ if (option == 'x') { _%>
private String field1;
<%_ } _%>
private String <%= nombre %>;
哎呀,我没有看到21小时前有人问我这个问题。嗨,我感谢你的回答,我和你同时回答了我自己。无论如何,我会接受你花时间帮忙的;-)是的,你的更新可能会把这个问题推到列表的首位,所以我认为这是一个新问题。无论如何,干杯!