@contentstack/cli-variants
Version:
Variants plugin
77 lines (76 loc) • 4.91 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 Events 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.eventsConfig = this.personalizeConfig.events;
this.mapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.backupDir), 'mapper', (0, cli_utilities_1.sanitizePath)(this.personalizeConfig.dirName));
this.eventMapperDirPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.mapperDirPath), (0, cli_utilities_1.sanitizePath)(this.eventsConfig.dirName));
this.eventsUidMapperPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.eventMapperDirPath), 'uid-mapping.json');
this.eventsUidMapper = {};
this.config.context.module = 'events';
}
/**
* The function asynchronously imports events 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.eventMapperDirPath);
cli_utilities_1.log.debug(`Created mapper directory: ${this.eventMapperDirPath}`, this.config.context);
const { dirName, fileName } = this.eventsConfig;
const eventsPath = (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 events file: ${eventsPath}`, this.config.context);
if ((0, fs_1.existsSync)(eventsPath)) {
try {
const events = utils_1.fsUtil.readFile(eventsPath, true);
cli_utilities_1.log.info(`Found ${events.length} events to import`, this.config.context);
for (const event of events) {
const { key, description, uid } = event;
cli_utilities_1.log.debug(`Processing event: ${key} (${uid})`, this.config.context);
try {
cli_utilities_1.log.debug(`Creating event: ${key}`, this.config.context);
const eventsResponse = yield this.createEvents({ key, description });
this.eventsUidMapper[uid] = (_a = eventsResponse === null || eventsResponse === void 0 ? void 0 : eventsResponse.uid) !== null && _a !== void 0 ? _a : '';
cli_utilities_1.log.debug(`Created event: ${uid} -> ${eventsResponse === null || eventsResponse === void 0 ? void 0 : eventsResponse.uid}`, this.config.context);
}
catch (error) {
(0, cli_utilities_1.handleAndLogError)(error, this.config.context, `Failed to create event: ${key} (${uid})`);
}
}
utils_1.fsUtil.writeFile(this.eventsUidMapperPath, this.eventsUidMapper);
cli_utilities_1.log.debug(`Saved ${Object.keys(this.eventsUidMapper).length} event mappings to: ${this.eventsUidMapperPath}`, this.config.context);
cli_utilities_1.log.success('Events imported successfully', this.config.context);
}
catch (error) {
(0, cli_utilities_1.handleAndLogError)(error, this.config.context);
}
}
else {
cli_utilities_1.log.warn(`Events file not found: ${eventsPath}`, this.config.context);
}
});
}
}
exports.default = Events;