@veecode-platform/safira-cli
Version:
Generate a microservice project from your spec.
184 lines (183 loc) • 10.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const color_1 = tslib_1.__importDefault(require("@oclif/color"));
const core_1 = require("@oclif/core");
const veecode_devportal_operations_1 = require("../../vkpr/veecode-devportal-operations");
const git_providers_1 = require("../../git/git-providers");
const constants_1 = require("../../utils/commands/constants");
const exit_codes_1 = require("../../command/exit-codes");
const inquirer_1 = tslib_1.__importDefault(require("inquirer"));
const cache_manager_1 = require("../../cache/cache-manager");
const credentials_1 = require("../../vkpr/credentials/credentials");
const git_utils_1 = require("../../git/git-utils");
const git_commands_1 = require("../../git/git-commands");
const credentials_dto_1 = require("../../vkpr/credentials/credentials-dto");
const okta_utils_1 = require("../../okta/okta-utils");
const spec_house_utils_1 = require("../../spec-house/spec-house-utils");
const configure_catalog_info_1 = require("../../devportal/configure-catalog-info");
class DevportalInstall extends core_1.Command {
async run() {
const { args, flags } = await this.parse(DevportalInstall);
console.log("Installing Devportal...");
let repo = null;
try {
repo = await git_utils_1.GitUtils.getRepositoryData(await git_commands_1.GitCommand.instance.getRemoteOriginUrl());
}
catch { }
let currentBranch = "develop";
try {
currentBranch = await git_commands_1.GitCommand.instance.getCurrentBranch();
}
catch { }
const domainCache = cache_manager_1.CacheManager.instance.getCache(cache_manager_1.CacheName.domain);
const specHouseRepo = cache_manager_1.CacheManager.instance.getCache(cache_manager_1.CacheName.specHouseRepository);
const specHouseBranch = cache_manager_1.CacheManager.instance.getCache(cache_manager_1.CacheName.specHouseBranch);
const oktaIdCache = cache_manager_1.CacheManager.instance.getCache(cache_manager_1.CacheName.oktaId);
const hasOktaCredential = credentials_1.Credentials.instance.exists(credentials_1.CredentialsKey.okta);
const answers = await inquirer_1.default.prompt([
{
when: () => !flags.domain && domainCache.length > 0,
type: "list",
name: "domain-list",
message: "Type the Devportal 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 Devportal domain:",
default: "localhost",
},
{
when: !flags.secure,
type: "list",
name: "secure",
message: "Use HTTPS?",
choices: constants_1.yesNoList,
default: "yes",
},
{
when: !flags["okta-client-id"] && !hasOktaCredential,
type: "input",
name: "okta-client-id",
message: "Type the Okta client id:",
},
{
when: !flags["okta-client-secret"] && !hasOktaCredential,
type: "input",
name: "okta-client-secret",
message: "Type the Okta client secret:",
},
{
when: !flags["okta-id"] && oktaIdCache.length > 0,
type: "list",
name: "okta-id-list",
message: "Type the Okta Account ID:",
choices: [...oktaIdCache, { name: "Input other value", value: "other" }],
},
{
when: answers => !flags["okta-id"] && (oktaIdCache.length === 0 || answers["okta-id-list"] === "other"),
type: "input",
name: "okta-id-item",
message: "Type the Okta Account ID:",
validate: (value) => {
return Number.isInteger(Number.parseInt(value)) ? true : "Invalid Okta Account ID";
},
},
{
type: "list",
name: "git-provider",
message: "Select the Spec House Git provider:",
choices: [git_providers_1.GitProviderEnum.github],
default: repo?.provider || git_providers_1.GitProviderEnum.github,
},
{
when: specHouseRepo.length > 0,
type: "list",
name: "spec-house-list",
message: "Select the spec house repository:",
choices: [...specHouseRepo, { name: "Input other value", value: "other" }],
},
{
when: answers => specHouseRepo.length === 0 || answers["spec-house-list"] === "other",
type: "input",
name: "spec-house-repo",
message: "Input your Spec House repository(owner/project)",
default: `${repo?.owner || "owner"}/spec-house`,
validate: (value) => {
const split = value.split("/");
if (value.length >= 2 && value.includes("/") && (split[0].length > 0 && split[1].length > 0))
return true;
return "Please enter a valid project name with owner/project format";
},
},
{
when: !flags["spec-house-branch"] && specHouseBranch.length > 0,
type: "list",
name: "spec-house-repo-branch-list",
message: answers => `Type the branch name to publish spec for ${answers["spec-house-repo"] || answers["spec-house-list"]}`,
choices: [...specHouseBranch, { name: "Input other value", value: "other" }],
},
{
when: answers => !flags["spec-house-branch"] && (specHouseBranch.length === 0 || answers["spec-house-repo-branch-list"] === "other"),
type: "input",
name: "spec-house-repo-branch-name",
message: answers => `Type the branch name to publish spec for ${answers["spec-house-repo"] || answers["spec-house-list"]}`,
default: currentBranch,
validate: (value) => value.length > 0 ? true : "Please enter a valid branch name",
},
]);
const gitProvider = git_providers_1.GitProviderEnum[answers["git-provider"] || flags["spec-house-provider"]];
const oktaAudience = okta_utils_1.OktaUtils.instance.buildOktaAudience(Number(flags["okta-id"] || answers["okta-id-item"] || answers["okta-id-list"]));
const specHouseOwnerRepo = flags["spec-house-owner-repo"] || answers["spec-house-repo"] || answers["spec-house-list"];
const specHouseBranchSelect = flags["spec-house-branch"] || answers["spec-house-repo-branch-name"] || answers["spec-house-repo-branch-list"];
const specHouseUrl = spec_house_utils_1.SpecHouseUtils.instance.buildSpecHouseUrl(gitProvider, specHouseOwnerRepo, specHouseBranchSelect);
if (answers["domain-item"])
cache_manager_1.CacheManager.instance.addItem(cache_manager_1.CacheName.domain, answers["domain-item"]);
if (answers["okta-id-item"])
cache_manager_1.CacheManager.instance.addItem(cache_manager_1.CacheName.oktaId, answers["okta-id-item"]);
if (answers["spec-house-repo"])
cache_manager_1.CacheManager.instance.addItem(cache_manager_1.CacheName.specHouseRepository, answers["spec-house-repo"]);
if (answers["spec-house-repo-branch-name"])
cache_manager_1.CacheManager.instance.addItem(cache_manager_1.CacheName.specHouseBranch, answers["spec-house-repo-branch-name"]);
if (!hasOktaCredential)
await credentials_1.Credentials.instance.setCredential(new credentials_dto_1.OktaCredential(oktaAudience, answers["okta-client-id"], answers["okta-client-secret"]));
if (!credentials_1.Credentials.instance.exists(credentials_1.CredentialsKey.github)) {
await git_utils_1.GitUtils.askByGitCredentials(gitProvider);
}
const params = {
domain: answers["domain-item"] || flags.domain || answers["domain-list"],
secure: (answers.secure || flags.secure) === "yes",
oktaClientId: answers["okta-client-id"] || flags["okta-client-id"],
oktaClientSecret: answers["okta-client-secret"] || flags["okta-client-secret"],
oktaClientAudience: answers["okta-audience"] || flags["okta-audience"],
specHouseUrl: specHouseUrl,
};
try {
await configure_catalog_info_1.ConfigureCatalogInfo.instance.setupCatalogInfo(gitProvider, credentials_1.Credentials.instance.getCredential(gitProvider).credential, specHouseOwnerRepo, specHouseBranchSelect);
await veecode_devportal_operations_1.VeecodeDevportalOperations.instance.install(params);
}
catch (error) {
this.log(color_1.default.red(error.message));
process.exit(exit_codes_1.ExitCode.operationNotPermitted);
}
}
}
exports.default = DevportalInstall;
DevportalInstall.description = "Install the Devportal";
DevportalInstall.examples = [
"<%= config.bin %> <%= command.id %>",
];
DevportalInstall.flags = {
domain: core_1.Flags.string({ description: "domain" }),
secure: core_1.Flags.string({ description: "secure", options: constants_1.yesNoList }),
"okta-client-id": core_1.Flags.string({ description: "okta clientid" }),
"okta-client-secret": core_1.Flags.string({ description: "okta clientsecret" }),
"okta-id": core_1.Flags.string({ description: "okta account id" }),
"spec-house-branch": core_1.Flags.string({ description: "branch to listen specs" }),
"spec-house-provider": core_1.Flags.string({ description: "spec house provider", options: [git_providers_1.GitProviderEnum.github] }),
"spec-house-owner-repo": core_1.Flags.string({ description: "owner/repo" }),
};
DevportalInstall.args = [];