@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
97 lines (96 loc) • 4.87 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.downloadDotenv = exports.downloadDotenvByAppSlug = exports.downloadDotenvByApp = exports.getDotenvContentByApp = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const fs_1 = require("fs");
const inquirer_1 = __importDefault(require("inquirer"));
const path_1 = __importDefault(require("path"));
const plugins_1 = require("../../plugins");
const app_helper_1 = require("../apps/app-helper");
const ask_project_and_app_1 = require("../apps/ask-project-and-app");
const dotenv_exec_1 = require("./dotenv-exec");
const getDotenvContentByApp = (app, env = "dev") => {
const { deployEnvironment, slug: appSlug } = app;
if (!deployEnvironment || !deployEnvironment[env])
throw new Error(`Can't download dotenv variables to ".env.${env}" locally due to deploy environment of "${appSlug}" app not existed.`);
const envVars = deployEnvironment[env].envVars || [];
return (0, plugins_1.kubeEnvToDotenv)(envVars);
};
exports.getDotenvContentByApp = getDotenvContentByApp;
const downloadDotenvByApp = async (app, env = "dev", options = {}) => {
const { targetDir = process.cwd(), fileName = `.env.${env}`, overwrite } = options;
const dotenvContent = (0, exports.getDotenvContentByApp)(app, env);
const filePath = path_1.default.resolve(targetDir, fileName);
if ((0, fs_1.existsSync)(filePath)) {
if (!overwrite) {
const { shouldOverwrite } = await inquirer_1.default.prompt({
name: "shouldOverwrite",
type: "confirm",
default: true,
message: `File "${fileName}" is existed, do you want to overwrite it?`,
});
if (shouldOverwrite) {
(0, fs_1.unlinkSync)(filePath);
}
else {
throw new Error(`Download DOTENV of "${env}" deploy environment failed, local DOTENV file is existed.`);
}
}
else {
(0, fs_1.unlinkSync)(filePath);
}
}
(0, fs_1.writeFileSync)(filePath, dotenvContent, "utf8");
(0, log_1.logSuccess)(`Downloaded "./${fileName}" file from "${env}" deploy environment of "${app.slug}" app.`);
return { filePath, fileName, content: dotenvContent };
};
exports.downloadDotenvByApp = downloadDotenvByApp;
const downloadDotenvByAppSlug = async (appSlug, env = "dev", options = {}) => {
const { DB } = await Promise.resolve().then(() => __importStar(require("../../modules/api/DB")));
const app = await DB.findOne("app", { slug: appSlug });
if (!app)
throw new Error(`Can't download dotenv variables to ".env.${env}" locally due to "${appSlug}" app not existed.`);
return (0, exports.downloadDotenvByApp)(app, env, options);
};
exports.downloadDotenvByAppSlug = downloadDotenvByAppSlug;
const downloadDotenv = async (env, options = {}) => {
const { targetDir = process.cwd() } = options;
const { app } = await (0, ask_project_and_app_1.askForProjectAndApp)(targetDir, options);
const appConfig = (0, app_helper_1.getAppConfigFromApp)(app, options);
const { slug: appSlug } = appConfig;
if (!appSlug) {
throw new Error(`Invalid working directory, the current deploy environment config on Diginext workspace might be corrupted, please re-initialize.`);
}
return (0, exports.downloadDotenvByAppSlug)(appSlug, env, options).then(async (result) => {
// [SECURITY CHECK] warns if DOTENV files are not listed in ".gitignore" file
await (0, dotenv_exec_1.checkGitignoreContainsDotenvFiles)({ targetDir });
return result;
});
};
exports.downloadDotenv = downloadDotenv;