-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathgenerate.ts
More file actions
78 lines (69 loc) · 2.79 KB
/
Copy pathgenerate.ts
File metadata and controls
78 lines (69 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { generateClientTypes } from "./generateClientTypes";
import { generatePreviewTypes } from "./generatePreviewTypes";
import { extractProperties, extractSystemProperties } from "./helpers";
import { WidgetXml } from "./WidgetXml";
const mxExports = [
"ActionValue",
"AssociationMetaData",
"AttributeMetaData",
"DynamicValue",
"EditableValue",
"EditableListValue",
"EditableFileValue",
"EditableImageValue",
"FileValue",
"ListValue",
"NativeIcon",
"NativeImage",
"Option",
"ListActionValue",
"ListAttributeValue",
"ListAttributeListValue",
"ListExpressionValue",
"ListReferenceValue",
"ListReferenceSetValue",
"ListWidgetValue",
"ReferenceValue",
"ReferenceSetValue",
"SelectionSingleValue",
"SelectionMultiValue",
"WebIcon",
"WebImage"
];
export function generateForWidget(widgetXml: WidgetXml, widgetName: string) {
if (!widgetXml?.widget?.properties) {
throw new Error("[XML] XML doesn't contains <properties> element");
}
if (widgetXml.widget.$.pluginWidget !== "true") {
throw new Error("[XML] Attribute pluginWidget=true not found. Please review your XML");
}
const isNative = widgetXml.widget.$.supportedPlatform === "Native";
const propElements = widgetXml.widget.properties[0] ?? [];
const properties = extractProperties(propElements).filter(prop => prop?.$?.key);
const systemProperties = extractSystemProperties(propElements).filter(prop => prop?.$?.key);
const clientTypes = generateClientTypes(widgetName, properties, systemProperties, isNative);
const modelerTypes = generatePreviewTypes(widgetName, properties, systemProperties);
const generatedTypesCode = clientTypes
.slice(0, clientTypes.length - 1) // all client auxiliary types
.concat(modelerTypes.slice(0, modelerTypes.length - 1)) // all preview auxiliary types
.concat([clientTypes[clientTypes.length - 1], modelerTypes[modelerTypes.length - 1]])
.join("\n\n");
const imports = [
generateImport("react", generatedTypesCode, ["ComponentType", "CSSProperties", "ReactNode"]),
generateImport("mendix", generatedTypesCode, mxExports),
generateImport("big.js", generatedTypesCode, ["Big"])
]
.filter(line => line)
.join("\n");
return `/**
* This file was generated from ${widgetName}.xml
* WARNING: All changes made to this file will be overwritten
* @author Mendix Widgets Framework Team
*/
${imports.length ? imports + "\n\n" : ""}${generatedTypesCode}
`;
}
function generateImport(from: string, code: string, availableNames: string[]) {
const usedNames = availableNames.filter(type => new RegExp(`\\W${type}\\W`).test(code));
return usedNames.length ? `import { ${usedNames.join(", ")} } from "${from}";` : "";
}