1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.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;
|
11 | const rxjs_1 = require("rxjs");
|
12 | const operators_1 = require("rxjs/operators");
|
13 | const exception_1 = require("../exception/exception");
|
14 | const host_tree_1 = require("../tree/host-tree");
|
15 | const interface_1 = require("../tree/interface");
|
16 | const scoped_1 = require("../tree/scoped");
|
17 | const static_1 = require("../tree/static");
|
18 | const call_1 = require("./call");
|
19 |
|
20 |
|
21 |
|
22 | function source(tree) {
|
23 | return () => tree;
|
24 | }
|
25 | exports.source = source;
|
26 |
|
27 |
|
28 |
|
29 | function empty() {
|
30 | return () => (0, static_1.empty)();
|
31 | }
|
32 | exports.empty = empty;
|
33 |
|
34 |
|
35 |
|
36 | function 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 | }
|
45 | exports.chain = chain;
|
46 |
|
47 |
|
48 |
|
49 | function apply(source, rules) {
|
50 | return (context) => (0, call_1.callRule)(chain(rules), (0, call_1.callSource)(source, context), context);
|
51 | }
|
52 | exports.apply = apply;
|
53 |
|
54 |
|
55 |
|
56 | function 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 | }
|
61 | exports.mergeWith = mergeWith;
|
62 | function noop() {
|
63 | return () => { };
|
64 | }
|
65 | exports.noop = noop;
|
66 | function 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 | }
|
76 | exports.filter = filter;
|
77 | function asSource(rule) {
|
78 | return (context) => (0, call_1.callRule)(rule, (0, static_1.empty)(), context);
|
79 | }
|
80 | exports.asSource = asSource;
|
81 | function 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 | }
|
86 | exports.branchAndMerge = branchAndMerge;
|
87 | function 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 | }
|
97 | exports.when = when;
|
98 | function 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 | }
|
107 | exports.partitionApplyMerge = partitionApplyMerge;
|
108 | function 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 | }
|
131 | exports.forEach = forEach;
|
132 | function composeFileOperators(operators) {
|
133 | return (entry) => {
|
134 | let current = entry;
|
135 | for (const op of operators) {
|
136 | current = op(current);
|
137 | if (current === null) {
|
138 |
|
139 | return null;
|
140 | }
|
141 | }
|
142 | return current;
|
143 | };
|
144 | }
|
145 | exports.composeFileOperators = composeFileOperators;
|
146 | function 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 | }
|
159 | exports.applyToSubtree = applyToSubtree;
|