UNPKG

@contentstack/cli-variants

Version:

Variants plugin

83 lines (82 loc) 5.39 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 ExportEvents 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.personalizeConfig = exportConfig.modules.personalize; this.eventsConfig = exportConfig.modules.events; this.eventsFolderPath = (0, node_path_1.resolve)(exportConfig.data, exportConfig.branchName || '', this.personalizeConfig.dirName, this.eventsConfig.dirName); this.events = []; this.exportConfig.context.module = 'events'; } start() { return __awaiter(this, void 0, void 0, function* () { var _a, _b; try { cli_utilities_1.log.debug('Starting events export process...', this.exportConfig.context); cli_utilities_1.log.info('Starting events 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 events directory at: ${this.eventsFolderPath}`, this.exportConfig.context); yield utils_1.fsUtil.makeDirectory(this.eventsFolderPath); cli_utilities_1.log.debug('Events directory created successfully', this.exportConfig.context); cli_utilities_1.log.debug('Fetching events from Personalize API...', this.exportConfig.context); this.events = (yield this.getEvents()); cli_utilities_1.log.debug(`Fetched ${((_a = this.events) === null || _a === void 0 ? void 0 : _a.length) || 0} events`, this.exportConfig.context); if (!((_b = this.events) === null || _b === void 0 ? void 0 : _b.length)) { cli_utilities_1.log.debug('No events found, completing export', this.exportConfig.context); cli_utilities_1.log.info('No events found for the given project.', this.exportConfig.context); return; } else { cli_utilities_1.log.debug(`Processing ${this.events.length} events`, this.exportConfig.context); this.sanitizeAttribs(); cli_utilities_1.log.debug('Events sanitization completed', this.exportConfig.context); const eventsFilePath = (0, node_path_1.resolve)(this.eventsFolderPath, this.eventsConfig.fileName); cli_utilities_1.log.debug(`Writing events to: ${eventsFilePath}`, this.exportConfig.context); utils_1.fsUtil.writeFile(eventsFilePath, this.events); cli_utilities_1.log.debug('Events export completed successfully', this.exportConfig.context); cli_utilities_1.log.success(`Events exported successfully! Total events: ${this.events.length}`, this.exportConfig.context); return; } } catch (error) { cli_utilities_1.log.debug(`Error occurred during events export: ${error}`, this.exportConfig.context); (0, cli_utilities_1.handleAndLogError)(error, Object.assign({}, this.exportConfig.context)); } }); } /** * function to remove invalid keys from event object */ sanitizeAttribs() { var _a, _b; cli_utilities_1.log.debug(`Sanitizing ${((_a = this.events) === null || _a === void 0 ? void 0 : _a.length) || 0} events`, this.exportConfig.context); cli_utilities_1.log.debug(`Invalid keys to remove: ${JSON.stringify(this.eventsConfig.invalidKeys)}`, this.exportConfig.context); this.events = ((_b = this.events) === null || _b === void 0 ? void 0 : _b.map((event) => (0, omit_1.default)(event, this.eventsConfig.invalidKeys))) || []; cli_utilities_1.log.debug(`Sanitization complete. Total events after sanitization: ${this.events.length}`, this.exportConfig.context); } } exports.default = ExportEvents;