@contentstack/cli-cm-bootstrap
Version:
Bootstrap contentstack apps
90 lines (89 loc) • 3.91 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ENGLISH_LOCALE = void 0;
const path = require("path");
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('Note: Before running the app, please configure a preview token, preview host, and app host in the environment file', {
color: 'yellow',
});
}
cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_SUCCESS'));
}
catch (error) {
cli_utilities_1.cliux.error(messages_1.default.parse('CLI_BOOTSTRAP_STACK_CREATION_FAILED', this.appConfig.stack));
}
}
}
exports.default = Bootstrap;