@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
119 lines (118 loc) • 6.15 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.execInitApp = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const inquirer_1 = __importDefault(require("inquirer"));
const plugins_1 = require("../../plugins");
const ask_for_git_provider_1 = require("../git/ask-for-git-provider");
const printInformation_1 = require("../project/printInformation");
const app_helper_1 = require("./app-helper");
const create_or_select_app_1 = require("./create-or-select-app");
const create_or_select_project_1 = require("./create-or-select-project");
const search_apps_1 = require("./search-apps");
async function execInitApp(options) {
const gitInfo = await (0, plugins_1.getCurrentGitRepoData)(options.targetDirectory);
if (options.isDebugging)
console.log("[INIT APP] gitInfo :>> ", gitInfo);
const { DB } = await Promise.resolve().then(() => __importStar(require("../../modules/api/DB")));
if (gitInfo === null || gitInfo === void 0 ? void 0 : gitInfo.repoSSH) {
const foundApps = await (0, search_apps_1.searchApps)({ repoSSH: gitInfo === null || gitInfo === void 0 ? void 0 : gitInfo.repoSSH });
if (foundApps && foundApps.length > 0) {
// display list to select:
foundApps.unshift({ name: "Create new", slug: "new", projectSlug: "" });
const { selectedApp } = await inquirer_1.default.prompt({
type: "list",
name: "selectedApp",
message: `Select your app or create new:`,
choices: foundApps.map((_app, i) => {
return { name: `[${i + 1}] ${_app.slug} (Project: ${_app.projectSlug})`, value: _app };
}),
});
if (selectedApp.name !== "Create new" && selectedApp.slug !== "new") {
// assign project info to "options":
options.project = selectedApp.project;
options.projectSlug = options.project.slug;
options.projectName = options.project.name;
options.namespace = `${options.project.slug}-${options.env || "dev"}`;
// assign app info to "options":
options.app = selectedApp;
options.slug = options.app.slug;
options.name = options.app.name;
if (!selectedApp.git || !selectedApp.git.repoSSH || !selectedApp.git.repoURL) {
(0, log_1.logError)(`Unable to select: app "${selectedApp.slug}" is corrupted.`);
return;
}
// git provider
options.git = selectedApp.gitProvider
? await DB.findOne("git", { _id: selectedApp.gitProvider }, { ignorable: true })
: await (0, ask_for_git_provider_1.askForGitProvider)();
if (!selectedApp.gitProvider && options.git) {
await DB.updateOne("app", { _id: selectedApp._id }, { gitProvider: options.git._id });
}
// app git info
options.repoSSH = options.app.git.repoSSH;
options.repoURL = options.app.git.repoURL;
options.gitProvider = options.app.git.provider;
options.repoURL = options.repoURL;
// print project information:
const finalConfig = (0, app_helper_1.getAppConfigFromApp)(selectedApp);
(0, printInformation_1.printInformation)(finalConfig);
return options;
}
}
}
// If no apps or projects found -> create new
const initProject = await (0, create_or_select_project_1.createOrSelectProject)(options);
const initApp = await (0, create_or_select_app_1.createOrSelectApp)(initProject.slug, options);
// ! The ONLY different with "createApp":
// ! Select the current working directory instead of create new one
options.skipCreatingDirectory = true;
if (typeof options.targetDirectory == "undefined")
options.targetDirectory = process.cwd();
// update framework & GIT info in the database
const updateData = {};
if (options.framework)
updateData.framework = options.framework;
updateData.git = {};
updateData.git.provider = gitInfo.provider;
updateData.git.repoURL = gitInfo.repoURL;
updateData.git.repoSSH = gitInfo.repoSSH;
if (options.isDebugging)
console.log("[INIT APP] updateData :>> ", updateData);
const [updatedApp] = await DB.update("app", { slug: initApp.slug }, updateData);
if (options.isDebugging)
console.log("[INIT APP] updatedApp :>> ", updatedApp);
if (!updatedApp)
(0, log_1.logError)(`[INIT APP] Can't initialize app due to network issue.`);
// print project information:
const finalConfig = (0, app_helper_1.getAppConfigFromApp)(updatedApp);
(0, printInformation_1.printInformation)(finalConfig);
return options;
}
exports.execInitApp = execInitApp;