angular - 角度示意图:将规则应用于当前树,然后修改树
问题描述
我正在创建一个 Angular 示意图。我想将规则应用于当前树,然后修改树。
如以下代码段所述,我想应用创建的规则,因此tree.exists("./file.txt")
返回true
export default function(options: any): Rule {
return (tree: Tree, context: SchematicContext) => {
let tmpl = apply(url("./files"), [template(options),move(".")]);
let rule = chain([branchAndMerge(chain([mergeWith(tmpl)]))]);
//how to apply this rule to the current tree, so it contains the files from ./files
//assume ./files contain a file named myfile.txt
console.log(tree.exists("./myfile.txt"))
tree.create("hello.txt", "");
return tree;
};
}
笔记
return rule;
创建 /file.txt,但我想通过 tree.create() 函数创建两个文件 file.txt(通过应用规则)和 hello.txt。
在应用规则之前使用 tree.create() 创建两个文件,但tree.exists('./file.txt')
仍然返回 false。
解决方案
编辑
只需调用chain
而不分配变量:
export default function(options: any): Rule {
return (tree: Tree) => {
const templateSource = apply(
url("./files"),
[template(options), move(".")]
);
const rules: Rule[] = [];
rules.push(mergeWith(templateSource, MergeStrategy.Overwrite));
rules.push(createFiles());
return chain(rules);
};
}
function createFiles(): Rule {
return (tree: Tree) => {
// Check if ./files contain a file named myfile.txt
if (tree.exists("./myfile.txt")) {
// Do whatever you want.
}
// You should check if it exists. If it does, use ovewrite instead.
tree.create("hello.txt", "");
return tree;
}
}
并且总是尝试提供一些合并策略。
推荐阅读
- javascript - 触地后跳跃球员
- android - ionic 4 - 有没有办法在 3 个项目之后的离子列表之间实施谷歌广告?
- shell - 如何防止终端损坏?
- angular - else 单元测试中未采用的路径
- java - 当 Fibers 到来时,Servlet 会比响应式 Web 框架更好吗?
- wordpress - 通过他们的 request_status 获取文档列表,例如 Zoho api PHP 中的“已完成”?
- javascript - Eslint 规则“语句之间的填充线”未按预期工作
- c++ - 在其他线程中迭代该 concurrent_vector 时调用 concurrency::concurrent_vector::push_back 是否并发安全?
- c# - FakeItEasy 模拟对象修改方法
- android - 向上滚动按钮 Android