UNPKG

albxrmtypesgen

Version:

A TypeScript Declaration Generator for Dynamics 365 Forms

109 lines 5.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.render = void 0; const prettier_1 = require("prettier"); const fs_1 = require("fs"); const handlebars_1 = require("handlebars"); const fieldtypes_1 = require("./fieldtypes"); const render = (data, meta, templateName, localOptionSet) => { const templateBuffer = fs_1.readFileSync(`${__dirname}/${templateName}.hbs`); const template = handlebars_1.compile(templateBuffer.toString()); const getFieldName = (value) => (value.DataFieldName ? value.DataFieldName : value.Id); handlebars_1.registerHelper('formtype', (value) => (value === 2 ? 'main' : 'quickcreate')); // eslint-disable-next-line no-confusing-arrow handlebars_1.registerHelper('toTypeName', // eslint-disable-next-line no-confusing-arrow (value) => { const typeName = value ? value.replace(/[^a-z^A-Z^0-9^_]+/g, '') : null; if (/^\d/.test(typeName) || typeName === 'import') { return `_${typeName}`; } return typeName; }); // eslint-disable-next-line no-nested-ternary handlebars_1.registerHelper('getFieldName', getFieldName); handlebars_1.registerHelper('jsonStringify', (value) => JSON.stringify(value)); handlebars_1.registerHelper('getAttributeType', (metadata, name, classid, fieldtype) => { const attributetype = fieldtypes_1.getAttributeType(metadata, name, classid, fieldtype); return attributetype; }); handlebars_1.registerHelper('hasControls', (section, metadata) => { const Rows = section.Rows.$values; return Rows.some((row) => { const Cells = row.Cells.$values; return Cells.some((cell) => cell.Control !== null && fieldtypes_1.getAttributeType(metadata, cell.Control.DataFieldName ? cell.Control.DataFieldName : cell.Control.Id, cell.Control.classid, 'formControl') !== undefined && getFieldName(cell.Control)); }); }); console.log(meta); handlebars_1.registerHelper('getFormControl', (formdata, formmeta) => { const controls = []; formdata.Tabs.$values.forEach((tab) => { tab.Columns.$values.forEach((col) => { col.Sections.$values.forEach((sec) => { sec.Rows.$values.forEach((row) => { row.Cells.$values.forEach((cell) => { if (cell.Control !== null && fieldtypes_1.getAttributeType(formmeta, cell.Control.DataFieldName ? cell.Control.DataFieldName : cell.Control.Id, cell.Control.ClassId, 'formControl') !== undefined && getFieldName(cell.Control) && !controls.some((control) => getFieldName(control) === getFieldName(cell.Control))) { controls.push(cell.Control); } }); }); }); }); }); controls.sort((a, b) => getFieldName(a).localeCompare(getFieldName(b))); return controls; }); handlebars_1.registerHelper('sectionCollector', (formdata) => { const sections = []; formdata.Columns.$values.forEach((col) => { col.Sections.$values.forEach((sec) => { sections.push(sec); }); }); sections.sort((a, b) => { var _a; return (_a = a.Name) === null || _a === void 0 ? void 0 : _a.localeCompare(b.Name); }); console.log(formdata.Name); console.log(formdata.Columns); return sections; }); handlebars_1.registerHelper('cellsCollector', (formdata) => { const cells = []; formdata.Rows.$values.forEach((row) => { row.Cells.$values.forEach((cell) => { if (getFieldName(cell.Control)) { cells.push(cell); } }); }); cells.sort((a, b) => getFieldName(a.Control).localeCompare(getFieldName(b.Control))); return cells; }); const formObj = data.formjson ? JSON.parse(data.formjson) : null; if (formObj !== null && formObj.Tabs.$values[0].Columns.$values[0].Sections.$values[0].Name !== null) { formObj.Tabs.$values.sort((a, b) => a.Name.localeCompare(b.Name)); formObj.Tabs.$values[0].Columns.$values[0].Sections.$values .sort((a, b) => a.Name.localeCompare(b.Name)); formObj.Tabs.$values[0].Columns.$values[0].Sections.$values[0] .Rows.$values[0].Cells.$values.sort((a, b) => { var _a; return (_a = a.Control.DataFieldName) === null || _a === void 0 ? void 0 : _a.localeCompare(b.Control.DataFieldName); }); } // eslint-disable-next-line no-param-reassign meta.Attributes = meta.Attributes.sort((a, b) => a.SchemaName.localeCompare(b.SchemaName)); // eslint-disable-next-line no-param-reassign localOptionSet = localOptionSet === null || localOptionSet === void 0 ? void 0 : localOptionSet.sort((a, b) => a.LogicalName.localeCompare(b.LogicalName)); const dts = template({ data, formObj, meta, localOptionSet, }); const formatted = prettier_1.format(dts, { parser: 'typescript' }); return formatted; }; exports.render = render; exports.default = exports.render; //# sourceMappingURL=renderer.js.map