UNPKG

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