[英]How to write files into the app tree in ember-cli preprocessTree hook
我有一个西兰花插件,它执行以下操作:
这非常有效,也可以通过触发西兰花构建进行验收测试。
西兰花构建的示例结果:
templates/generated-route.hbs
templates/my-sub/generated.hbs
下一阶段是将它集成到一个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;
}
};
上述解决方案的问题是,如果没有传递destDir
则无法合并树。 ember-app树以应用程序名称为前缀,例如:
my-ember-app-name/templates/application.hbs
my-ember-app-name/router.js
...
当从ember-app立即调用这个插件时, app.options.trees.app
会起作用,但是当这个插件从另一个插件调用为依赖时, included
挂钩中的参数会获得一个没有app的插件对象树名。
所以问题是,如何以正确的方式将文件写入应用程序树?
在大多数需要这种情况的插件中,有一段代码就像
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;
},
来源: https : //github.com/FortAwesome/ember-fontawesome/blob/master/index.js#L158-L163
还有一个私有方法this._findHost()
可以执行此操作,但它并不适用于所有版本的ember-cli(并且是私有的)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.