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 exception_1 = require("../exception/exception");
|
13 | const host_tree_1 = require("../tree/host-tree");
|
14 | const interface_1 = require("../tree/interface");
|
15 | const scoped_1 = require("../tree/scoped");
|
16 | const static_1 = require("../tree/static");
|
17 | const call_1 = require("./call");
|
18 |
|
19 |
|
20 |
|
21 | function source(tree) {
|
22 | return () => tree;
|
23 | }
|
24 | exports.source = source;
|
25 |
|
26 |
|
27 |
|
28 | function empty() {
|
29 | return () => (0, static_1.empty)();
|
30 | }
|
31 | exports.empty = empty;
|
32 |
|
33 |
|
34 |
|
35 | function 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 | }
|
44 | exports.chain = chain;
|
45 |
|
46 |
|
47 |
|
48 | function apply(source, rules) {
|
49 | return (context) => (0, call_1.callRule)(chain(rules), (0, call_1.callSource)(source, context), context);
|
50 | }
|
51 | exports.apply = apply;
|
52 |
|
53 |
|
54 |
|
55 | function 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 | }
|
60 | exports.mergeWith = mergeWith;
|
61 | function noop() {
|
62 | return () => { };
|
63 | }
|
64 | exports.noop = noop;
|
65 | function 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 | }
|
75 | exports.filter = filter;
|
76 | function asSource(rule) {
|
77 | return (context) => (0, call_1.callRule)(rule, (0, static_1.empty)(), context);
|
78 | }
|
79 | exports.asSource = asSource;
|
80 | function 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 | }
|
85 | exports.branchAndMerge = branchAndMerge;
|
86 | function 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 | }
|
96 | exports.when = when;
|
97 | function 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 | }
|
106 | exports.partitionApplyMerge = partitionApplyMerge;
|
107 | function 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 | }
|
130 | exports.forEach = forEach;
|
131 | function composeFileOperators(operators) {
|
132 | return (entry) => {
|
133 | let current = entry;
|
134 | for (const op of operators) {
|
135 | current = op(current);
|
136 | if (current === null) {
|
137 |
|
138 | return null;
|
139 | }
|
140 | }
|
141 | return current;
|
142 | };
|
143 | }
|
144 | exports.composeFileOperators = composeFileOperators;
|
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 | }
|
158 | exports.applyToSubtree = applyToSubtree;
|