UNPKG

@contentstack/cli-variants

Version:

Variants plugin

81 lines (80 loc) 5.67 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const omit_1 = __importDefault(require("lodash/omit")); const node_path_1 = require("node:path"); const cli_utilities_1 = require("@contentstack/cli-utilities"); const utils_1 = require("../utils"); class ExportAttributes extends utils_1.PersonalizationAdapter { constructor(exportConfig) { super({ config: exportConfig, baseURL: exportConfig.modules.personalize.baseURL[exportConfig.region.name], headers: { 'X-Project-Uid': exportConfig.project_id }, }); this.exportConfig = exportConfig; this.exportConfig = exportConfig; this.personalizeConfig = exportConfig.modules.personalize; this.attributesConfig = exportConfig.modules.attributes; this.attributesFolderPath = (0, node_path_1.resolve)((0, cli_utilities_1.sanitizePath)(exportConfig.data), (0, cli_utilities_1.sanitizePath)(exportConfig.branchName || ''), (0, cli_utilities_1.sanitizePath)(this.personalizeConfig.dirName), (0, cli_utilities_1.sanitizePath)(this.attributesConfig.dirName)); this.attributes = []; this.exportConfig.context.module = 'attributes'; } start() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { cli_utilities_1.log.info('Starting attributes export', this.exportConfig.context); cli_utilities_1.log.debug('Initializing personalization adapter...', this.exportConfig.context); yield this.init(); cli_utilities_1.log.debug('Personalization adapter initialized successfully', this.exportConfig.context); cli_utilities_1.log.debug(`Creating attributes directory at: ${this.attributesFolderPath}`, this.exportConfig.context); yield utils_1.fsUtil.makeDirectory(this.attributesFolderPath); cli_utilities_1.log.debug('Attributes directory created successfully', this.exportConfig.context); cli_utilities_1.log.debug('Fetching attributes from personalization API...', this.exportConfig.context); this.attributes = (yield this.getAttributes()); cli_utilities_1.log.debug(`Fetched ${((_a = this.attributes) === null || _a === void 0 ? void 0 : _a.length) || 0} attributes`, this.exportConfig.context); if (!((_b = this.attributes) === null || _b === void 0 ? void 0 : _b.length)) { cli_utilities_1.log.debug('No attributes found, completing export', this.exportConfig.context); cli_utilities_1.log.info('No Attributes found with the given project!', this.exportConfig.context); } else { cli_utilities_1.log.debug(`Processing ${this.attributes.length} attributes`, this.exportConfig.context); this.sanitizeAttribs(); cli_utilities_1.log.debug('Attributes sanitization completed', this.exportConfig.context); const attributesFilePath = (0, node_path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.attributesFolderPath), (0, cli_utilities_1.sanitizePath)(this.attributesConfig.fileName)); cli_utilities_1.log.debug(`Writing attributes to: ${attributesFilePath}`, this.exportConfig.context); utils_1.fsUtil.writeFile(attributesFilePath, this.attributes); cli_utilities_1.log.debug('Attributes export completed successfully', this.exportConfig.context); cli_utilities_1.log.success(`Attributes exported successfully! Total attributes: ${this.attributes.length}`, this.exportConfig.context); } } catch (error) { cli_utilities_1.log.debug(`Error occurred during attributes export: ${error}`, this.exportConfig.context); (0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.exportConfig.context)); } }); } /** * function to remove invalid keys from attributes object */ sanitizeAttribs() { var _a, _b; cli_utilities_1.log.debug(`Sanitizing ${((_a = this.attributes) === null || _a === void 0 ? void 0 : _a.length) || 0} attributes`, this.exportConfig.context); cli_utilities_1.log.debug(`Invalid keys to remove: ${JSON.stringify(this.attributesConfig.invalidKeys)}`, this.exportConfig.context); this.attributes = ((_b = this.attributes) === null || _b === void 0 ? void 0 : _b.map((audience) => (0, omit_1.default)(audience, this.attributesConfig.invalidKeys))) || []; cli_utilities_1.log.debug(`Sanitization complete. Total attributes after sanitization: ${this.attributes.length}`, this.exportConfig.context); } } exports.default = ExportAttributes;