【问题标题】:Yeoman generator - How to change the content of some file depending on a prompted paramYeoman 生成器 - 如何根据提示的参数更改某些文件的内容
【发布时间】:2017-12-20 05:26:05
【问题描述】:

我正在编写一个生成器,其中将提示用户一个参数,我们称之为option。根据其答案,我想更改其中一个输出文件:

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};

如何进行上述 cmets 中描述的操作?

【问题讨论】:

    标签: javascript node.js yeoman yeoman-generator


    【解决方案1】:

    index.js

    prompting() 方法中,您将声明所有提示以及包含名称的提示类型。然后在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
          }
        );
      } 
    };
    

    templates/MyClass.java

    public class MyClass{
    
        <% if (x) { %>
        private String <%= info %>;
        <% } else { %>
        private String field1;
        <% } %>
    
    }
    

    【讨论】:

    • 糟糕,我没有看到这是 21 小时前提出的。
    • 您好,感谢您的回答,我和您同时回答了自己。不管怎样,我会接受你花时间帮忙的;-)
    • 是的,您的更新可能已将此问题推到列表的顶部,所以我认为这是一个新问题。无论如何,干杯!
    【解决方案2】:

    我就是这样解决的:

    SomeClass.java:

    public class SomeClass{
    
        <%_ if (option == 'x') { _%>
        private String field1;
        <%_ } _%>
    
        private String <%= nombre %>;
    

    这是生成器代码:

    module.exports = class extends Generator {
      prompting() {
        // Have Yeoman greet the user.
        this.log(yosay(
          'Welcome to the super-duper ' + chalk.red('generator-mygenerator') + ' generator!'
        ));
    
        const prompts = [{
          type: 'input',
          name: 'option',
          message: 'Option?',
          default: 'x'
        },
        {
          type: 'input',
          name: 'info',
          message: 'Field name?',
          default: 'field'
        }
        ];
    
        return this.prompt(prompts).then(props => {
          this.props = props;
        });
      }
    
      writing() {
        this.fs.copyTpl(
          this.templatePath('SomeClass.java'),
          this.destinationPath('SomeClass.java'), this.props);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多