@gmetrixr/rjson
Version:
(R)ecursive Json
56 lines (55 loc) • 2.85 kB
JavaScript
;
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) {
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.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.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) {
properties[0] = properties[0].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;