@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
112 lines (111 loc) • 5.29 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.createOrSelectApp = void 0;
const inquirer_1 = __importDefault(require("inquirer"));
const lodash_1 = require("lodash");
const plugins_1 = require("../../plugins");
const slug_1 = require("../../plugins/slug");
const ask_for_git_provider_1 = require("../git/ask-for-git-provider");
const new_app_by_form_1 = require("./new-app-by-form");
const search_apps_1 = require("./search-apps");
const update_git_config_1 = require("./update-git-config");
async function createOrSelectApp(projectSlug, options, question) {
const { DB } = await Promise.resolve().then(() => __importStar(require("../../modules/api/DB")));
const { action } = await inquirer_1.default.prompt({
type: "list",
name: "action",
message: question || `Create new or select an existing app?`,
choices: [
{ name: "Create new app", value: "create" },
{ name: "Select existing app", value: "select" },
],
});
let app;
if (action === "select") {
// find/search projects
const apps = await (0, search_apps_1.searchApps)({ projectSlug });
if (!(0, lodash_1.isEmpty)(apps)) {
// display list to select:
const { selectedApp } = await inquirer_1.default.prompt({
type: "list",
name: "selectedApp",
message: `Select your app in "${projectSlug}" project:`,
choices: apps.map((_app, i) => {
return { name: `[${i + 1}] ${_app.name} (${_app.slug})`, value: _app };
}),
});
// [backward compatible <3.15.X] apps have no git provider id -> update one!
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 });
// [backward compatible <3.15.X] apps have no "public" field -> update them follows their gitProvider's "public" field
if (selectedApp.public !== options.git.public) {
selectedApp.public = options.git.public;
await DB.updateOne("app", { _id: selectedApp._id }, { public: selectedApp.public });
}
// select this app!
app = await DB.findOne("app", { _id: selectedApp._id }, { populate: ["project"] });
}
else {
app = await (0, new_app_by_form_1.createAppByForm)({ ...options, skipFramework: true });
}
}
else {
app = await (0, new_app_by_form_1.createAppByForm)({ ...options, skipFramework: true });
}
if (!app)
return;
options.app = app;
options.slug = app.slug;
options.name = app.name;
options.repoSlug = `${(0, slug_1.makeSlug)(projectSlug)}-${(0, slug_1.makeSlug)(options.name)}`.toLowerCase();
// If there are no git info of this app in database, try to fetch current git data:
if (!app.git) {
const gitInfo = await (0, plugins_1.getCurrentGitRepoData)(options.targetDirectory);
if (options.isDebugging)
console.log(`[CREATE_OR_SELECT_APP] try to fetch current git data :>>`, gitInfo);
if (!gitInfo)
throw new Error(`[CREATE_OR_SELECT_APP] This directory has no git remote integrated.`);
app = await (0, update_git_config_1.updateAppGitInfo)(app, {
provider: gitInfo.provider,
repoURL: gitInfo.repoURL,
repoSSH: gitInfo.repoSSH,
});
if (!app)
throw new Error(`[CREATE_OR_SELECT_APP] Failed to update new git info to this app (${options.slug} / ${projectSlug}).`);
}
options.repoSSH = app.git.repoSSH;
options.repoURL = app.git.repoURL;
options.gitProvider = app.git.provider;
options.repoURL = options.repoURL;
return app;
}
exports.createOrSelectApp = createOrSelectApp;