UNPKG

@contentstack/cli-cm-bootstrap

Version:
176 lines (175 loc) 7.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DEFAULT_MASTER_LOCALE = void 0; const cli_command_1 = require("@contentstack/cli-command"); const path_1 = require("path"); const bootstrap_1 = require("../../bootstrap"); const interactive_1 = require("../../bootstrap/interactive"); const cli_utilities_1 = require("@contentstack/cli-utilities"); const config_1 = require("../../config"); const messages_1 = require("../../messages"); exports.DEFAULT_MASTER_LOCALE = 'en-us'; class BootstrapCommand extends cli_command_1.Command { async run() { const { flags: bootstrapCommandFlags } = await this.parse(BootstrapCommand); const managementTokenAlias = bootstrapCommandFlags.alias; try { if (!(0, cli_utilities_1.isAuthenticated)() && !managementTokenAlias) { this.error(messages_1.default.parse('CLI_BOOTSTRAP_LOGIN_FAILED'), { exit: 2, suggestions: ['https://www.contentstack.com/docs/developers/cli/authentication/'], }); } this.bootstrapManagementAPIClient = await (0, cli_utilities_1.managementSDKClient)({ host: this.cmaHost, }); // inquire user inputs let appType = bootstrapCommandFlags.appType || bootstrapCommandFlags['app-type'] || 'starterapp'; if (!appType) { appType = await (0, interactive_1.inquireAppType)(); } const selectedAppName = bootstrapCommandFlags.appName || bootstrapCommandFlags['app-name']; let selectedApp; if (!selectedAppName) { if (appType === 'sampleapp') { selectedApp = await (0, interactive_1.inquireApp)(config_1.default.sampleApps); } else if (appType === 'starterapp') { selectedApp = await (0, interactive_1.inquireApp)(config_1.default.starterApps); } else { this.error('Invalid app type provided ' + appType, { exit: 1 }); } } if (!selectedAppName && !selectedApp) { this.error(messages_1.default.parse('CLI_BOOTSTRAP_INVALID_APP_NAME'), { exit: 1, }); } const yes = bootstrapCommandFlags.yes; const appConfig = (0, config_1.getAppLevelConfigByName)(selectedAppName || selectedApp.configKey); const master_locale = appConfig.master_locale || exports.DEFAULT_MASTER_LOCALE; let cloneDirectory = bootstrapCommandFlags.directory || bootstrapCommandFlags['project-dir']; if (!cloneDirectory) { cloneDirectory = await (0, interactive_1.inquireCloneDirectory)(); } cloneDirectory = (0, path_1.resolve)(cloneDirectory); const livePreviewEnabled = bootstrapCommandFlags.yes ? true : await (0, interactive_1.inquireLivePreviewSupport)(); const seedParams = {}; const stackAPIKey = bootstrapCommandFlags['stack-api-key']; const org = bootstrapCommandFlags.org; const stackName = bootstrapCommandFlags['stack-name']; if (stackAPIKey) seedParams.stackAPIKey = stackAPIKey; if (org) seedParams.org = org; if (stackName) seedParams.stackName = stackName; if (yes) seedParams.yes = yes; if (managementTokenAlias) { seedParams.managementTokenAlias = managementTokenAlias; const listOfTokens = cli_utilities_1.configHandler.get('tokens'); const managementToken = listOfTokens[managementTokenAlias].token; seedParams.managementToken = managementToken; } // initiate bootstrsourceap const options = { appConfig, seedParams, cloneDirectory, managementAPIClient: this.bootstrapManagementAPIClient, region: this.region, appType, livePreviewEnabled, master_locale, }; const bootstrap = new bootstrap_1.default(options); await bootstrap.run(); } catch (error) { this.error(error, { exit: 1, suggestions: error.suggestions }); } } } exports.default = BootstrapCommand; BootstrapCommand.description = 'Bootstrap contentstack apps'; BootstrapCommand.examples = [ '$ csdx cm:bootstrap', '$ csdx cm:bootstrap --project-dir <path/to/setup/the/app>', '$ csdx cm:bootstrap --app-name "reactjs-starter" --project-dir <path/to/setup/the/app>', '$ csdx cm:bootstrap --app-name "reactjs-starter" --project-dir <path/to/setup/the/app> --stack-api-key "stack-api-key"', '$ csdx cm:bootstrap --app-name "reactjs-starter" --project-dir <path/to/setup/the/app> --org "your-org-uid" --stack-name "stack-name"', ]; BootstrapCommand.flags = { 'app-name': cli_utilities_1.flags.string({ description: 'App name, reactjs-starter, nextjs-starter, gatsby-starter, angular-starter, nuxt-starter, vue-starter, stencil-starter', multiple: false, required: false, }), 'project-dir': cli_utilities_1.flags.string({ description: 'Directory to setup the project. If directory name has a space then provide the path as a string or escap the space using back slash eg: "../../test space" or ../../test\\ space', multiple: false, required: false, }), 'app-type': cli_utilities_1.flags.string({ description: 'Sample or Starter app', multiple: false, required: false, hidden: true, }), 'stack-api-key': cli_utilities_1.flags.string({ char: 'k', description: 'Provide stack API key to seed content', multiple: false, required: false, exclusive: ['org', 'stack-name'], }), org: cli_utilities_1.flags.string({ description: 'Provide organization UID to create a new stack', multiple: false, required: false, exclusive: ['stack-api-key'], }), 'stack-name': cli_utilities_1.flags.string({ char: 'n', description: 'Name of the new stack that will be created.', multiple: false, required: false, exclusive: ['stack-api-key'], }), yes: cli_utilities_1.flags.string({ description: '[Optional] Skip stack confirmation', char: 'y', required: false, }), // To be deprecated appName: cli_utilities_1.flags.string({ char: 'a', description: 'App name, reactjs-starter, nextjs-starter, gatsby-starter, angular-starter, nuxt-starter', multiple: false, required: false, hidden: true, parse: (0, cli_utilities_1.printFlagDeprecation)(['-a', '--appName'], ['--app-name']), }), directory: cli_utilities_1.flags.string({ char: 'd', description: 'Directory to set up the project. If directory name has a space then provide the path as a string or escape the space using backslash eg: "../../test space" or ../../test\\ space', multiple: false, required: false, hidden: true, parse: (0, cli_utilities_1.printFlagDeprecation)(['-d', '--directory'], ['--project-dir']), }), appType: cli_utilities_1.flags.string({ char: 's', description: 'Sample or Starter app', multiple: false, required: false, hidden: true, parse: (0, cli_utilities_1.printFlagDeprecation)(['-s', '--appType'], ['--app-type']), }), alias: cli_utilities_1.flags.string({ char: 'a', description: 'Alias of the management token', }), };