UNPKG

@contentstack/cli-variants

Version:

Variants plugin

92 lines (91 loc) 6.56 kB
"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 Audiences 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.audienceConfig = this.personalizeConfig.audiences; 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.audienceMapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.audienceConfig.dirName)); this.audiencesUidMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.audienceMapperDirPath), 'uid-mapping.json'); this.attributesMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.attributeConfig.dirName), 'uid-mapping.json'); this.audiencesUidMapper = {}; this.config.context.module = 'audiences'; } /** * The function asynchronously imports audiences from a JSON file and creates them in the system. */ import() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; yield this.init(); yield utils_1.fsUtil.makeDirectory(this.audienceMapperDirPath); cli_utilities_1.log.debug(`Created mapper directory: ${this.audienceMapperDirPath}`, this.config.context); const { dirName, fileName } = this.audienceConfig; const audiencesPath = (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 audiences file: ${audiencesPath}`, this.config.context); if ((0, fs_1.existsSync)(audiencesPath)) { try { const audiences = utils_1.fsUtil.readFile(audiencesPath, true); cli_utilities_1.log.info(`Found ${audiences.length} audiences to import`, this.config.context); const attributesUid = utils_1.fsUtil.readFile(this.attributesMapperPath, true) || {}; cli_utilities_1.log.debug(`Loaded ${Object.keys(attributesUid).length} attribute mappings`, this.config.context); for (const audience of audiences) { let { name, definition, description, uid } = audience; cli_utilities_1.log.debug(`Processing audience: ${name} (${uid})`, this.config.context); try { //check whether reference attributes exists or not if ((_a = definition.rules) === null || _a === void 0 ? void 0 : _a.length) { cli_utilities_1.log.debug(`Processing ${definition.rules.length} definition rules for audience: ${name}`, this.config.context); definition.rules = (0, utils_1.lookUpAttributes)(definition.rules, attributesUid); cli_utilities_1.log.debug(`Processed definition rules, remaining rules: ${definition.rules.length}`, this.config.context); } else { cli_utilities_1.log.debug(`No definition rules found for audience: ${name}`, this.config.context); } cli_utilities_1.log.debug(`Creating audience: ${name}`, this.config.context); const audienceRes = yield this.createAudience({ definition, name, description }); //map old audience uid to new audience uid //mapper file is used to check whether audience created or not before creating experience this.audiencesUidMapper[uid] = (_b = audienceRes === null || audienceRes === void 0 ? void 0 : audienceRes.uid) !== null && _b !== void 0 ? _b : ''; cli_utilities_1.log.debug(`Created audience: ${uid} -> ${audienceRes === null || audienceRes === void 0 ? void 0 : audienceRes.uid}`, this.config.context); } catch (error) { (0, cli_utilities_1.handleAndLogError)(error, this.config.context, `Failed to create audience: ${name} (${uid})`); } } utils_1.fsUtil.writeFile(this.audiencesUidMapperPath, this.audiencesUidMapper); cli_utilities_1.log.debug(`Saved ${Object.keys(this.audiencesUidMapper).length} audience mappings to: ${this.audiencesUidMapperPath}`, this.config.context); cli_utilities_1.log.success('Audiences imported successfully', this.config.context); } catch (error) { (0, cli_utilities_1.handleAndLogError)(error, this.config.context); } } else { cli_utilities_1.log.warn(`Audiences file not found: ${audiencesPath}`, this.config.context); } }); } } exports.default = Audiences;