2017-12-18 22 views
0

私は、ユーザにパラメータを入力するようにジェネレータを作成しています。optionとしましょう。その答えに応じて、私は、出力ファイルのうち1つを変更したい: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 promted attribute 
    private String ${info}; 

どのように私は上記のコメントに記載されている作業を行うことができますか?ここでは、すべてのあなたのプロンプトを宣言し、名前のプロンプトの種類が含まprompting()方法で

答えて

0

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

テンプレート/ MyClass.java

public class MyClass{ 

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

} 
+0

おっと、私はこれが21時間前に頼まれたことが表示されませんでした。 –

+0

こんにちは、私はあなたの答えを感謝し、私は自分自身と同時にあなたに答えました。とにかく、私はあなたの助けに時間を割いて受け入れます;-) – codependent

+0

ええ、あなたのアップデートによってこの質問がリストの一番上に表示されている可能性があります。とにかく、乾杯! –

0

これは私がそれに解決する方法です:

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); 
    } 
} 
関連する問題