UNPKG

@contentstack/cli-variants

Version:

Variants plugin

82 lines (81 loc) 5.38 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 Project extends utils_1.PersonalizationAdapter { constructor(config) { const conf = { config, baseURL: config.modules.personalize.baseURL[config.region.name], headers: { organization_uid: config.org_uid }, }; super(Object.assign(config, conf)); this.config = config; this.projectMapperFolderPath = (0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.config.backupDir), 'mapper', (0, cli_utilities_1.sanitizePath)(this.config.modules.personalize.dirName), 'projects'); this.config.context.module = 'project'; } /** * The function asynchronously imports projects data from a file and creates projects based on the * data. */ import() { return __awaiter(this, void 0, void 0, function* () { const personalize = this.config.modules.personalize; const { dirName, fileName } = personalize.projects; const projectPath = (0, path_1.join)((0, cli_utilities_1.sanitizePath)(this.config.data), (0, cli_utilities_1.sanitizePath)(personalize.dirName), (0, cli_utilities_1.sanitizePath)(dirName), (0, cli_utilities_1.sanitizePath)(fileName)); cli_utilities_1.log.debug(`Checking for project file: ${projectPath}`, this.config.context); if ((0, fs_1.existsSync)(projectPath)) { const projects = JSON.parse((0, fs_1.readFileSync)(projectPath, 'utf8')); cli_utilities_1.log.debug(`Loaded ${(projects === null || projects === void 0 ? void 0 : projects.length) || 0} projects from file`, this.config.context); if (!projects || projects.length < 1) { this.config.modules.personalize.importData = false; // Stop personalize import if stack not connected to any project cli_utilities_1.log.warn('No projects found in file', this.config.context); return; } yield this.init(); for (const project of projects) { cli_utilities_1.log.debug(`Processing project: ${project.name}`, this.config.context); const createProject = (newName) => __awaiter(this, void 0, void 0, function* () { cli_utilities_1.log.debug(`Creating project with name: ${newName || project.name}`, this.config.context); return yield this.createProject({ name: newName || project.name, description: project.description, connectedStackApiKey: this.config.apiKey, }).catch((error) => __awaiter(this, void 0, void 0, function* () { if (error.includes('personalization.PROJECTS.DUPLICATE_NAME') || error.includes('personalize.PROJECTS.DUPLICATE_NAME')) { cli_utilities_1.log.warn(`Project name already exists, generating new name.`, this.config.context); const projectName = yield (0, utils_1.askProjectName)('Copy Of ' + (newName || project.name)); return yield createProject(projectName); } throw error; })); }); const projectRes = yield createProject(this.config.personalizeProjectName); this.config.modules.personalize.project_id = projectRes.uid; this.config.modules.personalize.importData = true; yield utils_1.fsUtil.makeDirectory(this.projectMapperFolderPath); utils_1.fsUtil.writeFile((0, path_1.resolve)((0, cli_utilities_1.sanitizePath)(this.projectMapperFolderPath), 'projects.json'), projectRes); cli_utilities_1.log.success(`Project created successfully: ${projectRes.uid}`, this.config.context); cli_utilities_1.log.debug(`Project data saved to: ${this.projectMapperFolderPath}/projects.json`, this.config.context); } } else { this.config.modules.personalize.importData = false; // Stop personalize import if stack not connected to any project cli_utilities_1.log.warn(`Project file not found: ${projectPath}`, this.config.context); } }); } } exports.default = Project;