简体   繁体   English

如何在ember-cli preprocessTree钩子中将文件写入应用程序树

[英]How to write files into the app tree in ember-cli preprocessTree hook

I have a broccoli plugin which does the following: 我有一个西兰花插件,它执行以下操作:

  • Read json file from root folder 从根文件夹中读取json文件
  • based on the JSON generate some route template HBS files 基于JSON生成一些路由模板HBS文件

This works perfectly and can also be acceptance tested by triggering a broccoli build. 这非常有效,也可以通过触发西兰花构建进行验收测试。

Example result of the broccoli build: 西兰花构建的示例结果:

templates/generated-route.hbs
templates/my-sub/generated.hbs

The next phase is integrating it into an ember-addon, and I created one with a preprocessTree hook. 下一阶段是将它集成到一个ember-addon中,我用preprocessTree钩子创建了一个。

module.exports = {
  name: 'route-template-generator',

  included(app) {
    this._super.included.apply(this, arguments);

    this.options = Object.assign({}, app.options);
    this.appName = app.name;

    if (!this.options.trees) {
        throw new Error('No trees found in app to add the new files to');
    }
    if (!this.appName) {
        throw new Error('no valid application name, unable to add files to the tree');
    }
  },

  preprocessTree(type, tree) {
    if (type === 'template') {
      const extraTemplates = new RouteTemplateGenerator(
        [new Funnel(this.options.trees.app, { include: ['router.json'] })], 
        { destDir: this.appName }
      );

      return mergeTrees([tree, extraTemplates], { overwrite: true });
    }
    return tree;
  }
};

The problem with above solution is that the trees cannot be merged if no destDir is passed. 上述解决方案的问题是,如果没有传递destDir则无法合并树。 The ember-app tree is prefixed with the application name, eg: ember-app树以应用程序名称为前缀,例如:

my-ember-app-name/templates/application.hbs
my-ember-app-name/router.js
...

When calling this addon immediately from a ember-app the app.options.trees.app will work, however when this addon is called as dependency from another addon, the parameter in the included hook gets an addon object which doesn't have the app tree name. 当从ember-app立即调用这个插件时, app.options.trees.app会起作用,但是当这个插件从另一个插件调用为依赖时, included挂钩中的参数会获得一个没有app的插件对象树名。

So the question is, how do I write files to an app tree the right way? 所以问题是,如何以正确的方式将文件写入应用程序树?

In most addons where this is needed there is a block of code like 在大多数需要这种情况的插件中,有一段代码就像

included(app) {
  this._super.included.apply(this, arguments)
  let current = this;
  // Keep iterating upward until we don't have a grandparent.
  // Has to do this grandparent check because at some point we hit the project.
  do {
    app = current.app || app;
  } while (current.parent.parent && (current = current.parent));

  this.app = app;
},

source: https://github.com/FortAwesome/ember-fontawesome/blob/master/index.js#L158-L163 来源: https//github.com/FortAwesome/ember-fontawesome/blob/master/index.js#L158-L163

There is also a private method this._findHost() that does this, but it isn't available in all versions of ember-cli (and is private). 还有一个私有方法this._findHost()可以执行此操作,但它并不适用于所有版本的ember-cli(并且是私有的)。

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

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