@contentstack/cli-variants
Version:
Variants plugin
84 lines (83 loc) • 5.58 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = require("path");
const fs_1 = require("fs");
const cli_utilities_1 = require("@contentstack/cli-utilities");
const utils_1 = require("../utils");
class Attribute extends utils_1.PersonalizationAdapter {
constructor(config) {
const conf = {
config,
baseURL: config.modules.personalize.baseURL[config.region.name],
headers: { 'X-Project-Uid': config.modules.personalize.project_id },
};
super(Object.assign(config, conf));
this.config = config;
this.personalizeConfig = this.config.modules.personalize;
this.attributeConfig = this.personalizeConfig.attributes;
this.mapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.backupDir), 'mapper', (0, cli_utilities_1.sanitizePath)(this.personalizeConfig.dirName));
this.attrMapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.attributeConfig.dirName));
this.attributesUidMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.attrMapperDirPath), 'uid-mapping.json');
this.attributesUidMapper = {};
this.config.context.module = 'attributes';
}
/**
* The function asynchronously imports attributes from a JSON file and creates them in the system.
*/
import() {
return __awaiter(this, void 0, void 0, function* () {
var _a;
yield this.init();
yield utils_1.fsUtil.makeDirectory(this.attrMapperDirPath);
cli_utilities_1.log.debug(`Created mapper directory: ${this.attrMapperDirPath}`, this.config.context);
const { dirName, fileName } = this.attributeConfig;
const attributesPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.data), (0, cli_utilities_1.sanitizePath)(this.personalizeConfig.dirName), (0, cli_utilities_1.sanitizePath)(dirName), (0, cli_utilities_1.sanitizePath)(fileName));
cli_utilities_1.log.debug(`Checking for attributes file: ${attributesPath}`, this.config.context);
if ((0, fs_1.existsSync)(attributesPath)) {
try {
const attributes = utils_1.fsUtil.readFile(attributesPath, true);
cli_utilities_1.log.info(`Found ${attributes.length} attributes to import`, this.config.context);
for (const attribute of attributes) {
const { key, name, description, uid } = attribute;
cli_utilities_1.log.debug(`Processing attribute: ${name} - ${attribute.__type}`, this.config.context);
// skip creating preset attributes, as they are already present in the system
if (attribute.__type === 'PRESET') {
cli_utilities_1.log.debug(`Skipping preset attribute: ${name}`, this.config.context);
continue;
}
try {
cli_utilities_1.log.debug(`Creating custom attribute: ${name}`, this.config.context);
const attributeRes = yield this.createAttribute({ key, name, description });
//map old attribute uid to new attribute uid
//mapper file is used to check whether attribute created or not before creating audience
this.attributesUidMapper[uid] = (_a = attributeRes === null || attributeRes === void 0 ? void 0 : attributeRes.uid) !== null && _a !== void 0 ? _a : '';
cli_utilities_1.log.debug(`Created attribute: ${uid} -> ${attributeRes === null || attributeRes === void 0 ? void 0 : attributeRes.uid}`, this.config.context);
}
catch (error) {
(0, cli_utilities_1.handleAndLogError)(error, this.config.context, `Failed to create attribute: ${name}`);
}
}
utils_1.fsUtil.writeFile(this.attributesUidMapperPath, this.attributesUidMapper);
cli_utilities_1.log.debug(`Saved ${Object.keys(this.attributesUidMapper).length} attribute mappings to: ${this.attributesUidMapperPath}`, this.config.context);
cli_utilities_1.log.success('Attributes imported successfully', this.config.context);
}
catch (error) {
(0, cli_utilities_1.handleAndLogError)(error, this.config.context);
}
}
else {
cli_utilities_1.log.warn(`Attributes file not found: ${attributesPath}`, this.config.context);
}
});
}
}
exports.default = Attribute;