UNPKG

@gmetrixr/rjson

Version:
59 lines (58 loc) 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const r_1 = require("../../../r"); const VariableTypes_1 = require("../../../r/definitions/variables/VariableTypes"); const ProjectFactory_1 = require("../../../r/recordFactories/ProjectFactory"); const elements_1 = require("../../../r/definitions/elements"); class Migration { execute(projectJson) { var _a; const pJson = projectJson; const projectF = r_1.r.record(pJson); const variables = projectF.getRecords(r_1.RT.variable); for (const v of variables) { // * If a variable is a predefined var, set it's var_category = predefined if (Number(v["id"]) in VariableTypes_1.predefinedVariableIdToName) { v.props.var_category = VariableTypes_1.VarCategory.predefined; // * update few names of the variables so that their templating can be done correctly only when this is a predefined var if (v["name"] === "platform_var") { v["name"] = "device_var"; } } } // * also fix any templating issues with platform_var -> device_var name change const allTemplatableRecords = ProjectFactory_1.ProjectUtils.getAllTemplatedRecords(pJson); const regex = /{{platform_var}}/g; // /g to match all instances for (const record of allTemplatableRecords) { if (record.type === r_1.RT.element) { switch (record.props.element_type) { case elements_1.ElementType.text: { const recordF = r_1.r.element(record); const value = recordF.getValueOrDefault(r_1.rtp.element.text); recordF.set(r_1.rtp.element.text, value === null || value === void 0 ? void 0 : value.replace(regex, "{{device_var}}")); break; } case elements_1.ElementType.embed_html: { const recordF = r_1.r.element(record); const value = recordF.getValueOrDefault(r_1.rtp.element.embed_string); recordF.set(r_1.rtp.element.embed_string, value === null || value === void 0 ? void 0 : value.replace(regex, "{{device_var}}")); break; } } } else if (record.type === r_1.RT.then_action) { const recordF = r_1.r.record(record); const properties = recordF.getValueOrDefault(r_1.rtp.then_action.properties); if (properties.length > 0) { if (properties[0]) { properties[0] = (_a = properties[0]) === null || _a === void 0 ? void 0 : _a.replace(regex, "{{device_var}}"); } recordF.set(r_1.rtp.then_action.properties, properties); } } } projectF.set(r_1.rtp.project.version, 121); } } const migration = new Migration(); exports.default = migration;