@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
74 lines (73 loc) • 3.36 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;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.viewAppLogs = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const config_1 = require("../../config/config");
const plugins_1 = require("../../plugins");
const api_1 = require("../api");
async function viewAppLogs(options) {
var _a;
const { allPods = true, targetDirectory = process.cwd(), env = "dev" } = options;
const { DB } = await Promise.resolve().then(() => __importStar(require("../../modules/api/DB")));
const currentGitData = await (0, plugins_1.getCurrentGitRepoData)(targetDirectory);
const app = await DB.findOne("app", { "git.repoSSH": currentGitData.repoSSH }, { populate: ["project", "owner", "workspace"] });
if (!app)
return (0, log_1.logError)(`App not found.`);
if (options === null || options === void 0 ? void 0 : options.isDebugging)
console.log("viewAppLogs() > app :>> ", app);
try {
const { buildServerUrl } = (0, config_1.getCliConfig)();
const url = `${buildServerUrl}/api/v1/app/environment/logs?slug=${app.slug}&env=${env}`;
if (options.isDebugging)
console.log("viewAppLogs() > API url :>> ", url);
const res = await (0, api_1.fetchApi)({ url });
if (options.isDebugging) {
console.log("viewAppLogs() > response :>> ");
console.dir(res, { depth: 10 });
}
if (!res.status)
throw new Error(res.messages[0] || `Unable to reach the API: ${url}.`);
if (options === null || options === void 0 ? void 0 : options.isDebugging)
console.log("requestResult.data :>> ", res.data);
const logData = res.data;
if (!logData)
throw new Error(`No logs found.`);
const podNames = ((_a = logData.status) === null || _a === void 0 ? void 0 : _a.toString()) === "0" ? [] : Object.keys(logData);
const firstPodName = podNames[0];
const podLogs = firstPodName ? logData[firstPodName] : "";
if (allPods) {
podNames.map((podName) => console.log(logData[podName]));
}
else {
console.log(podLogs);
}
}
catch (e) {
(0, log_1.logError)(`Unable to reach the API:`, e);
return;
}
}
exports.viewAppLogs = viewAppLogs;