UNPKG

3.32 kBPlain TextView Raw
1/**
2 * Created by frank.zickert on 05.04.19.
3 */
4
5import { getChildrenArray } from './index';
6import Types from '../types';
7
8
9/**
10 * loads the specified configuration into a Javascript object
11 *
12 * @param configFilePath
13 */
14export function loadConfiguration(configFilePath: string, infrastructureMode: string | undefined) {
15
16 //console.log("loadConfiguration");
17 return loadConfigurationFromModule(require(configFilePath), infrastructureMode);
18}
19
20export function loadConfigurationFromModule(configModule: any, infrastructureMode: string | undefined) {
21 //console.log("loadConfigurationFromModule");
22 return loadInfrastructureComponent(configModule.default,infrastructureMode);
23}
24
25
26/**
27 * Modes of loading, make sure that during RUNTIME mode, only things are used that exist in webpack- web-mode!
28 *
29 * @type {{COMPILATION: string, RUNTIME: string}}
30 */
31export const INFRASTRUCTURE_MODES = {
32 /**
33 * When packing the application
34 */
35 COMPILATION : "COMPILATION",
36
37 /**
38 * when the app actually runs
39 */
40 RUNTIME: "RUNTIME"
41}
42
43/**
44 * Loads an InfrastructureComponent (of any type)
45 */
46export const loadInfrastructureComponent = (component, infrastructureMode: string | undefined) => {
47
48 //console.log("loadInfrastructureComponent");
49
50 // when the component is an array, return it
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 // first load the children!
59 const children = getChildrenArray(component.props).reduce((result, child) => {
60 return result.concat(loadInfrastructureComponent(child, infrastructureMode))
61 }, []);
62
63 //console.log("parseInfrastructureComponent: ", component, children);
64
65
66 // overwrite the children in the props
67 const props = Object.assign({}, component.props, {
68 children: children
69 });
70
71 // now load the component at hand
72 const params = Object.assign({
73 infrastructureMode: infrastructureMode,
74 }, props);
75
76 const result = component.type(params);
77
78 //console.log("parsed InfrastructureComponent: ", result);
79
80 if (result.infrastructureType == undefined) {
81 //console.log("not an infrastructure-component: ", result);
82
83 return loadInfrastructureComponent(result,infrastructureMode);
84 }
85
86 //console.log("result: ", result);
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 * Get the searched object from the configuration, if it exists
100 *
101 * This function runs on the compiled/webpacked bundle (Plugins removed!!)
102 *
103 * @param component
104 */
105export 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}