@contentstack/cli-variants
Version:
Variants plugin
54 lines (53 loc) • 3.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.lookUpAttributes = void 0;
const cli_utilities_1 = require("@contentstack/cli-utilities");
/**
* function to either modify the UID or eliminate it if the attribute is not created in the target project
* @param attributeRules
* @param attributesUid - {attributesUid} attributes mapper data in format {<old-uid>: <new-uid>}
* @returns
*/
const lookUpAttributes = (attributeRules, attributesUid) => {
var _a;
cli_utilities_1.log.debug(`Looking up attributes in ${(attributeRules === null || attributeRules === void 0 ? void 0 : attributeRules.length) || 0} rules`);
cli_utilities_1.log.debug(`Available attribute mappings: ${Object.keys(attributesUid).length}`);
for (let index = 0; index < (attributeRules === null || attributeRules === void 0 ? void 0 : attributeRules.length); index++) {
const rule = attributeRules[index];
cli_utilities_1.log.debug(`Processing rule ${index + 1}/${attributeRules.length} of type: ${rule['__type']}`);
if (rule['__type'] === 'Rule') {
// Check if attribute reference exists in attributesUid
const attributeRef = (_a = rule.attribute) === null || _a === void 0 ? void 0 : _a.ref;
const attributeType = rule.attribute['__type'];
cli_utilities_1.log.debug(`Rule attribute type: ${attributeType}, reference: ${attributeRef}`);
// check if type is CustomAttributeReference
if (attributeType === 'CustomAttributeReference') {
if (attributeRef && attributesUid.hasOwnProperty(attributeRef) && attributesUid[attributeRef]) {
const newAttributeRef = attributesUid[attributeRef];
cli_utilities_1.log.debug(`Mapping attribute reference: ${attributeRef} -> ${newAttributeRef}`);
rule.attribute.ref = newAttributeRef;
}
else {
cli_utilities_1.log.warn(`Attribute reference not found in mapping: ${attributeRef}. Removing rule.`);
// Remove the rule if the attribute reference is not found
attributeRules.splice(index, 1);
--index;
}
}
else {
cli_utilities_1.log.debug(`Skipping non-custom attribute reference: ${attributeType}`);
}
}
else if (rule['__type'] === 'RuleCombination' && Array.isArray(rule.rules)) {
cli_utilities_1.log.debug(`Processing nested rule combination with ${rule.rules.length} sub-rules`);
// Recursively look up attributes in nested rule combinations
(0, exports.lookUpAttributes)(rule.rules, attributesUid);
}
else {
cli_utilities_1.log.debug(`Skipping rule of type: ${rule['__type']}`);
}
}
cli_utilities_1.log.debug(`Attribute lookup completed. Final rule count: ${attributeRules.length}`);
return attributeRules;
};
exports.lookUpAttributes = lookUpAttributes;