@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
89 lines (88 loc) • 4.47 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.uploadDotenvFile = exports.uploadDotenvFileByAppSlug = exports.uploadDotenvFileByApp = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const plugins_1 = require("../../plugins");
const api_1 = require("../api");
const ask_project_and_app_1 = require("../apps/ask-project-and-app");
const dotenv_exec_1 = require("./dotenv-exec");
const uploadDotenvFileByApp = async (envFile, app, env = "dev", options) => {
const { slug: appSlug } = app;
const containerEnvVars = (0, plugins_1.loadEnvFileAsContainerEnvVars)(envFile);
// log({ containerEnvVars });
// update env vars to database:
const updateAppData = {};
updateAppData.envVars = JSON.stringify(containerEnvVars);
updateAppData.slug = appSlug;
updateAppData.env = env;
const url = `/api/v1/app/environment/variables`;
// const updateData = flattenObjectPaths(updateAppData);
const { status, data, messages } = await (0, api_1.fetchApi)({
url,
method: "POST",
data: updateAppData,
isDebugging: options === null || options === void 0 ? void 0 : options.isDebugging,
});
let updatedApp;
if (data && data.length > 0)
updatedApp = data[0];
// console.log("[DB] UPDATE > result :>> ", result);
if (!status)
(0, log_1.logError)(`Upload DOTENV file by app :>>`, messages);
// const [updatedApp] = await DB.update<App>("app", { slug: appSlug }, updateAppData);
if (!updatedApp)
throw new Error(`Can't upload dotenv variables to "${env}" deploy environment of "${appSlug}" app.`);
(0, log_1.log)(`Your local ENV variables (${containerEnvVars.length}) of "${appSlug}" app has been uploaded to ${env.toUpperCase()} deploy environment.`);
return updatedApp;
};
exports.uploadDotenvFileByApp = uploadDotenvFileByApp;
const uploadDotenvFileByAppSlug = async (envFile, 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 upload dotenv variables to "${env}" deploy environment due to "${appSlug}" app not found.`);
return (0, exports.uploadDotenvFileByApp)(envFile, app, env, options);
};
exports.uploadDotenvFileByAppSlug = uploadDotenvFileByAppSlug;
const uploadDotenvFile = async (env = "dev", options = {}) => {
const { targetDir = process.cwd(), fileName = `.env.${env}` } = options;
const { app } = await (0, ask_project_and_app_1.askForProjectAndApp)(targetDir);
if (!app)
throw new Error(`Unable to upload dotenv variables file to "${env}" deploy environment.`);
const envFile = path_1.default.resolve(targetDir, fileName);
if (!(0, fs_1.existsSync)(envFile)) {
throw new Error(`DOTENV file (${fileName}) is not existed.`);
}
// [SECURITY CHECK] warns if DOTENV files are not listed in ".gitignore" file
await (0, dotenv_exec_1.checkGitignoreContainsDotenvFiles)({ targetDir });
return (0, exports.uploadDotenvFileByApp)(envFile, app, env, options);
};
exports.uploadDotenvFile = uploadDotenvFile;