UNPKG

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