UNPKG

@contentstack/cli-cm-seed

Version:

create a Stack from existing content types, entries, assets, etc.

213 lines (212 loc) 9.13 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ENGLISH_LOCALE = void 0; const tmp = require("tmp"); const cli_utilities_1 = require("@contentstack/cli-utilities"); const importer = require("../seed/importer"); const client_1 = require("../seed/contentstack/client"); const interactive_1 = require("../seed/interactive"); const client_2 = require("./github/client"); const error_1 = require("./github/error"); const DEFAULT_OWNER = 'contentstack'; const DEFAULT_STACK_PATTERN = 'stack-'; exports.ENGLISH_LOCALE = 'en-us'; class ContentModelSeeder { get ghPath() { return `${this.ghUsername}/${this.ghRepo}`; } constructor(options) { this.options = options; this.parent = null; this.ghUsername = DEFAULT_OWNER; this.parent = options.parent || null; this._options = options; const gh = client_2.default.parsePath(options.gitHubPath); this.ghUsername = gh.username || DEFAULT_OWNER; this.ghRepo = gh.repo; const limit = Number(this.options.fetchLimit); this.managementToken = options.managementToken; this.csClient = new client_1.default(options.cmaHost, limit); this.ghClient = new client_2.default(this.ghUsername, DEFAULT_STACK_PATTERN); } async run() { let api_key; const { organizationResponse, stackResponse } = await this.getInput(); if (stackResponse.isNew && stackResponse.name) { api_key = await this.createStack(organizationResponse, stackResponse.name); } else { api_key = stackResponse.api_key; const proceed = await this.shouldProceed(api_key); if (!proceed) { cli_utilities_1.cliux.print('Exiting. Please re-run the command, if you wish to seed content.'); return; } } const tmpPath = await this.downloadRelease(); cli_utilities_1.cliux.print(`Importing into ${this.managementToken ? 'your stack' : `'${stackResponse.name}'`}.`); await importer.run({ api_key: api_key, cdaHost: this.options.cdaHost, cmaHost: this.options.cmaHost, master_locale: this.options.master_locale || exports.ENGLISH_LOCALE, tmpPath: tmpPath, isAuthenticated: this.options.isAuthenticated, alias: this.options.alias, }); return { api_key }; } async getInput() { if (!this.ghRepo) { await this.inquireGitHubRepo(); } let repoExists = false; let repoResponseData = {}; try { const repoCheckResult = await this.ghClient.makeGetApiCall(this.ghRepo); repoExists = repoCheckResult.statusCode === 200; repoResponseData = { status: repoCheckResult.statusCode, statusMessage: repoCheckResult.statusMessage }; } catch (error) { throw error; } if (repoExists === false) { cli_utilities_1.cliux.error(repoResponseData.status === 403 ? repoResponseData.statusMessage : `Could not find GitHub repository '${this.ghPath}'.`); if (this.parent) this.parent.exit(1); } else { let organizationResponse; let stackResponse; let stack; if (this.options.stackUid && this.options.managementToken) { stackResponse = { isNew: false, name: 'your stack', uid: this.options.stackUid, api_key: this.options.stackUid, }; } else if (this.options.stackUid) { stack = await this.csClient.getStack(this.options.stackUid); stackResponse = { isNew: false, name: stack.name, uid: stack.uid, api_key: stack.api_key, }; } else { if (this.options.orgUid) { organizationResponse = await this.csClient.getOrganization(this.options.orgUid); } else { const organizations = await this.csClient.getOrganizations(); if (!organizations || organizations.length === 0) { throw new Error('You do not have access to any organizations. Please try again or ask an Administrator for assistance.'); } organizationResponse = await (0, interactive_1.inquireOrganization)(organizations); } const stacks = await this.csClient.getStacks(organizationResponse.uid); stackResponse = await (0, interactive_1.inquireStack)(stacks, this.options.stackName); } return { organizationResponse, stackResponse }; } } async createStack(organization, stackName) { cli_utilities_1.cliux.loader(`Creating Stack '${stackName}' within Organization '${organization.name}'`); const newStack = await this.csClient.createStack({ name: stackName, description: '', master_locale: this.options.master_locale || exports.ENGLISH_LOCALE, org_uid: organization.uid, }); cli_utilities_1.cliux.loader(); return newStack.api_key; } async shouldProceed(api_key) { let count; const stack_details = await this.csClient.getStack(api_key); if (this.options.master_locale != stack_details.master_locale) { cli_utilities_1.cliux.print(`Compass app requires the master locale to be set to English (en).`, { color: "yellow", bold: true, }); return false; } const managementBody = { "name": "Checking roles for creating management token", "description": "This is a compass app management token.", "scope": [ { "module": "content_type", "acl": { "read": true, "write": true } }, { "module": "branch", "branches": [ "main" ], "acl": { "read": true } } ], "expires_on": "3000-01-01", "is_email_notification_enabled": false }; let managementTokenResult = await this.csClient.createManagementToken(api_key, this.managementToken, managementBody); if ((managementTokenResult === null || managementTokenResult === void 0 ? void 0 : managementTokenResult.response_code) == "161" || (managementTokenResult === null || managementTokenResult === void 0 ? void 0 : managementTokenResult.response_code) == "401") { cli_utilities_1.cliux.print(`Info: Failed to generate a management token.\nNote: Management token is not available in your plan. Please contact the admin for support.`, { color: 'red', }); return false; } count = await this.csClient.getContentTypeCount(api_key, this.managementToken); if (count > 0 && this._options.skipStackConfirmation !== 'yes') { const proceed = await (0, interactive_1.inquireProceed)(); if (!proceed) { return false; } } return true; } async downloadRelease() { const tmpDir = tmp.dirSync({ unsafeCleanup: true, }); cli_utilities_1.cliux.print(`Creating temporary directory '${tmpDir.name}'.`); cli_utilities_1.cliux.loader('Downloading and extracting Stack'); try { await this.ghClient.getLatest(this.ghRepo, tmpDir.name); } catch (error) { if (error instanceof error_1.default) { if (error.status === 404) { cli_utilities_1.cliux.error(`Unable to find a release for '${this.ghPath}'.`); } } } finally { cli_utilities_1.cliux.loader(); } return tmpDir.name; } async inquireGitHubRepo() { try { const allRepos = await this.ghClient.getAllRepos(); const stackRepos = allRepos.filter((repo) => repo.name.startsWith(DEFAULT_STACK_PATTERN)); const repoResponse = await (0, interactive_1.inquireRepo)(stackRepos); this.ghRepo = repoResponse.choice; } catch (error) { cli_utilities_1.cliux.error(`Unable to find any Stack repositories within the '${this.ghUsername}' GitHub account. Please re-run this command with a GitHub repository in the 'account/repo' format. You can also re-run the command without arguments to pull from the official Stack list.`); } } } exports.default = ContentModelSeeder;