简体   繁体   English

我可以在Yeoman子生成器中定义一个选项,然后从基本生成器中引用该选项吗?

[英]Can I define an option in a Yeoman subgenerator, and then reference that option from the base generator?

I have an EJS template file that is located in the base generator, but needs to be able to read options from both the base generator and the subgenerator. 我有一个EJS模板文件,该文件位于基本生成器中,但需要能够从基本生成器和子生成器中读取选项。

Base Generator 基础发电机

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

    this.option('option1');
  },

  initializing: function() {
    this.composeWith('base:sub-gen', this.options, {
      local: require.resolve('../sub-gen'),
      link: 'strong'
      });
  }
});

Subgenerator 子发电机

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

    this.option('option2');
  },
});

Template 模板

Base generator option: <%=this.options.option1 %>
Sub-generator option: <%=this.options.option2 %>

Is there any way to reference the sub-generator options from my ejs template? 有什么方法可以从我的ejs模板中引用子生成器选项吗? Barring that, how else can I make sure that my base generator and my sub-generator all have access to the same list of options? 除非如此,否则我如何才能确保我的基本生成器和子生成器都可以访问相同的选项列表? Maybe it's possible using .yo-rc.json ? 也许可以使用.yo-rc.json吗?

After working on it some more, I found a solution: 经过更多努力之后,我找到了解决方案:

  1. In both generators, set the configs in the configuring step, but read them in the default step. 在两个生成器中,都在configuring步骤中设置configuring ,但在default步骤中读取它们。
  2. After reading the configs with this.config.get('key') or this.config.getAll() , save them as a property of the generator you want to use them in: this.imports = this.config.get('option-name') . 使用this.config.get('key')this.config.getAll()阅读配置后,将它们另存为要在其中使用它们的生成器的属性: this.imports = this.config.get('option-name')

Example: 例:

Base Generator 基础发电机

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

    this.option('option1');
  },

  initializing: function() {
    this.composeWith('base:sub-gen', this.options, {
      local: require.resolve('../sub-gen'),
      link: 'strong'
      });
  },

  configuring: function () {
    let configs = {
      option1: option1
    };

    this.config.set(configs);
  },

    default: function() {
        let config = this.config.getAll()
        this.imports = this.config.get('subGenOptions');
    },
});

Subgenerator 子发电机

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

    this.option('option2');
  },
  configuring: function() {
    let configs = {
        subGenOptions: {
            option2: this.options.option2
        }
    }

    this.config.set(configs)
  },
});

Template (in base generator) 模板(在基本生成器中)

Base generator option: <%=options.option1 %>
Sub-generator option: <%=imports.option2 %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM