@contentstack/cli-variants
Version:
Variants plugin
105 lines (104 loc) • 6.38 kB
JavaScript
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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 = __importStar(require("path"));
const cli_utilities_1 = require("@contentstack/cli-utilities");
const utils_1 = require("../utils");
class ExportProjects extends utils_1.PersonalizationAdapter {
constructor(exportConfig) {
super({
config: exportConfig,
baseURL: exportConfig.modules.personalize.baseURL[exportConfig.region.name],
headers: { organization_uid: exportConfig.org_uid },
});
this.exportConfig = exportConfig;
this.personalizeConfig = exportConfig.modules.personalize;
this.projectFolderPath = path.resolve((0, cli_utilities_1.sanitizePath)(exportConfig.data), (0, cli_utilities_1.sanitizePath)(exportConfig.branchName || ''), (0, cli_utilities_1.sanitizePath)(this.personalizeConfig.dirName), 'projects');
this.exportConfig.context.module = 'projects';
}
start() {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
try {
cli_utilities_1.log.debug('Starting projects export process...', this.exportConfig.context);
cli_utilities_1.log.info(`Starting projects 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 projects directory at: ${this.projectFolderPath}`, this.exportConfig.context);
yield utils_1.fsUtil.makeDirectory(this.projectFolderPath);
cli_utilities_1.log.debug('Projects directory created successfully', this.exportConfig.context);
cli_utilities_1.log.debug(`Fetching projects for stack API key: ${this.exportConfig.apiKey}`, this.exportConfig.context);
const project = yield this.projects({ connectedStackApiKey: this.exportConfig.apiKey });
cli_utilities_1.log.debug(`Fetched ${(project === null || project === void 0 ? void 0 : project.length) || 0} projects`, this.exportConfig.context);
if (!project || (project === null || project === void 0 ? void 0 : project.length) < 1) {
cli_utilities_1.log.debug('No projects found, disabling personalization', this.exportConfig.context);
cli_utilities_1.log.info(`No Personalize Project connected with the given stack`, this.exportConfig.context);
this.exportConfig.personalizationEnabled = false;
return;
}
cli_utilities_1.log.debug(`Found ${project.length} projects, enabling personalization`, this.exportConfig.context);
this.exportConfig.personalizationEnabled = true;
this.exportConfig.project_id = (_a = project[0]) === null || _a === void 0 ? void 0 : _a.uid;
cli_utilities_1.log.debug(`Set project ID: ${(_b = project[0]) === null || _b === void 0 ? void 0 : _b.uid}`, this.exportConfig.context);
const projectsFilePath = path.resolve((0, cli_utilities_1.sanitizePath)(this.projectFolderPath), 'projects.json');
cli_utilities_1.log.debug(`Writing projects data to: ${projectsFilePath}`, this.exportConfig.context);
utils_1.fsUtil.writeFile(projectsFilePath, project);
cli_utilities_1.log.debug('Projects export completed successfully', this.exportConfig.context);
cli_utilities_1.log.success(`Projects exported successfully!`, this.exportConfig.context);
}
catch (error) {
if (error !== 'Forbidden') {
cli_utilities_1.log.debug(`Error occurred during projects export: ${error}`, this.exportConfig.context);
cli_utilities_1.log.error('Failed to export projects!', this.exportConfig.context);
}
else {
cli_utilities_1.log.debug('Projects export forbidden, likely due to permissions', this.exportConfig.context);
}
throw error;
}
});
}
}
exports.default = ExportProjects;