UNPKG

5.08 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google LLC All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.dev/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.source = source;
11exports.empty = empty;
12exports.chain = chain;
13exports.apply = apply;
14exports.mergeWith = mergeWith;
15exports.noop = noop;
16exports.filter = filter;
17exports.asSource = asSource;
18exports.branchAndMerge = branchAndMerge;
19exports.when = when;
20exports.partitionApplyMerge = partitionApplyMerge;
21exports.forEach = forEach;
22exports.composeFileOperators = composeFileOperators;
23exports.applyToSubtree = applyToSubtree;
24const rxjs_1 = require("rxjs");
25const exception_1 = require("../exception/exception");
26const host_tree_1 = require("../tree/host-tree");
27const interface_1 = require("../tree/interface");
28const scoped_1 = require("../tree/scoped");
29const static_1 = require("../tree/static");
30const call_1 = require("./call");
31/**
32 * A Source that returns an tree as its single value.
33 */
34function source(tree) {
35 return () => tree;
36}
37/**
38 * A source that returns an empty tree.
39 */
40function empty() {
41 return () => (0, static_1.empty)();
42}
43/**
44 * Chain multiple rules into a single rule.
45 */
46function chain(rules) {
47 return async (initialTree, context) => {
48 let intermediateTree;
49 for await (const rule of rules) {
50 intermediateTree = (0, call_1.callRule)(rule, intermediateTree ?? initialTree, context);
51 }
52 return () => intermediateTree;
53 };
54}
55/**
56 * Apply multiple rules to a source, and returns the source transformed.
57 */
58function apply(source, rules) {
59 return (context) => (0, call_1.callRule)(chain(rules), (0, call_1.callSource)(source, context), context);
60}
61/**
62 * Merge an input tree with the source passed in.
63 */
64function mergeWith(source, strategy = interface_1.MergeStrategy.Default) {
65 return (tree, context) => {
66 return (0, call_1.callSource)(source, context).pipe((0, rxjs_1.map)((sourceTree) => tree.merge(sourceTree, strategy || context.strategy)), (0, rxjs_1.mapTo)(tree));
67 };
68}
69function noop() {
70 return () => { };
71}
72function filter(predicate) {
73 return (tree) => {
74 if (host_tree_1.HostTree.isHostTree(tree)) {
75 return new host_tree_1.FilterHostTree(tree, predicate);
76 }
77 else {
78 throw new exception_1.SchematicsException('Tree type is not supported.');
79 }
80 };
81}
82function asSource(rule) {
83 return (context) => (0, call_1.callRule)(rule, (0, static_1.empty)(), context);
84}
85function branchAndMerge(rule, strategy = interface_1.MergeStrategy.Default) {
86 return (tree, context) => {
87 return (0, call_1.callRule)(rule, tree.branch(), context).pipe((0, rxjs_1.map)((branch) => tree.merge(branch, strategy || context.strategy)), (0, rxjs_1.mapTo)(tree));
88 };
89}
90function when(predicate, operator) {
91 return (entry) => {
92 if (predicate(entry.path, entry)) {
93 return operator(entry);
94 }
95 else {
96 return entry;
97 }
98 };
99}
100function partitionApplyMerge(predicate, ruleYes, ruleNo) {
101 return (tree, context) => {
102 const [yes, no] = (0, static_1.partition)(tree, predicate);
103 return (0, rxjs_1.concat)((0, call_1.callRule)(ruleYes, yes, context), (0, call_1.callRule)(ruleNo || noop(), no, context)).pipe((0, rxjs_1.toArray)(), (0, rxjs_1.map)(([yesTree, noTree]) => {
104 yesTree.merge(noTree, context.strategy);
105 return yesTree;
106 }));
107 };
108}
109function forEach(operator) {
110 return (tree) => {
111 tree.visit((path, entry) => {
112 if (!entry) {
113 return;
114 }
115 const newEntry = operator(entry);
116 if (newEntry === entry) {
117 return;
118 }
119 if (newEntry === null) {
120 tree.delete(path);
121 return;
122 }
123 if (newEntry.path != path) {
124 tree.rename(path, newEntry.path);
125 }
126 if (!newEntry.content.equals(entry.content)) {
127 tree.overwrite(newEntry.path, newEntry.content);
128 }
129 });
130 };
131}
132function composeFileOperators(operators) {
133 return (entry) => {
134 let current = entry;
135 for (const op of operators) {
136 current = op(current);
137 if (current === null) {
138 // Deleted, just return.
139 return null;
140 }
141 }
142 return current;
143 };
144}
145function applyToSubtree(path, rules) {
146 return (tree, context) => {
147 const scoped = new scoped_1.ScopedTree(tree, path);
148 return (0, call_1.callRule)(chain(rules), scoped, context).pipe((0, rxjs_1.map)((result) => {
149 if (result === scoped) {
150 return tree;
151 }
152 else {
153 throw new exception_1.SchematicsException('Original tree must be returned from all rules when using "applyToSubtree".');
154 }
155 }));
156 };
157}