1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
10 | exports.source = source;
|
11 | exports.empty = empty;
|
12 | exports.chain = chain;
|
13 | exports.apply = apply;
|
14 | exports.mergeWith = mergeWith;
|
15 | exports.noop = noop;
|
16 | exports.filter = filter;
|
17 | exports.asSource = asSource;
|
18 | exports.branchAndMerge = branchAndMerge;
|
19 | exports.when = when;
|
20 | exports.partitionApplyMerge = partitionApplyMerge;
|
21 | exports.forEach = forEach;
|
22 | exports.composeFileOperators = composeFileOperators;
|
23 | exports.applyToSubtree = applyToSubtree;
|
24 | const rxjs_1 = require("rxjs");
|
25 | const exception_1 = require("../exception/exception");
|
26 | const host_tree_1 = require("../tree/host-tree");
|
27 | const interface_1 = require("../tree/interface");
|
28 | const scoped_1 = require("../tree/scoped");
|
29 | const static_1 = require("../tree/static");
|
30 | const call_1 = require("./call");
|
31 |
|
32 |
|
33 |
|
34 | function source(tree) {
|
35 | return () => tree;
|
36 | }
|
37 |
|
38 |
|
39 |
|
40 | function empty() {
|
41 | return () => (0, static_1.empty)();
|
42 | }
|
43 |
|
44 |
|
45 |
|
46 | function 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 |
|
57 |
|
58 | function apply(source, rules) {
|
59 | return (context) => (0, call_1.callRule)(chain(rules), (0, call_1.callSource)(source, context), context);
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 | function 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 | }
|
69 | function noop() {
|
70 | return () => { };
|
71 | }
|
72 | function 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 | }
|
82 | function asSource(rule) {
|
83 | return (context) => (0, call_1.callRule)(rule, (0, static_1.empty)(), context);
|
84 | }
|
85 | function 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 | }
|
90 | function 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 | }
|
100 | function 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 | }
|
109 | function 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 | }
|
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 | function 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 | }
|