[英]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.