@mendix/pluggable-widgets-tools
Version:
Mendix Pluggable Widgets Tools
71 lines (70 loc) • 3.26 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateForWidget = generateForWidget;
const generateClientTypes_1 = require("./generateClientTypes");
const generatePreviewTypes_1 = require("./generatePreviewTypes");
const helpers_1 = require("./helpers");
const mxExports = [
"ActionValue",
"AssociationMetaData",
"AttributeMetaData",
"DynamicValue",
"EditableValue",
"EditableListValue",
"FileValue",
"ListValue",
"NativeIcon",
"NativeImage",
"Option",
"ListActionValue",
"ListAttributeValue",
"ListAttributeListValue",
"ListExpressionValue",
"ListReferenceValue",
"ListReferenceSetValue",
"ListWidgetValue",
"ReferenceValue",
"ReferenceSetValue",
"SelectionSingleValue",
"SelectionMultiValue",
"WebIcon",
"WebImage"
];
function generateForWidget(widgetXml, widgetName) {
var _a, _b;
if (!((_a = widgetXml === null || widgetXml === void 0 ? void 0 : widgetXml.widget) === null || _a === void 0 ? void 0 : _a.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 = (_b = widgetXml.widget.properties[0]) !== null && _b !== void 0 ? _b : [];
const properties = (0, helpers_1.extractProperties)(propElements).filter(prop => { var _a; return (_a = prop === null || prop === void 0 ? void 0 : prop.$) === null || _a === void 0 ? void 0 : _a.key; });
const systemProperties = (0, helpers_1.extractSystemProperties)(propElements).filter(prop => { var _a; return (_a = prop === null || prop === void 0 ? void 0 : prop.$) === null || _a === void 0 ? void 0 : _a.key; });
const clientTypes = (0, generateClientTypes_1.generateClientTypes)(widgetName, properties, systemProperties, isNative);
const modelerTypes = (0, generatePreviewTypes_1.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, code, availableNames) {
const usedNames = availableNames.filter(type => new RegExp(`\\W${type}\\W`).test(code));
return usedNames.length ? `import { ${usedNames.join(", ")} } from "${from}";` : "";
}
;