@contentstack/cli-cm-bootstrap
Version:
Bootstrap contentstack apps
269 lines (268 loc) • 18.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupEnvironments = void 0;
const fs = require("fs");
const path = require("path");
const cli_utilities_1 = require("@contentstack/cli-utilities");
const interactive_1 = require("../bootstrap/interactive");
const messages_1 = require("../messages");
/**
* @description Setup the environment for a given app for each environment
* Loads the environments for a given stack
* Create delivery token
* Create enviroment
*/
let managementTokenResult;
const setupEnvironments = async (managementAPIClient, api_key, appConfig, clonedDirectory, region, livePreviewEnabled, managementToken) => {
var _a, _b;
const environmentResult = await managementAPIClient
.stack({ api_key, management_token: managementToken })
.environment()
.query()
.find();
//create management token if not present
if (!managementToken) {
const managementBody = {
token: {
name: 'sample app',
description: 'This is a sample 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,
},
};
managementTokenResult = await managementAPIClient
.stack({ api_key: api_key })
.managementToken()
.create(managementBody);
if (!managementTokenResult.uid) {
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: 'yellow',
});
if ((await (0, interactive_1.continueBootstrapCommand)()) === 'no') {
return;
}
}
}
if (Array.isArray(environmentResult.items) && environmentResult.items.length > 0) {
for (const environment of environmentResult.items) {
if (environment.name) {
const body = {
token: {
name: `Sample app ${environment.name}`,
description: 'Sample app',
scope: [
{
module: 'environment',
environments: [environment.name],
acl: { read: true },
},
{
module: 'branch',
acl: { read: true },
branches: ['main'],
},
],
},
};
try {
const tokenResult = !managementToken
? await managementAPIClient
.stack({ api_key })
.deliveryToken()
.create(body, livePreviewEnabled ? { create_with_preview_token: true } : {})
: {};
if (livePreviewEnabled && !tokenResult.preview_token && !managementToken) {
cli_utilities_1.cliux.print(`Info: Failed to generate a preview token for the ${environment.name} environment.\nNote: Live Preview using a preview token is not available in your plan. Please contact the admin for support.`, {
color: 'yellow',
});
if ((await (0, interactive_1.continueBootstrapCommand)()) === 'no') {
return;
}
}
if (tokenResult.token) {
const environmentVariables = {
api_key,
deliveryToken: (_a = tokenResult.token) !== null && _a !== void 0 ? _a : '',
environment: environment.name,
livePreviewEnabled,
preview_token: (_b = tokenResult.preview_token) !== null && _b !== void 0 ? _b : '',
};
await envFileHandler(appConfig.appConfigKey || '', environmentVariables, clonedDirectory, region, livePreviewEnabled);
}
else {
cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_APP_FAILED_TO_CREATE_TOKEN_FOR_ENV', environment.name));
}
}
catch (error) {
cli_utilities_1.cliux.print(messages_1.default.parse('CLI_BOOTSTRAP_APP_FAILED_TO_CREATE_ENV_FILE_FOR_ENV', environment.name));
}
}
else {
cli_utilities_1.cliux.print('No environment name found for the selected environment.');
}
}
}
else {
cli_utilities_1.cliux.error(messages_1.default.parse('CLI_BOOTSTRAP_APP_ENV_NOT_FOUND_FOR_THE_STACK'));
}
};
exports.setupEnvironments = setupEnvironments;
const writeEnvFile = (content, fileName) => {
if (!content || !fileName) {
return;
}
return new Promise((resolve, reject) => {
fs.writeFile(fileName, content, 'utf8', (error) => {
if (error) {
reject(error);
}
else {
resolve('done');
}
});
});
};
/**
* @description Create environment files for each app
* TBD: moving the content to config file
*/
const envFileHandler = async (appConfigKey, environmentVariables, clonedDirectory, region, livePreviewEnabled) => {
var _a, _b, _c, _d, _e, _f;
if (!appConfigKey || !environmentVariables) {
return;
}
let content;
let result;
let filePath;
let fileName;
let customHost;
const managementAPIHost = (_a = region === null || region === void 0 ? void 0 : region.cma) === null || _a === void 0 ? void 0 : _a.substring('8');
const regionName = region && region.name && region.name.toLowerCase();
const previewHost = (_c = (_b = region === null || region === void 0 ? void 0 : region.uiHost) === null || _b === void 0 ? void 0 : _b.substring(8)) === null || _c === void 0 ? void 0 : _c.replace('app', 'rest-preview');
const cdnHost = (_d = region === null || region === void 0 ? void 0 : region.cda) === null || _d === void 0 ? void 0 : _d.substring('8');
const appHost = (_e = region === null || region === void 0 ? void 0 : region.uiHost) === null || _e === void 0 ? void 0 : _e.substring(8);
const isUSRegion = regionName === 'us' || regionName === 'na';
if (regionName !== 'eu' && !isUSRegion) {
customHost = (_f = region === null || region === void 0 ? void 0 : region.cma) === null || _f === void 0 ? void 0 : _f.substring(8);
}
let graphqlHost = "graphql.contentstack.com";
if (regionName != 'na') {
graphqlHost = `${regionName}-.graphql.contentstack.com`;
}
// Construct image hostname based on the actual host being used
let imageHostname = '*-images.contentstack.com'; // default fallback
if (region === null || region === void 0 ? void 0 : region.cda) {
const baseHost = region.cda.replace(/^https?:\/\//, '').replace(/^[^.]+\./, '');
imageHostname = `images.${baseHost}`;
}
const production = environmentVariables.environment === 'production' ? true : false;
switch (appConfigKey) {
case 'kickstart-next':
case 'kickstart-next-ssr':
case 'kickstart-next-ssg':
case 'kickstart-next-middleware':
case 'kickstart-next-graphql':
fileName = `.env`;
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), (0, cli_utilities_1.sanitizePath)(fileName)));
content = `NEXT_PUBLIC_CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nNEXT_PUBLIC_CONTENTSTACK_DELIVERY_TOKEN=${environmentVariables.deliveryToken}\nNEXT_PUBLIC_CONTENTSTACK_PREVIEW_TOKEN=${environmentVariables.preview_token || ''}\nNEXT_PUBLIC_CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}\nNEXT_PUBLIC_CONTENTSTACK_REGION=${regionName}\nNEXT_PUBLIC_CONTENTSTACK_PREVIEW=${livePreviewEnabled ? 'true' : 'false'}\nNEXT_PUBLIC_CONTENTSTACK_CONTENT_DELIVERY = ${graphqlHost}\nNEXT_PUBLIC_CONTENTSTACK_CONTENT_APPLICATION = ${appHost}\nNEXT_PUBLIC_CONTENTSTACK_PREVIEW_HOST = ${previewHost}\nNEXT_PUBLIC_CONTENTSTACK_IMAGE_HOSTNAME=${imageHostname}`;
result = await writeEnvFile(content, filePath);
break;
case 'kickstart-nuxt':
case 'kickstart-nuxt-ssr':
fileName = `.env`;
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), (0, cli_utilities_1.sanitizePath)(fileName)));
content = `NUXT_CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nNUXT_CONTENTSTACK_DELIVERY_TOKEN=${environmentVariables.deliveryToken}\nNUXT_CONTENTSTACK_PREVIEW_TOKEN=${environmentVariables.preview_token || ''}\nNUXT_CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}\nNUXT_CONTENTSTACK_REGION=${region.name}\nNUXT_CONTENTSTACK_PREVIEW=${livePreviewEnabled ? 'true' : 'false'}\nNUXT_CONTENTSTACK_CONTENT_DELIVERY = ${cdnHost}\nNUXT_CONTENTSTACK_CONTENT_APPLICATION = ${appHost}\nNUXT_CONTENTSTACK_PREVIEW_HOST = ${previewHost}`;
result = await writeEnvFile(content, filePath);
break;
case 'reactjs':
case 'reactjs-starter':
fileName = `.env.${environmentVariables.environment}.local`;
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), (0, cli_utilities_1.sanitizePath)(fileName)));
content = `REACT_APP_CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nREACT_APP_CONTENTSTACK_DELIVERY_TOKEN=${environmentVariables.deliveryToken}${livePreviewEnabled
? `\nREACT_APP_CONTENTSTACK_PREVIEW_TOKEN=${environmentVariables.preview_token || `''`}\nREACT_APP_CONTENTSTACK_PREVIEW_HOST=${previewHost}\nREACT_APP_CONTENTSTACK_APP_HOST=${appHost}\n`
: '\n'}\nREACT_APP_CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}\n${customHost ? '\nREACT_APP_CONTENTSTACK_API_HOST=' + customHost : ''}${!isUSRegion && !customHost ? '\nREACT_APP_CONTENTSTACK_REGION=' + region.name : ''}\nSKIP_PREFLIGHT_CHECK=true\nREACT_APP_CONTENTSTACK_LIVE_PREVIEW=${livePreviewEnabled}`;
result = await writeEnvFile(content, filePath);
break;
case 'nextjs':
case 'nextjs-starter':
fileName = `.env.${environmentVariables.environment}.local`;
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), (0, cli_utilities_1.sanitizePath)(fileName)));
content = `CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nCONTENTSTACK_DELIVERY_TOKEN=${environmentVariables.deliveryToken}\n${livePreviewEnabled
? `\nCONTENTSTACK_PREVIEW_TOKEN=${environmentVariables.preview_token || `''`}\nCONTENTSTACK_PREVIEW_HOST=${previewHost}\nCONTENTSTACK_APP_HOST=${appHost}\n`
: '\n'}CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}\nCONTENTSTACK_API_HOST=${customHost ? customHost : managementAPIHost}${!isUSRegion && !customHost ? '\nCONTENTSTACK_REGION=' + region.name : ''}\nCONTENTSTACK_LIVE_PREVIEW=${livePreviewEnabled}\nCONTENTSTACK_LIVE_EDIT_TAGS=false`;
result = await writeEnvFile(content, filePath);
break;
case 'compass-app':
fileName = '.env';
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), (0, cli_utilities_1.sanitizePath)(fileName)));
content = `CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nCONTENTSTACK_DELIVERY_TOKEN=${environmentVariables.deliveryToken}\nCONTENTSTACK_BRANCH=main${livePreviewEnabled
? `\nCONTENTSTACK_PREVIEW_TOKEN=${environmentVariables.preview_token || `''`}\nCONTENTSTACK_PREVIEW_HOST=${previewHost}\n`
: '\n'}CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}${!isUSRegion && !customHost ? '\nCONTENTSTACK_REGION=' + region.name : ''}\nCONTENTSTACK_LIVE_PREVIEW=${livePreviewEnabled}\nCONTENTSTACK_LIVE_EDIT_TAGS=false\nCONTENTSTACK_API_HOST=${customHost ? customHost : managementAPIHost}${!isUSRegion && !customHost ? '\nCONTENTSTACK_REGION=' + region.name : ''}\nCONTENTSTACK_APP_HOST=${appHost}\nCONTENTSTACK_MANAGEMENT_TOKEN=${managementTokenResult.uid}\nCONTENTSTACK_HOST=${cdnHost}`;
result = await writeEnvFile(content, filePath);
break;
case 'gatsby':
case 'gatsby-starter':
fileName = `.env.${environmentVariables.environment}`;
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), (0, cli_utilities_1.sanitizePath)(fileName)));
content = `CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nCONTENTSTACK_DELIVERY_TOKEN=${environmentVariables.deliveryToken}\n${livePreviewEnabled
? `\nCONTENTSTACK_PREVIEW_TOKEN=${environmentVariables.preview_token || `''`}\nCONTENTSTACK_PREVIEW_HOST=${previewHost}\nCONTENTSTACK_APP_HOST=${appHost}\n`
: '\n'}\nCONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}\nCONTENTSTACK_API_HOST=${managementAPIHost}\nCONTENTSTACK_LIVE_PREVIEW=${livePreviewEnabled}`;
result = await writeEnvFile(content, filePath);
break;
case 'angular':
content = `export const environment = { \n\tproduction:${environmentVariables.environment === 'production' ? true : false}, \n\tconfig : { \n\t\tapi_key: '${environmentVariables.api_key}', \n\t\tdelivery_token: '${environmentVariables.deliveryToken}',\n${livePreviewEnabled
? `\n\tpreivew_token:'${environmentVariables.preview_token || `''`}'\n\tpreview_host:'${previewHost}'\n\tapp_host:'${appHost}'\n`
: '\n'},\n\t\tenvironment: '${environmentVariables.environment}'${!isUSRegion && !customHost ? `,\n\t\tregion: '${region.name}'` : ''} \n\t } \n };`;
fileName = `.env${environmentVariables.environment === 'production' ? '.prod' : ''}`;
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), 'src', 'environments', (0, cli_utilities_1.sanitizePath)(fileName)));
result = await writeEnvFile(content, filePath);
break;
case 'angular-starter':
content = `CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nCONTENTSTACK_DELIVERY_TOKEN=${environmentVariables.deliveryToken}\n${livePreviewEnabled
? `\nCONTENTSTACK_PREVIEW_TOKEN=${environmentVariables.preview_token || `''`}\nCONTENTSTACK_PREVIEW_HOST=${previewHost}\nCONTENTSTACK_APP_HOST=${appHost}\n`
: '\n'}CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}\nCONTENTSTACK_API_HOST=${customHost ? customHost : managementAPIHost}${!isUSRegion && !customHost ? '\nCONTENTSTACK_REGION=' + region.name : ''}\nCONTENTSTACK_LIVE_PREVIEW=${livePreviewEnabled}\nCONTENTSTACK_LIVE_EDIT_TAGS=false`;
fileName = `.env${environmentVariables.environment === 'production' ? '.prod' : ''}`;
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), (0, cli_utilities_1.sanitizePath)(fileName)));
result = await writeEnvFile(content, filePath);
break;
case 'nuxtjs':
case 'nuxt-starter':
case 'nuxt3-starter':
case 'stencil-starter':
fileName = production ? '.env.production' : '.env';
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), (0, cli_utilities_1.sanitizePath)(fileName)));
// Note: Stencil app needs all the env variables, even if they are not having values otherwise the rollup does not work properly and throws process in undefined error.
content = `CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nCONTENTSTACK_DELIVERY_TOKEN=${environmentVariables.deliveryToken}\n${livePreviewEnabled
? `\nCONTENTSTACK_PREVIEW_TOKEN=${environmentVariables.preview_token || `''`}\nCONTENTSTACK_PREVIEW_HOST=${customHost !== null && customHost !== void 0 ? customHost : previewHost}\nCONTENTSTACK_APP_HOST=${appHost}`
: '\n'}\nCONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}${!isUSRegion && !customHost ? '\nCONTENTSTACK_REGION=' + region.name : ''}\nCONTENTSTACK_API_HOST=${customHost ? customHost : managementAPIHost}\nCONTENTSTACK_LIVE_PREVIEW=${livePreviewEnabled}\n\nCONTENTSTACK_LIVE_EDIT_TAGS=false`;
result = await writeEnvFile(content, filePath);
break;
case 'vue-starter':
fileName = '.env';
filePath = (0, cli_utilities_1.pathValidator)(path.join((0, cli_utilities_1.sanitizePath)(clonedDirectory), (0, cli_utilities_1.sanitizePath)(fileName)));
content = `VUE_APP_CONTENTSTACK_API_KEY=${environmentVariables.api_key}\nVUE_APP_CONTENTSTACK_DELIVERY_TOKEN=${environmentVariables.deliveryToken}\n${livePreviewEnabled
? `\nVUE_APP_CONTENTSTACK_PREVIEW_TOKEN=${environmentVariables.preview_token || `''`}\nVUE_APP_CONTENTSTACK_PREVIEW_HOST=${previewHost}\nVUE_APP_CONTENTSTACK_APP_HOST=${appHost}\n`
: '\n'}\nVUE_APP_CONTENTSTACK_ENVIRONMENT=${environmentVariables.environment}${customHost ? '\nVUE_APP_CONTENTSTACK_API_HOST=' + customHost : ''}${!isUSRegion && !customHost ? '\nVUE_APP_CONTENTSTACK_REGION=' + region.name : ''}\nVUE_APP_CONTENTSTACK_LIVE_PREVIEW=${livePreviewEnabled}`;
result = await writeEnvFile(content, filePath);
break;
default:
cli_utilities_1.cliux.error(messages_1.default.parse('CLI_BOOTSTRAP_INVALID_APP_NAME'));
}
return result;
};