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