1 | import { cloneDeep, concat, find } from 'lodash';
|
2 |
|
3 | import { cleanLifecycleHooksFromMethods } from '.';
|
4 | import Configuration from '../app/configuration';
|
5 |
|
6 | export class ExtendsMerger {
|
7 | private components;
|
8 | private classes;
|
9 | private injectables;
|
10 |
|
11 | private static instance: ExtendsMerger;
|
12 | private constructor() {}
|
13 | public static getInstance() {
|
14 | if (!ExtendsMerger.instance) {
|
15 | ExtendsMerger.instance = new ExtendsMerger();
|
16 | }
|
17 | return ExtendsMerger.instance;
|
18 | }
|
19 |
|
20 | public merge(deps) {
|
21 | this.components = deps.components;
|
22 | this.classes = deps.classes;
|
23 | this.injectables = deps.injectables;
|
24 |
|
25 | this.components.forEach(component => {
|
26 | let ext;
|
27 | if (typeof component.extends !== 'undefined') {
|
28 | ext = this.findInDependencies(component.extends);
|
29 | if (ext) {
|
30 | let recursiveScanWithInheritance = cls => {
|
31 |
|
32 | if (typeof cls.methods !== 'undefined' && cls.methods.length > 0) {
|
33 | let newMethods = cloneDeep(cls.methods);
|
34 | newMethods = this.markInheritance(newMethods, cls);
|
35 | if (typeof component.methodsClass !== 'undefined') {
|
36 | component.methodsClass = [...component.methodsClass, ...newMethods];
|
37 | }
|
38 | }
|
39 | if (typeof cls.properties !== 'undefined' && cls.properties.length > 0) {
|
40 | let newProperties = cloneDeep(cls.properties);
|
41 | newProperties = this.markInheritance(newProperties, cls);
|
42 | if (typeof component.propertiesClass !== 'undefined') {
|
43 | component.propertiesClass = [
|
44 | ...component.propertiesClass,
|
45 | ...newProperties
|
46 | ];
|
47 | }
|
48 | }
|
49 |
|
50 | if (typeof cls.inputsClass !== 'undefined' && cls.inputsClass.length > 0) {
|
51 | let newInputs = cloneDeep(cls.inputsClass);
|
52 | newInputs = this.markInheritance(newInputs, cls);
|
53 | if (typeof component.inputsClass !== 'undefined') {
|
54 | component.inputsClass = [...component.inputsClass, ...newInputs];
|
55 | }
|
56 | }
|
57 | if (
|
58 | typeof cls.outputsClass !== 'undefined' &&
|
59 | cls.outputsClass.length > 0
|
60 | ) {
|
61 | let newOutputs = cloneDeep(cls.outputsClass);
|
62 | newOutputs = this.markInheritance(newOutputs, cls);
|
63 | if (typeof component.outputsClass !== 'undefined') {
|
64 | component.outputsClass = [...component.outputsClass, ...newOutputs];
|
65 | }
|
66 | }
|
67 | if (
|
68 | typeof cls.methodsClass !== 'undefined' &&
|
69 | cls.methodsClass.length > 0
|
70 | ) {
|
71 | let newMethods = cloneDeep(cls.methodsClass);
|
72 | newMethods = this.markInheritance(newMethods, cls);
|
73 | if (typeof component.methodsClass !== 'undefined') {
|
74 | component.methodsClass = [...component.methodsClass, ...newMethods];
|
75 | }
|
76 | }
|
77 | if (
|
78 | typeof cls.propertiesClass !== 'undefined' &&
|
79 | cls.propertiesClass.length > 0
|
80 | ) {
|
81 | let newProperties = cloneDeep(cls.propertiesClass);
|
82 | newProperties = this.markInheritance(newProperties, cls);
|
83 | if (typeof component.propertiesClass !== 'undefined') {
|
84 | component.propertiesClass = [
|
85 | ...component.propertiesClass,
|
86 | ...newProperties
|
87 | ];
|
88 | }
|
89 | }
|
90 | if (
|
91 | typeof cls.hostBindings !== 'undefined' &&
|
92 | cls.hostBindings.length > 0
|
93 | ) {
|
94 | let newHostBindings = cloneDeep(cls.hostBindings);
|
95 | newHostBindings = this.markInheritance(newHostBindings, cls);
|
96 | if (typeof component.hostBindings !== 'undefined') {
|
97 | component.hostBindings = [
|
98 | ...component.hostBindings,
|
99 | ...newHostBindings
|
100 | ];
|
101 | }
|
102 | }
|
103 | if (
|
104 | typeof cls.hostListeners !== 'undefined' &&
|
105 | cls.hostListeners.length > 0
|
106 | ) {
|
107 | let newHostListeners = cloneDeep(cls.hostListeners);
|
108 | newHostListeners = this.markInheritance(newHostListeners, cls);
|
109 | if (typeof component.hostListeners !== 'undefined') {
|
110 | component.hostListeners = [
|
111 | ...component.hostListeners,
|
112 | ...newHostListeners
|
113 | ];
|
114 | }
|
115 | }
|
116 | if (Configuration.mainData.disableLifeCycleHooks) {
|
117 | component.methodsClass = cleanLifecycleHooksFromMethods(
|
118 | component.methodsClass
|
119 | );
|
120 | }
|
121 | if (cls.extends) {
|
122 | recursiveScanWithInheritance(this.findInDependencies(cls.extends));
|
123 | }
|
124 | };
|
125 |
|
126 | recursiveScanWithInheritance(ext);
|
127 | }
|
128 | }
|
129 | });
|
130 |
|
131 | const mergeExtendedClasses = el => {
|
132 | let ext;
|
133 | if (typeof el.extends !== 'undefined') {
|
134 | ext = this.findInDependencies(el.extends);
|
135 | if (ext) {
|
136 | let recursiveScanWithInheritance = cls => {
|
137 | if (typeof cls.methods !== 'undefined' && cls.methods.length > 0) {
|
138 | let newMethods = cloneDeep(cls.methods);
|
139 | newMethods = this.markInheritance(newMethods, cls);
|
140 | if (typeof el.methods !== 'undefined') {
|
141 | el.methods = [...el.methods, ...newMethods];
|
142 | }
|
143 | }
|
144 | if (typeof cls.properties !== 'undefined' && cls.properties.length > 0) {
|
145 | let newProperties = cloneDeep(cls.properties);
|
146 | newProperties = this.markInheritance(newProperties, cls);
|
147 | if (typeof el.properties !== 'undefined') {
|
148 | el.properties = [...el.properties, ...newProperties];
|
149 | }
|
150 | }
|
151 | if (cls.extends) {
|
152 | recursiveScanWithInheritance(this.findInDependencies(cls.extends));
|
153 | }
|
154 | };
|
155 |
|
156 | recursiveScanWithInheritance(ext);
|
157 | }
|
158 | }
|
159 | };
|
160 |
|
161 | this.classes.forEach(mergeExtendedClasses);
|
162 | this.injectables.forEach(mergeExtendedClasses);
|
163 |
|
164 | return deps;
|
165 | }
|
166 |
|
167 | private markInheritance(data, originalource) {
|
168 | return data.map(el => {
|
169 | let newElement = el;
|
170 | newElement.inheritance = {
|
171 | file: originalource.name
|
172 | };
|
173 | return newElement;
|
174 | });
|
175 | }
|
176 |
|
177 | private findInDependencies(name: string) {
|
178 | let mergedData = concat([], this.components, this.classes, this.injectables);
|
179 | let result = find(mergedData, { name: name } as any);
|
180 | return result || false;
|
181 | }
|
182 | }
|
183 |
|
184 | export default ExtendsMerger.getInstance();
|