Skip to content

Rule

Introduction

A Rule is a function that takes two parameters: a Tree and a SchematicContext. It returns a Tree, an Observable<Tree>, a Rule, a Promise<void | Rule>, or void.

What makes a Rule particularly powerful is that a factory function returns a Rule, enabling the composition of multiple rules. This compositional ability allows you to divide your code into smaller, manageable rules, which are then executed in sequence. This modular approach not only enhances code readability and maintainability but also facilitates complex transformations and operations within your schematics.

However, as you may have observed, a factory function typically returns only one Rule. So, you might wonder, how can we utilize multiple rules in a single operation? Fortunately, Schematics provides several functions designed specifically for this purpose, allowing for the seamless combination and execution of multiple rules. Let’s explore these functions further:

FunctionDescriptionParametersReturn Type
chainChain multiple rules into a single rule.rules: Iterable<Rule> | AsyncIterable<Rule>Rule
mergeWithMerge an input tree with the source passed in.source: Source, strategy?: MergeStrategyRule
branchAndMergeBranches the input tree, applies a rule, and merges the result.rule: Rule, strategy?: MergeStrategyRule
partitionApplyMergePartitions the input tree, applies rules to each partition, then merges.predicate: FilePredicate<boolean>, ruleYes: Rule, ruleNo?: RuleRule
forEachApplies an operation to each file in the input tree.operator: FileOperatorRule
applyToSubtreeApplies rules to a specific subtree.path: string, rules: Rule[]Rule
noopA rule that does nothing.NoneRule
filterFilters files from the input tree based on a predicate.predicate: FilePredicate<boolean>Rule

Don’t worry if you don’t know how to use all of them. I will create tutorial for all cases.