@topgroup/diginext
Version:
A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.
217 lines (216 loc) • 9.86 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.createAppByForm = void 0;
const log_1 = require("diginext-utils/dist/xconsole/log");
const inquirer_1 = __importDefault(require("inquirer"));
const lodash_1 = require("lodash");
const plugins_1 = require("../../plugins");
const slug_1 = require("../../plugins/slug");
const string_1 = require("../../plugins/string");
const ask_for_git_provider_1 = require("../git/ask-for-git-provider");
const git_utils_1 = require("../git/git-utils");
const create_or_select_project_1 = require("./create-or-select-project");
const update_config_1 = require("./update-config");
async function createAppByForm(options) {
var _a, _b;
if (!options.project)
options.project = await (0, create_or_select_project_1.createOrSelectProject)(options);
// console.log("options.project :>> ", options.project);
const { DB } = await Promise.resolve().then(() => __importStar(require("../../modules/api/DB")));
const { skipFramework } = options;
// if (options.isDebugging) console.log("createAppByForm() > options.framework :>> ", options.framework);
// if (options.isDebugging) console.log("createAppByForm() > skipFramework :>> ", skipFramework);
if (!options.name) {
const { name } = await inquirer_1.default.prompt({
type: "input",
name: "name",
message: "Enter your app name:",
validate: function (value) {
if (value.length < 3)
return "App name is required & has at least 3 characters.";
if ((0, string_1.containsSpecialCharacters)(value))
return `App name should not contain special characters.`;
return true;
},
});
// console.log("createAppByForm() > name :>> ", name);
options.name = name;
}
// git repo slug
options.repoSlug = `${options.project.slug}-${(0, slug_1.makeSlug)(options.name)}`.toLowerCase();
const noneFramework = { name: "None/unknown", slug: "none", version: "unknown", isPrivate: false };
// if "--framework" flag is defined...
let curFramework = options.framework && options.framework.slug !== "none" ? options.framework : noneFramework;
// can skip selecting framework if wanted (eg. when deploy existing app)
if (skipFramework)
options.framework = curFramework = noneFramework;
if (!options.framework) {
const frameworks = await DB.find("framework", {});
const selectFrameworks = [noneFramework];
if (!(0, lodash_1.isEmpty)(frameworks))
selectFrameworks.push(...frameworks);
// log({ selectFrameworks });
const { framework } = await inquirer_1.default.prompt({
type: "list",
name: "framework",
message: "Select starting framework:",
default: selectFrameworks[0],
choices: selectFrameworks.map((fw) => {
return { name: `${fw.name} ${fw.gitProvider ? `(${fw.gitProvider})` : ""}`, value: fw };
}),
});
curFramework = framework;
}
options.framework = curFramework;
if (options.isDebugging)
console.log("options.framework :>> ", options.framework);
// Check git provider authentication
let isFwPrivate = false;
let frameworkGitProvider;
let fwSlug = options.framework.slug;
let fwRepoSSH = "";
if (fwSlug !== "none") {
const { isPrivate, repoSSH } = options.framework;
isFwPrivate = isPrivate;
fwRepoSSH = repoSSH;
const { providerType: gitProvider } = (0, git_utils_1.parseGitRepoDataFromRepoSSH)(repoSSH);
frameworkGitProvider = gitProvider;
// Request select specific version
if (!options.frameworkVersion) {
const { frameworkVersion } = await inquirer_1.default.prompt({
type: "input",
name: "frameworkVersion",
message: `Framework version:`,
default: (curFramework === null || curFramework === void 0 ? void 0 : curFramework.mainBranch) || "main",
});
options.frameworkVersion = frameworkVersion;
}
}
else {
options.frameworkVersion = "unknown";
}
if (options.isDebugging)
console.log("options.frameworkVersion :>> ", options.frameworkVersion);
if (options.isDebugging)
console.log("options.git :>> ", options.git);
// select git provider for this app:
let gitProvider = options.git || (await (0, ask_for_git_provider_1.askForGitProvider)({ isDebugging: options.isDebugging }));
options.git = gitProvider;
options.gitProvider = gitProvider.type;
if (options.isDebugging)
(0, log_1.log)(`[CREATE APP BY FORM] git provider :>>`, gitProvider);
const currentGitData = options.shouldCreate ? undefined : await (0, plugins_1.getCurrentGitRepoData)(options.targetDirectory);
if (options.isDebugging)
(0, log_1.log)(`[CREATE APP BY FORM] current git data :>>`, currentGitData);
if (currentGitData) {
options.gitProvider = currentGitData.provider;
options.repoSSH = currentGitData.repoSSH;
options.repoURL = currentGitData.repoURL;
}
else {
// Create new repo:
const repoData = {
name: options.repoSlug,
private: !options.isPublic,
};
// ![DANGER] if "--force" was declared, try to delete if the repo was existed
if (options.overwrite) {
try {
await DB.delete("git_repo", { slug: gitProvider.slug }, { name: options.repoSlug }, {
subpath: "/orgs/repos",
ignorable: true,
});
}
catch (e) { }
}
if (options.isDebugging)
console.log("[newAppByForm] CREATE REPO > repoData :>> ", repoData);
const newRepo = await DB.create("git_repo", repoData, {
subpath: "/orgs/repos",
filter: { slug: gitProvider.slug },
isDebugging: options === null || options === void 0 ? void 0 : options.isDebugging,
});
if (options.isDebugging)
console.log("[newAppByForm] CREATE REPO > newRepo :>> ", newRepo);
if (!newRepo)
throw new Error(`Unable to create new ${gitProvider.type} repository.`);
options.gitProvider = newRepo.provider;
options.repoSSH = newRepo.ssh_url;
options.repoURL = newRepo.repo_url;
}
// Call API to create new app
const appData = {
name: options.name,
public: options.git.public,
project: options.project._id,
framework: {
name: options.framework.name,
slug: options.framework.slug,
repoSSH: options.framework.repoSSH,
repoURL: options.framework.repoURL,
version: options.frameworkVersion,
},
git: currentGitData
? { repoSSH: currentGitData.repoSSH, provider: currentGitData.provider, repoURL: currentGitData.repoURL }
: {},
environment: {},
deployEnvironment: {},
gitProvider: (_a = options.git) === null || _a === void 0 ? void 0 : _a._id,
// ownership
owner: options.userId,
ownerSlug: options.user,
workspace: options.workspaceId,
// FIXME: I don't know why "options.workspace" is undefined when uploading files to cloud storage
workspaceSlug: ((_b = options.workspace) === null || _b === void 0 ? void 0 : _b.slug) || options.project.workspaceSlug,
};
appData.git.provider = options.gitProvider;
if (options.repoSSH)
appData.git.repoSSH = options.repoSSH;
if (options.repoURL)
appData.git.repoURL = options.repoURL;
if (options.isDebugging)
(0, log_1.log)(`Create new app with data:`, appData);
const newApp = await DB.create("app", appData, { populate: ["project"], isDebugging: options.isDebugging });
if (options.isDebugging)
(0, log_1.log)({ newApp });
if ((0, lodash_1.isEmpty)(newApp) || newApp.error) {
(0, log_1.logError)(`[CREATE APP BY FORM] Can't create new app due to network issue.`);
return;
}
// to make sure it write down the correct app "slug" in app config
options.app = newApp;
options.slug = newApp.slug;
options.name = newApp.name;
// update existing app config if any
let appConfig = await (0, update_config_1.updateAppConfig)(newApp);
if (options.isDebugging)
console.log("createAppByForm() > appConfig :>> ", appConfig);
return newApp;
}
exports.createAppByForm = createAppByForm;