Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use composeWith options in sub-generator

I'm putting together a Yeoman Generator and having trouble understanding how the options parameter of composeWith() works. My goal is to pass user input from the main generator's prompt to sub-generators, and I thought options was the way of doing this.

My main generator's prompting looks something like this:

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

    var prompts = [
      {
        type    : 'input',
        name    : 'name',
        message : 'What is the name of your project?',
        default : this.appname // Default to current folder name
      }
    ];

    this.prompt(prompts, function (answers) {

        this.composeWith('app:subgenerator', {
          options: {
            name: answers.name;
          }
        },
        {
          local: require.resolve('generator-angular/app')
        });
        done();
    }.bind(this)
  )
}

I've tried in my sub-generator to set a local variable using the arguments in the constructor (because I assumed that's where the options would be), like this:

module.exports = generators.Base.extend({
  constructor: function () {
    generators.Base.apply(this, arguments);
    this.foo = arguments.options.name;
  }
}

But this didn't work. I tried console logging the arguments variable, and it shows that options is an object, but it appears to be empty.

Is this how I can pass user input through a generator to another, or is there another way to do that?

like image 407
Josh Vickerson Avatar asked Apr 22 '15 20:04

Josh Vickerson


1 Answers

Your sub-generator needs to be written just as you'd write a normal ones expecting options.

module.exports = generators.Base.extend({
  constructor: function () {
    generators.Base.apply(this, arguments);

    this.option('name');
  },

  initializing: function () {
    console.log(this.options.name)
  }
}

See the user interaction documentation for more details.

like image 184
Simon Boudrias Avatar answered Oct 17 '22 10:10

Simon Boudrias