Search code examples
javascripttemplatingyeoman-generator

When creating my own custom yeoman generator, how do I use a variable from the prompt in my Gruntfile?


When building a yeoman generator. Given, my index.js contains:

...
prompting: function () {
    var done = this.async();

    // have Yeoman greet the user
    this.log(this.yeoman);

    var questions = [
        {
            name: 'value',
            message: 'Give me some value?'
        },
    ];

    this.prompt(questions, function (answers) {
        this.value = answers.value;
        done();
    }.bind(this));
},
...

writing: function () {
    this.copy("_Gruntfile.js", "Gruntfile.js");
}
...

and my answer to the prompt 'Give me some value?' is "test". How do I use the value from the prompt in the _Gruntfile to generate:

 uglify: {
       dist: {
         files: {
           '<%= yeoman.dist %>/test.min.js': ['<%= yeoman.dist %>/test.js']
         }
       }
    },

I've tried:

uglify: {
       dist: {
         files: {
           '<%= yeoman.dist %>/<%% value%>.min.js': ['<%= yeoman.dist %>/<%value%>.js']
         }
       }
    },

and

uglify: {
       dist: {
         files: {
           '<%= yeoman.dist %>/<%= value%>.min.js': ['<%= yeoman.dist %>/<%= value%>.js']
         }
       }
    },

to get the generator to replace <%% value%> or <%= value%> with "test" but nothing happens when it's run. The second option works when used in my _bower.json. e.g "name": "<%= value%>" becomes "name": "test"


Solution

  • this.copy() just copies the file, you should instead use this.template() to parse it:

    this.template("_Gruntfile.js", "Gruntfile.js");