@veecode-platform/safira-cli
Version:
Generate a microservice project from your spec.
155 lines (154 loc) • 7.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const core_1 = require("@oclif/core");
const mockserver_install_1 = require("../../mockserver/mockserver-install");
const color_1 = tslib_1.__importDefault(require("@oclif/color"));
const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
const constants_1 = require("../../utils/commands/constants");
const cache_manager_1 = require("../../cache/cache-manager");
const file_system_utils_1 = require("../../utils/file-system-utils");
const exit_codes_1 = require("../../command/exit-codes");
const kong_utils_1 = require("../../kong/kong-utils");
const install_1 = tslib_1.__importDefault(require("../kong/install"));
const url_utils_1 = require("../../utils/url-utils");
class MockserverInstall extends core_1.Command {
async run() {
const { args, flags } = await this.parse(MockserverInstall);
let isKongInstalled = await kong_utils_1.KongUtils.isKongInstalled().then(isInstalled => isInstalled).catch(() => false);
if (!isKongInstalled) {
const answers = await inquirer_1.default.prompt([
{
type: "list",
name: "kong-install",
message: "Kong is not installed. Do you want to install it?",
choices: constants_1.yesNoList,
},
]);
if (answers["kong-install"] === "yes") {
await install_1.default.run([]);
isKongInstalled = true;
}
else {
console.log(color_1.default.bold.yellowBright("Please consider install Kong before installing a mockserver."));
}
}
console.log("Installing Mockserver...");
const domainCache = cache_manager_1.CacheManager.instance.getCache(cache_manager_1.CacheName.domain);
try {
const answers = await inquirer_1.default.prompt([
{
when: () => !flags.domain && domainCache.length > 0,
type: "list",
name: "domain-list",
message: "Type the Mockserver domain:",
choices: [...domainCache, { name: color_1.default.bold("Input other value."), value: "other" }],
},
{
when: answers => !flags.domain && (answers["domain-list"] === "other" || !answers["domain-list"]),
type: "input",
name: "domain-item",
message: "Type the Mockserver domain:",
default: "localhost",
},
{
when: !flags.secure,
type: "list",
name: "secure",
message: "Use HTTPS?",
choices: constants_1.yesNoList,
},
{
when: !flags.ssl,
type: "list",
name: "ssl",
message: "Do you will use SSL?",
choices: constants_1.yesNoList,
},
{
when: answers => !flags["crt-file"] && answers.ssl === "yes",
type: "input",
name: "crt-file",
message: "Type the path to the certificate file:",
default: "./certs/cert.crt",
validate: async (file) => file_system_utils_1.FileSystemUtils.exists(file) ? true : "Invalid certificate file",
},
{
when: answers => !flags["key-file"] && answers.ssl === "yes",
type: "input",
name: "key-file",
message: "Type the path to the key file:",
default: "./certs/key.key",
validate: async (keyFile) => file_system_utils_1.FileSystemUtils.exists(keyFile) ? true : "Invalid key file",
},
{
when: !flags["secure-admin"] && isKongInstalled,
type: "list",
name: "secure-admin",
message: "Would you like to secure mockserver cPanel using KongIngress?",
choices: constants_1.yesNoList,
},
{
when: flags.cors && isKongInstalled,
type: "list",
name: "configure-cors",
message: "Would you like to configure CORS for mockserver?",
choices: constants_1.yesNoList,
},
]);
let corsList = [];
if (answers["configure-cors"] === "yes") {
corsList = await this.collectCorsQuestions();
}
if (answers["domain-item"])
cache_manager_1.CacheManager.instance.addItem(cache_manager_1.CacheName.domain, answers["domain-item"]);
await mockserver_install_1.MockServerInstallImpl.instance.install({
domain: flags.domain || answers["domain-item"] || answers["domain-list"],
crtFile: flags["crt-file"] || answers["crt-file"],
keyFile: flags["key-file"] || answers["key-file"],
secure: (flags.secure || answers.secure) === "yes",
ssl: (flags.ssl || answers.ssl) === "yes",
secureCPanel: (flags["secure-admin"] || answers["secure-admin"]) === "yes",
corsHost: corsList,
});
}
catch (error) {
this.log(color_1.default.red(error.message));
process.exit(exit_codes_1.ExitCode.operationNotPermitted);
}
}
async collectCorsQuestions(inputs = []) {
const prompts = [
{
type: "input",
name: "cors-item",
message: "HTTP Address to add to CORS: ",
validate: (value) => url_utils_1.UrlUtils.validate(value) ? true : "Invalid URL",
},
{
type: "list",
name: "again",
message: "Would you like to add one more? ",
choices: constants_1.yesNoList,
},
];
const { again, ...answers } = await inquirer_1.default.prompt(prompts);
const newInputs = [...inputs, answers["cors-item"]];
return again === "yes" ? this.collectCorsQuestions(newInputs) : newInputs;
}
}
exports.default = MockserverInstall;
MockserverInstall.description = "Install a mockserver";
MockserverInstall.examples = [
"<%= config.bin %> <%= command.id %>",
];
MockserverInstall.flags = {
domain: core_1.Flags.string({ description: "Specifies the domain to use in the application" }),
secure: core_1.Flags.string({ description: "Specifies if the application will have HTTPS", options: constants_1.yesNoList }),
ssl: core_1.Flags.string({ description: "Specifies if the application will have HTTPS", options: constants_1.yesNoList }),
"crt-file": core_1.Flags.string({ description: "Specifies the mockserver .crt file path" }),
"key-file": core_1.Flags.string({ description: "Specifies the mockserver .key file path" }),
"secure-admin": core_1.Flags.string({ description: "Specifies if CPanel must be secured with apikey", options: constants_1.yesNoList }),
cors: core_1.Flags.string({ description: "List of hosts to configure Cors" }),
};
MockserverInstall.args = [];