UNPKG

@contentstack/cli-cm-bootstrap

Version:
134 lines (133 loc) 6.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ENGLISH_LOCALE = void 0; const path = require("path"); const child_process_1 = require("child_process"); const cli_utilities_1 = require("@contentstack/cli-utilities"); const seed_1 = require("@contentstack/cli-cm-seed/lib/commands/cm/stacks/seed"); const messages_1 = require("../messages"); const utils_1 = require("./utils"); const github_error_1 = require("./github/github-error"); const client_1 = require("./github/client"); exports.ENGLISH_LOCALE = 'en-us'; /** * @description Bootstraps the sample app * Clone the repo * Create the stack from the source * Setup the environment * Ready to use!! */ class Bootstrap { constructor(options) { this.options = options; this.region = options.region; this.appConfig = options.appConfig; this.managementAPIClient = options.managementAPIClient; this.repo = client_1.default.parsePath(options.appConfig.source); if (options.appConfig.branch) { this.repo.branch = options.appConfig.branch; } this.cloneDirectory = path.join((0, cli_utilities_1.sanitizePath)(options.cloneDirectory), (0, cli_utilities_1.sanitizePath)(this.repo.name)); this.ghClient = new client_1.default(this.repo, options.appConfig.private, options.accessToken); this.options = options; } async run() { cli_utilities_1.cliux.loader(messages_1.default.parse('CLI_BOOTSTRAP_START_CLONE_APP')); try { await this.ghClient.getLatest(this.cloneDirectory); } catch (error) { if (error instanceof github_error_1.default) { if (error.status === 404) { cli_utilities_1.cliux.error(messages_1.default.parse('CLI_BOOTSTRAP_REPO_NOT_FOUND', this.appConfig.source)); } } throw error; } finally { cli_utilities_1.cliux.loader(); } // seed plugin start try { const cmd = ['--repo', this.appConfig.stack]; if (this.options.seedParams.stackAPIKey) { cmd.push('--stack-api-key', this.options.seedParams.stackAPIKey); } if (this.options.seedParams.org) { cmd.push('--org', this.options.seedParams.org); } if (this.options.seedParams.stackName) { cmd.push('-n', this.options.seedParams.stackName); } if (this.options.seedParams.yes) { cmd.push('-y', this.options.seedParams.yes); } if (this.options.seedParams.managementTokenAlias) { cmd.push('--alias', this.options.seedParams.managementTokenAlias); } if (this.options.master_locale) { cmd.push('--locale', this.options.master_locale); } const result = await seed_1.default.run(cmd); if (result && result.api_key) { await (0, utils_1.setupEnvironments)(this.managementAPIClient, result.api_key, this.appConfig, this.cloneDirectory, this.region, this.options.livePreviewEnabled, this.options.seedParams.managementToken); } else { throw new Error(messages_1.default.parse('CLI_BOOTSTRAP_NO_API_KEY_FOUND')); } if (this.options.livePreviewEnabled) { cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_SUCCESS_LIVE_PREVIEW_NOTE'), { color: 'yellow', }); } cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_SUCCESS')); // Install dependencies and start development server if requested (after all other operations) if (this.options.runDevServer) { // Install project dependencies cli_utilities_1.cliux.loader(messages_1.default.parse('CLI_BOOTSTRAP_INSTALLING_DEPENDENCIES')); try { (0, child_process_1.execSync)('npm install', { cwd: this.cloneDirectory, stdio: 'inherit' }); cli_utilities_1.cliux.loader(); cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_DEPENDENCIES_INSTALLED')); // Start development server cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_STARTING_DEV_SERVER')); cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_DEV_SERVER_STARTED')); cli_utilities_1.cliux.print('You can now access your application. Check the output above for the local URL.'); // Run npm run dev using spawn for long-running process const devProcess = (0, child_process_1.spawn)('npm', ['run', 'dev'], { cwd: this.cloneDirectory, stdio: 'inherit', shell: true }); devProcess.on('error', (error) => { cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_DEV_SERVER_FAILED'), { color: 'yellow', }); console.error('Failed to start dev server:', error); }); // Handle process exit devProcess.on('exit', (code) => { if (code !== 0 && code !== null) { cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_DEV_SERVER_FAILED'), { color: 'yellow', }); } }); } catch (installError) { cli_utilities_1.cliux.loader(); cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_DEPENDENCIES_INSTALL_FAILED'), { color: 'yellow', }); } } } catch (error) { cli_utilities_1.cliux.error(messages_1.default.parse('CLI_BOOTSTRAP_STACK_CREATION_FAILED', this.appConfig.stack)); } } } exports.default = Bootstrap;