1 |
|
2 |
|
3 |
|
4 |
|
5 | import { getChildrenArray } from './index';
|
6 | import Types from '../types';
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | export function loadConfiguration(configFilePath: string, infrastructureMode: string | undefined) {
|
15 |
|
16 |
|
17 | return loadConfigurationFromModule(require(configFilePath), infrastructureMode);
|
18 | }
|
19 |
|
20 | export function loadConfigurationFromModule(configModule: any, infrastructureMode: string | undefined) {
|
21 |
|
22 | return loadInfrastructureComponent(configModule.default,infrastructureMode);
|
23 | }
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | export const INFRASTRUCTURE_MODES = {
|
32 | |
33 |
|
34 |
|
35 | COMPILATION : "COMPILATION",
|
36 |
|
37 | |
38 |
|
39 |
|
40 | RUNTIME: "RUNTIME"
|
41 | }
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | export const loadInfrastructureComponent = (component, infrastructureMode: string | undefined) => {
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | if (component !== undefined && Array.isArray(component) && component.length > 0) {
|
52 | return component.map(c => loadInfrastructureComponent(c, infrastructureMode))
|
53 | }
|
54 |
|
55 | try {
|
56 |
|
57 |
|
58 |
|
59 | const children = getChildrenArray(component.props).reduce((result, child) => {
|
60 | return result.concat(loadInfrastructureComponent(child, infrastructureMode))
|
61 | }, []);
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 | const props = Object.assign({}, component.props, {
|
68 | children: children
|
69 | });
|
70 |
|
71 |
|
72 | const params = Object.assign({
|
73 | infrastructureMode: infrastructureMode,
|
74 | }, props);
|
75 |
|
76 | const result = component.type(params);
|
77 |
|
78 |
|
79 |
|
80 | if (result.infrastructureType == undefined) {
|
81 |
|
82 |
|
83 | return loadInfrastructureComponent(result,infrastructureMode);
|
84 | }
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | return result;
|
90 |
|
91 | } catch (error) {
|
92 | console.error("NOT an infrastructure component --> ", error);
|
93 | return undefined;
|
94 | };
|
95 | };
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 | export function extractObject(component: any, infrastructureType: string, instanceId: string) {
|
106 | if (component !== undefined &&
|
107 | component.infrastructureType === infrastructureType &&
|
108 | component.instanceId === instanceId
|
109 | ) {
|
110 | return component;
|
111 | } else {
|
112 | return getChildrenArray(component).reduce(
|
113 | (found, child) => found !== undefined ? found : extractObject(child, infrastructureType, instanceId),
|
114 | undefined
|
115 | )
|
116 | }
|
117 | }
|