在子生成器中使用composeWith选项

时间:2015-04-22 20:51:30

标签: yeoman yeoman-generator

我正在整理一个Yeoman Generator并且无法理解composeWith()的options参数是如何工作的。我的目标是将用户输入从主生成器的提示传递给子生成器,我认为选项是这样做的。

我的主要生成器的提示看起来像这样:

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

我已经尝试在我的子生成器中使用构造函数中的参数设置局部变量(因为我假设这是选项所在的位置),如下所示:

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

但是这没用。我试过控制台记录参数变量,它显示选项是一个对象,但它似乎是空的。

这是我如何将用户输入通过生成器传递给另一个,还是有其他方法可以做到这一点?

1 个答案:

答案 0 :(得分:7)

您的子生成器需要像编写正常期望的选项一样编写。

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

    this.option('name');
  },

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

有关详细信息,请参阅user interaction documentation

相关问题