From cba319bc3de595cdcf836ec97e68abbb24d42023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ali=20Tu=C4=9Frul=20P=C4=B1nar?= Date: Wed, 17 Jan 2024 16:00:53 +0300 Subject: [PATCH] feat(core): BE-2553 add commander for help command --- package.json | 2 +- src/command.ts | 455 +++++++++++++++++++++++++ src/constant.ts | 3 + src/main.ts | 884 +++++++++++++----------------------------------- src/program.ts | 33 ++ src/services.ts | 14 +- yarn.lock | 8 +- 7 files changed, 742 insertions(+), 657 deletions(-) create mode 100644 src/command.ts create mode 100644 src/constant.ts create mode 100644 src/program.ts diff --git a/package.json b/package.json index 8ced931..0badc54 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,7 @@ "dependencies": { "axios": "^0.27.1", "chalk": "^4.1.1", - "commander": "^7.1.0", + "commander": "^11.1.0", "enquirer": "^2.3.6", "esm": "^3.2.25", "form-data": "^4.0.0", diff --git a/src/command.ts b/src/command.ts new file mode 100644 index 0000000..8b1fc45 --- /dev/null +++ b/src/command.ts @@ -0,0 +1,455 @@ +import chalk from "chalk"; +import { APPCIRCLE_COLOR } from "./constant"; + +export const CommandParameterTypes = { + SELECT: "select", + BOOLEAN: "boolean", + STRING: "input", + PASSWORD: "password", +}; + +export const CommandTypes = { + LOGIN: "login", + LIST_BUILD_PROFILES: "listBuildProfiles", + LIST_BUILD_PROFILE_BRANCHES: "listBuildProfileBranches", + LIST_BUILD_PROFILE_WORKFLOWS: "listBuildProfileWorkflows", + LIST_BUILD_PROFILE_COMMITS: "listBuildProfileCommits", + LIST_BUILD_PROFILE_BUILDS_OF_COMMIT: "listBuildProfileBuildsOfCommit", + LIST_DISTRIBUTION_PROFILES: "listDistributionProfiles", + BUILD: "build", + DOWNLOAD: "download", + UPLOAD: "upload", + CREATE_DISTRIBUTION_PROFILE: "createDistributionProfile", + LIST_ENVIRONMENT_VARIABLE_GROUPS: "listEnvironmentVariableGroups", + CREATE_ENVIRONMENT_VARIABLE_GROUP: "createEnvironmentVariableGroup", + LIST_ENVIRONMENT_VARIABLES: "listEnvironmentVariables", + CREATE_ENVIRONMENT_VARIABLE: "createEnvironmentVariable", + LIST_ENTERPRISE_PROFILES: "listEnterpriseProfiles", + LIST_ENTERPRISE_APP_VERSIONS: "listEnterpriseAppVersions", + PUBLISH_ENTERPRISE_APP_VERSION: "publishEnterpriseAppVersion", + UNPUBLISH_ENTERPRISE_APP_VERSION: "unpublishEnterpriseAppVersion", + REMOVE_ENTERPRISE_APP_VERSION: "removeEnterpriseAppVersion", + NOTIFY_ENTERPRISE_APP_VERSION: "notifyEnterpriseAppVersion", + UPLOAD_ENTERPRISE_APP: "uploadEnterpriseApp", + UPLOAD_ENTERPRISE_APP_VERSION: "uploadEnterpriseAppVersion", + GET_ENTERPRISE_DOWNLOAD_LINK: "getEnterpriseDownloadLink", +}; + +export const Commands: { + command: string; + description: string; + params: { name: string; description: string; type: string; valueType: string; required?: boolean; params?: any }[]; +}[] = [ + { + command: CommandTypes.LOGIN, + description: "Login", + params: [ + { + name: "pat", + description: "Personal Access Token", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + ], + }, + { + command: CommandTypes.LIST_BUILD_PROFILES, + description: "Get list of build profiles", + params: [], + }, + { + command: CommandTypes.LIST_BUILD_PROFILE_BRANCHES, + description: "Get list of branches of a build profile", + params: [ + { + name: "profileId", + description: "Build profile ID", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + ], + }, + { + command: CommandTypes.LIST_BUILD_PROFILE_WORKFLOWS, + description: "Get list of workflows of a build profile", + params: [ + { + name: "profileId", + description: "Build profile ID", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + ], + }, + { + command: CommandTypes.LIST_BUILD_PROFILE_COMMITS, + description: "Get list of commits of a branch", + params: [ + { + name: "branchId", + description: "Branch ID", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + ], + }, + { + command: CommandTypes.LIST_BUILD_PROFILE_BUILDS_OF_COMMIT, + description: "Get list of builds of a commit", + params: [ + { + name: "commitId", + description: "Commit ID", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + ], + }, + { + command: CommandTypes.LIST_DISTRIBUTION_PROFILES, + description: "Get list of distribution profiles", + params: [], + }, + { + command: CommandTypes.BUILD, + description: "Start a new build", + params: [ + { + name: "profileId", + description: "Build profile ID", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + { + name: "branch", + description: "Branch", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + params: [], + }, + { + name: "workflow", + description: "Workflow", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + params: [], + }, + ], + }, + { + command: CommandTypes.DOWNLOAD, + description: "Download your artifact to the given directory on your machine", + params: [ + { + name: "path", + description: "[OPTIONAL] The path for artifacts to be downloaded: (Defaults to the current directory)", + type: CommandParameterTypes.STRING, + valueType: "string", + required: false, + }, + { + name: "commitId", + description: "Commit ID of your build", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + { + name: "buildId", + description: "Build ID of your commit. This can be retrieved from builds of commit", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + ], + }, + { + command: CommandTypes.UPLOAD, + description: "Upload your mobile app to selected distribution profile", + params: [ + { + name: "app", + description: "App path", + type: CommandParameterTypes.STRING, + valueType: "path", + }, + { + name: "message", + description: "Release notes", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + { + name: "profileId", + description: "Distribution profile ID", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + ], + }, + { + command: CommandTypes.CREATE_DISTRIBUTION_PROFILE, + description: "Create a distribution profile", + params: [ + { + name: "name", + description: "Profile name", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + ], + }, + { + command: CommandTypes.LIST_ENVIRONMENT_VARIABLE_GROUPS, + description: "Get list of environment variable groups", + params: [], + }, + { + command: CommandTypes.CREATE_ENVIRONMENT_VARIABLE_GROUP, + description: "Create an environment variable group", + params: [ + { + name: "name", + description: "Variable group name", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + ], + }, + { + command: CommandTypes.LIST_ENVIRONMENT_VARIABLES, + description: "Get list of environment variables", + params: [ + { + name: "variableGroupId", + description: "Variable Groups ID", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + ], + }, + { + command: CommandTypes.CREATE_ENVIRONMENT_VARIABLE, + description: "Create a file or text environment variable", + params: [ + { + name: "type", + description: "Type", + type: CommandParameterTypes.SELECT, + valueType: "string", + params: [ + { + name: "file", + description: "File", + }, + { + name: "text", + description: "Text", + }, + ], + }, + { + name: "isSecret", + description: "Secret", + type: CommandParameterTypes.BOOLEAN, + valueType: "boolean", + }, + { + name: "variableGroupId", + description: "Variable group ID", + type: CommandParameterTypes.STRING, + valueType: "uuid", + }, + { + name: "key", + description: "Key Name", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + { + name: "value", + description: `Key Value (You can skip this if you selected type of ${chalk.hex(APPCIRCLE_COLOR)("file")})`, + type: CommandParameterTypes.STRING, + valueType: "string", + }, + { + name: "filePath", + description: `File path (You can skip this if you selected type of ${chalk.hex(APPCIRCLE_COLOR)("text")})`, + type: CommandParameterTypes.STRING, + valueType: "string", + }, + ], + }, + { + command: CommandTypes.LIST_ENTERPRISE_PROFILES, + description: "Get list of enterprise profiles", + params: [], + }, + { + command: CommandTypes.LIST_ENTERPRISE_APP_VERSIONS, + description: "Get list of enterprise app versions", + params: [ + { + name: "entProfileId", + description: "Enterprise Profile ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + { + name: "publishType", + description: "[OPTIONAL] Publish Type Empty,0=All,1=Beta,2=Live", + type: CommandParameterTypes.STRING, + required: false, + valueType: "number", + }, + ], + }, + { + command: CommandTypes.PUBLISH_ENTERPRISE_APP_VERSION, + description: "Publish enterprise app version", + params: [ + { + name: "entProfileId", + description: "Enterprise Profile ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + { + name: "entVersionId", + description: "App Version ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + { + name: "summary", + description: "Summary", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + { + name: "releaseNotes", + description: "Release Notes", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + { + name: "publishType", + description: "Publish Type 0=None,1=Beta,2=Live", + type: CommandParameterTypes.STRING, + valueType: "number", + }, + ], + }, + { + command: CommandTypes.UNPUBLISH_ENTERPRISE_APP_VERSION, + description: "Unpublish enterprise app version", + params: [ + { + name: "entProfileId", + description: "Enterprise Profile ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + { + name: "entVersionId", + description: "App Version ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + ], + }, + { + command: CommandTypes.REMOVE_ENTERPRISE_APP_VERSION, + description: "Remove enterprise app version", + params: [ + { + name: "entProfileId", + description: "Enterprise Profile ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + { + name: "entVersionId", + description: "App Version ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + ], + }, + { + command: CommandTypes.NOTIFY_ENTERPRISE_APP_VERSION, + description: "Notify enterprise app version", + params: [ + { + name: "entProfileId", + description: "Enterprise Profile ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + { + name: "entVersionId", + description: "App Version ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + { + name: "subject", + description: "Subject", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + { + name: "message", + description: "Message", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + ], + }, + { + command: CommandTypes.UPLOAD_ENTERPRISE_APP_VERSION, + description: "Upload enterprise app version for a profile", + params: [ + { + name: "entProfileId", + description: "Enterprise Profile Id", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + { + name: "app", + description: "App path", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + ], + }, + { + command: CommandTypes.UPLOAD_ENTERPRISE_APP, + description: "Upload enterprise app version without a profile", + params: [ + { + name: "app", + description: "App path", + type: CommandParameterTypes.STRING, + valueType: "string", + }, + ], + }, + { + command: CommandTypes.GET_ENTERPRISE_DOWNLOAD_LINK, + description: "Get enterprise app download link", + params: [ + { + name: "entProfileId", + description: "Enterprise Profile Id", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + { + name: "entVersionId", + description: "App Version ID", + type: CommandParameterTypes.SELECT, + valueType: "uuid", + }, + ], + }, +]; diff --git a/src/constant.ts b/src/constant.ts new file mode 100644 index 0000000..2160928 --- /dev/null +++ b/src/constant.ts @@ -0,0 +1,3 @@ +export const APPCIRCLE_COLOR = "#ff8F34"; +export const API_HOSTNAME = process.env.API_HOSTNAME || "https://api.appcircle.io"; +export const AUTH_HOSTNAME = process.env.AUTH_HOSTNAME || "https://auth.appcircle.io"; diff --git a/src/main.ts b/src/main.ts index bf815af..4c1f6f7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,457 +1,132 @@ //@ts-ignore https://github.com/enquirer/enquirer/issues/212 -import { prompt, Select, BooleanPrompt } from 'enquirer'; -import ora from 'ora'; -import chalk from 'chalk'; -import fs from 'fs'; -import minimist from 'minimist'; +import { prompt, Select, BooleanPrompt } from "enquirer"; +import ora from "ora"; +import chalk from "chalk"; +import fs from "fs"; +import minimist from "minimist"; import { - getToken, - getDistributionProfiles, - createDistributionProfile, - downloadArtifact, - uploadArtifact, - getBuildProfiles, - startBuild, - getEnvironmentVariableGroups, - createEnvironmentVariableGroup, - getEnvironmentVariables, - createEnvironmentVariable, - getBranches, - getWorkflows, - getCommits, - getBuildsOfCommit, - getEnterpriseProfiles, - getEnterpriseAppVersions, - publishEnterpriseAppVersion, - unpublishEnterpriseAppVersion, - removeEnterpriseAppVersion, - notifyEnterpriseAppVersion, - uploadEnterpriseApp, - uploadEnterpriseAppVersion, - getEnterpriseDownloadLink -} from './services'; - -import { readVariable, EnvironmentVariables } from './data'; - -const APPCIRCLE_COLOR = '#ff8F34'; -const CommandParameterTypes = { - SELECT: 'select', - BOOLEAN: 'boolean', - STRING: 'input', - PASSWORD: 'password' -}; - -const CommandTypes = { - LOGIN: 'login', - LIST_BUILD_PROFILES: 'listBuildProfiles', - LIST_BUILD_PROFILE_BRANCHES: 'listBuildProfileBranches', - LIST_BUILD_PROFILE_WORKFLOWS: 'listBuildProfileWorkflows', - LIST_BUILD_PROFILE_COMMITS: 'listBuildProfileCommits', - LIST_BUILD_PROFILE_BUILDS_OF_COMMIT: 'listBuildProfileBuildsOfCommit', - LIST_DISTRIBUTION_PROFILES: 'listDistributionProfiles', - BUILD: 'build', - DOWNLOAD: 'download', - UPLOAD: 'upload', - CREATE_DISTRIBUTION_PROFILE: 'createDistributionProfile', - LIST_ENVIRONMENT_VARIABLE_GROUPS: 'listEnvironmentVariableGroups', - CREATE_ENVIRONMENT_VARIABLE_GROUP: 'createEnvironmentVariableGroup', - LIST_ENVIRONMENT_VARIABLES: 'listEnvironmentVariables', - CREATE_ENVIRONMENT_VARIABLE: 'createEnvironmentVariable', - LIST_ENTERPRISE_PROFILES: 'listEnterpriseProfiles', - LIST_ENTERPRISE_APP_VERSIONS: 'listEnterpriseAppVersions', - PUBLISH_ENTERPRISE_APP_VERSION: 'publishEnterpriseAppVersion', - UNPUBLISH_ENTERPRISE_APP_VERSION: 'unpublishEnterpriseAppVersion', - REMOVE_ENTERPRISE_APP_VERSION: 'removeEnterpriseAppVersion', - NOTIFY_ENTERPRISE_APP_VERSION: 'notifyEnterpriseAppVersion', - UPLOAD_ENTERPRISE_APP: 'uploadEnterpriseApp', - UPLOAD_ENTERPRISE_APP_VERSION: 'uploadEnterpriseAppVersion', - GET_ENTERPRISE_DOWNLOAD_LINK: 'getEnterpriseDownloadLink' + getToken, + getDistributionProfiles, + createDistributionProfile, + downloadArtifact, + uploadArtifact, + getBuildProfiles, + startBuild, + getEnvironmentVariableGroups, + createEnvironmentVariableGroup, + getEnvironmentVariables, + createEnvironmentVariable, + getBranches, + getWorkflows, + getCommits, + getBuildsOfCommit, + getEnterpriseProfiles, + getEnterpriseAppVersions, + publishEnterpriseAppVersion, + unpublishEnterpriseAppVersion, + removeEnterpriseAppVersion, + notifyEnterpriseAppVersion, + uploadEnterpriseApp, + uploadEnterpriseAppVersion, + getEnterpriseDownloadLink, +} from "./services"; + +import { CommandParameterTypes, CommandTypes, Commands } from "./command"; +import { APPCIRCLE_COLOR } from "./constant"; +import { ProgramCommand, createProgram } from "./program"; + +const runCommandHelper = (command: ProgramCommand) => { + const params = command.opts() as any; + switch (command.name()) { + case CommandTypes.LOGIN: + getToken(params); + break; + case CommandTypes.LIST_BUILD_PROFILES: + getBuildProfiles(params); + break; + case CommandTypes.LIST_BUILD_PROFILE_BRANCHES: + getBranches(params); + break; + case CommandTypes.LIST_BUILD_PROFILE_WORKFLOWS: + getWorkflows(params); + break; + case CommandTypes.LIST_BUILD_PROFILE_COMMITS: + getCommits(params); + break; + case CommandTypes.LIST_BUILD_PROFILE_BUILDS_OF_COMMIT: + getBuildsOfCommit(params); + break; + case CommandTypes.LIST_DISTRIBUTION_PROFILES: + getDistributionProfiles(params); + break; + case CommandTypes.BUILD: + startBuild(params); + break; + case CommandTypes.DOWNLOAD: + downloadArtifact(params); + break; + case CommandTypes.UPLOAD: + uploadArtifact(params); + break; + case CommandTypes.CREATE_DISTRIBUTION_PROFILE: + createDistributionProfile(params); + break; + case CommandTypes.LIST_ENVIRONMENT_VARIABLE_GROUPS: + getEnvironmentVariableGroups(params); + break; + case CommandTypes.CREATE_ENVIRONMENT_VARIABLE_GROUP: + createEnvironmentVariableGroup(params); + break; + case CommandTypes.LIST_ENVIRONMENT_VARIABLES: + getEnvironmentVariables(params); + break; + case CommandTypes.CREATE_ENVIRONMENT_VARIABLE: + createEnvironmentVariable(params as any); + break; + case CommandTypes.LIST_ENTERPRISE_PROFILES: + getEnterpriseProfiles(); + break; + case CommandTypes.LIST_ENTERPRISE_APP_VERSIONS: + getEnterpriseAppVersions(params); + break; + case CommandTypes.PUBLISH_ENTERPRISE_APP_VERSION: + publishEnterpriseAppVersion(params); + break; + case CommandTypes.UNPUBLISH_ENTERPRISE_APP_VERSION: + unpublishEnterpriseAppVersion(params); + break; + case CommandTypes.REMOVE_ENTERPRISE_APP_VERSION: + removeEnterpriseAppVersion(params); + break; + case CommandTypes.NOTIFY_ENTERPRISE_APP_VERSION: + notifyEnterpriseAppVersion(params); + break; + case CommandTypes.UPLOAD_ENTERPRISE_APP: + uploadEnterpriseApp(params); + break; + case CommandTypes.UPLOAD_ENTERPRISE_APP_VERSION: + uploadEnterpriseAppVersion(params); + break; + case CommandTypes.GET_ENTERPRISE_DOWNLOAD_LINK: + getEnterpriseDownloadLink(params); + break; + default: + console.error("Command not found"); + break; + } }; -const Commands = [ - { - command: CommandTypes.LOGIN, - description: 'Login', - params: [ - { - name: 'pat', - description: 'Personal Access Token', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.LIST_BUILD_PROFILES, - description: 'Get list of build profiles', - params: [] - }, - { - command: CommandTypes.LIST_BUILD_PROFILE_BRANCHES, - description: 'Get list of branches of a build profile', - params: [ - { - name: 'profileId', - description: 'Build profile ID', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.LIST_BUILD_PROFILE_WORKFLOWS, - description: 'Get list of workflows of a build profile', - params: [ - { - name: 'profileId', - description: 'Build profile ID', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.LIST_BUILD_PROFILE_COMMITS, - description: 'Get list of commits of a branch', - params: [ - { - name: 'branchId', - description: 'Branch ID', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.LIST_BUILD_PROFILE_BUILDS_OF_COMMIT, - description: 'Get list of builds of a commit', - params: [ - { - name: 'commitId', - description: 'Commit ID', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.LIST_DISTRIBUTION_PROFILES, - description: 'Get list of distribution profiles', - params: [] - }, - { - command: CommandTypes.BUILD, - description: 'Start a new build', - params: [ - { - name: 'profileId', - description: 'Build profile ID', - type: CommandParameterTypes.STRING - }, - { - name: 'branch', - description: 'Branch', - type: CommandParameterTypes.SELECT, - params: [] - }, - { - name: 'workflow', - description: 'Workflow', - type: CommandParameterTypes.SELECT, - params: [] - } - ] - }, - { - command: CommandTypes.DOWNLOAD, - description: 'Download your artifact to the given directory on your machine', - params: [ - { - name: 'path', - description: '[OPTIONAL] The path for artifacts to be downloaded: (Defaults to the current directory)', - type: CommandParameterTypes.STRING, - required: false - }, - { - name: 'commitId', - description: 'Commit ID of your build', - type: CommandParameterTypes.STRING - }, - { - name: 'buildId', - description: 'Build ID of your commit. This can be retrieved from builds of commit', - type: CommandParameterTypes.STRING - }, - ] - }, - { - command: CommandTypes.UPLOAD, - description: 'Upload your mobile app to selected distribution profile', - params: [ - { - name: 'app', - description: 'App path', - type: CommandParameterTypes.STRING - }, - { - name: 'message', - description: 'Release notes', - type: CommandParameterTypes.STRING - }, - { - name: 'profileId', - description: 'Distribution profile ID', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.CREATE_DISTRIBUTION_PROFILE, - description: 'Create a distribution profile', - params: [ - { - name: 'name', - description: 'Profile name', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.LIST_ENVIRONMENT_VARIABLE_GROUPS, - description: 'Get list of environment variable groups', - params: [] - }, - { - command: CommandTypes.CREATE_ENVIRONMENT_VARIABLE_GROUP, - description: 'Create an environment variable group', - params: [ - { - name: 'name', - description: 'Variable group name', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.LIST_ENVIRONMENT_VARIABLES, - description: 'Get list of environment variables', - params: [ - { - name: 'variableGroupId', - description: 'Variable Groups ID', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.CREATE_ENVIRONMENT_VARIABLE, - description: 'Create a file or text environment variable', - params: [ - { - name: 'type', - description: 'Type', - type: CommandParameterTypes.SELECT, - params: [ - { - name: 'file', - description: 'File' - }, - { - name: 'text', - description: 'Text' - } - ] - }, - { - name: 'isSecret', - description: 'Secret', - type: CommandParameterTypes.BOOLEAN - }, - { - name: 'variableGroupId', - description: 'Variable group ID', - type: CommandParameterTypes.STRING - }, - { - name: 'key', - description: 'Key Name', - type: CommandParameterTypes.STRING - }, - { - name: 'value', - description: `Key Value (You can skip this if you selected type of ${chalk.hex(APPCIRCLE_COLOR)('file')})`, - type: CommandParameterTypes.STRING - }, - { - name: 'filePath', - description: `File path (You can skip this if you selected type of ${chalk.hex(APPCIRCLE_COLOR)('text')})`, - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.LIST_ENTERPRISE_PROFILES, - description: 'Get list of enterprise profiles', - params: [] - }, - { - command: CommandTypes.LIST_ENTERPRISE_APP_VERSIONS, - description: 'Get list of enterprise app versions', - params: [ - { - name: 'entProfileId', - description: 'Enterprise Profile ID', - type: CommandParameterTypes.SELECT - }, - { - name: 'publishType', - description: '[OPTIONAL] Publish Type Empty,0=All,1=Beta,2=Live', - type: CommandParameterTypes.STRING, - required: false - } - ] - }, - { - command: CommandTypes.PUBLISH_ENTERPRISE_APP_VERSION, - description: 'Publish enterprise app version', - params: [ - { - name: 'entProfileId', - description: 'Enterprise Profile ID', - type: CommandParameterTypes.SELECT - }, - { - name: 'entVersionId', - description: 'App Version ID', - type: CommandParameterTypes.SELECT - }, - { - name: 'summary', - description: 'Summary', - type: CommandParameterTypes.STRING - }, - { - name: 'releaseNotes', - description: 'Release Notes', - type: CommandParameterTypes.STRING - }, - { - name: 'publishType', - description: 'Publish Type 0=None,1=Beta,2=Live', - type: CommandParameterTypes.STRING - } - ] - }, - { - command: CommandTypes.UNPUBLISH_ENTERPRISE_APP_VERSION, - description: 'Unpublish enterprise app version', - params: [ - { - name: 'entProfileId', - description: 'Enterprise Profile ID', - type: CommandParameterTypes.SELECT - }, - { - name: 'entVersionId', - description: 'App Version ID', - type: CommandParameterTypes.SELECT - }, - ] - }, - { - command: CommandTypes.REMOVE_ENTERPRISE_APP_VERSION, - description: 'Remove enterprise app version', - params: [ - { - name: 'entProfileId', - description: 'Enterprise Profile ID', - type: CommandParameterTypes.SELECT - }, - { - name: 'entVersionId', - description: 'App Version ID', - type: CommandParameterTypes.SELECT - }, - ] - }, - { - command: CommandTypes.NOTIFY_ENTERPRISE_APP_VERSION, - description: 'Notify enterprise app version', - params: [ - { - name: 'entProfileId', - description: 'Enterprise Profile ID', - type: CommandParameterTypes.SELECT - }, - { - name: 'entVersionId', - description: 'App Version ID', - type: CommandParameterTypes.SELECT - }, - { - name: 'subject', - description: 'Subject', - type: CommandParameterTypes.STRING - }, - { - name: 'message', - description: 'Message', - type: CommandParameterTypes.STRING - }, - ] - }, - { - command: CommandTypes.UPLOAD_ENTERPRISE_APP_VERSION, - description: 'Upload enterprise app version for a profile', - params: [ - { - name: 'entProfileId', - description: 'Enterprise Profile Id', - type: CommandParameterTypes.SELECT, - }, - { - name: 'app', - description: 'App path', - type: CommandParameterTypes.STRING - }, - ] - }, - { - command: CommandTypes.UPLOAD_ENTERPRISE_APP, - description: 'Upload enterprise app version without a profile', - params: [ - { - name: 'app', - description: 'App path', - type: CommandParameterTypes.STRING - }, - ] - }, - { - command: CommandTypes.GET_ENTERPRISE_DOWNLOAD_LINK, - description: 'Get enterprise app download link', - params: [ - { - name: 'entProfileId', - description: 'Enterprise Profile Id', - type: CommandParameterTypes.SELECT, - }, - { - name: 'entVersionId', - description: 'App Version ID', - type: CommandParameterTypes.SELECT - }, - ] - }, -]; - - (async () => { - const accessToken = readVariable(EnvironmentVariables.AC_ACCESS_TOKEN); - let params: any = {}; - let selectedCommand: typeof Commands[number]; - let selectedCommandDescription = ''; - let selectedCommandIndex = -1; - - if (process.argv.length === 2) { - console.info( - chalk.hex(APPCIRCLE_COLOR)( - ` + const program = createProgram(); + const argv = minimist(process.argv.slice(2)); + let params: any = {}; + let selectedCommand: (typeof Commands)[number]; + let selectedCommandDescription = ""; + let selectedCommandIndex = -1; + if (process.argv.length === 2 || argv.i || argv.interactive) { + console.info( + chalk.hex(APPCIRCLE_COLOR)( + ` ███████ ██████╗ ██████╗ ██████╗██╗██████╗ ██████╗██╗ ███████╗ ██╔══██╗██╔══██╗██╔══██╗██╔════╝██║██╔══██╗██╔════╝██║ ██╔════╝ ███████║██████╔╝██████╔╝██║ ██║██████╔╝██║ ██║ █████╗ @@ -459,204 +134,119 @@ const Commands = [ ██║ ██║██║ ██║ ╚██████╗██║██║ ██║╚██████╗███████╗███████╗ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═════╝╚═╝╚═╝ ╚═╝ ╚═════╝╚══════╝╚══════╝ ` - ) - ); - - const commandSelect = new Select({ - name: 'command', - message: 'What do you want to do?', - choices: [ - ...Commands.map((command, index) => `${index + 1}. ${command.description}`) - ] - }); - - selectedCommandDescription = await commandSelect.run(); - selectedCommandIndex = (Number(selectedCommandDescription.split('.')[0]) - 1); - selectedCommand = Commands[selectedCommandIndex]; - - for (let param of selectedCommand.params) { - if (param.name === 'branch') { - const spinner = ora('Branches fetching').start(); - - const branches = await getBranches({ profileId: params.profileId || '' }); - if (!branches || branches.length === 0) { - spinner.text = 'No branches available'; - spinner.fail(); - return; - } - //@ts-ignore - param.params = branches.map((branch: any) => ({ name: branch.name, description: branch.name })); - - spinner.text = 'Branches fetched'; - spinner.succeed(); - } else if (param.name === 'entProfileId') { - const spinner = ora('Enterprise Profiles fetching').start(); - const profiles = await getEnterpriseProfiles(); - if (!profiles || profiles.length === 0) { - spinner.text = 'No enterprise profile available'; - spinner.fail(); - return; - } - //@ts-ignore - param.params = profiles.map((profile: any) => ({ name: profile.id, message: profile.name })); - spinner.text = 'Enterprise Profiles fetched'; - spinner.succeed(); - } else if (param.name === 'entVersionId') { - const spinner = ora('Enterprise Versions fetching').start(); - const profiles = await getEnterpriseAppVersions({entProfileId: params.entProfileId, publishType: ''}); - if (!profiles || profiles.length === 0) { - spinner.text = 'No version available'; - spinner.fail(); - return; - } - //@ts-ignore - param.params = profiles.map((profile: any) => ({ name: profile.id, message: `${profile.version} (${profile.versionCode})` })); - spinner.text = 'Enterprise Versions fetched'; - spinner.succeed(); - } - else if (param.name === 'workflow') { - const spinner = ora('Workflow fetching').start(); - const workflows = await getWorkflows({ profileId: params.profileId || '' }); - if (!workflows || workflows.length === 0) { - spinner.text = 'No workflows available'; - spinner.fail(); - return; - } - //@ts-ignore - param.params = workflows.map((workflow: any) => ({ name: workflow.workflowName, description: workflow.workflowName })); - spinner.text = 'Workflows fetched'; - spinner.succeed(); - } else if (param.name === 'value' && params.isSecret) { - param.type = CommandParameterTypes.PASSWORD; - } - - if ([CommandParameterTypes.STRING, CommandParameterTypes.PASSWORD].includes(param.type)) { - const stringPrompt = await prompt([ - { - type: param.type, - name: param.name, - message: param.description, - validate(value) { - //@ts-ignore - if (value.length === 0 && param.required !== false) { - return 'This field is required'; - } else if (['app'].includes(param.name)) { - return fs.existsSync(value) ? true : 'File not exists'; - } - return true; - } - } - ]); - (params as any)[param.name] = (stringPrompt as any)[Object.keys(stringPrompt)[0]]; - } else if (param.type === CommandParameterTypes.BOOLEAN) { - const booleanPrompt = new BooleanPrompt({ - name: param.name, - message: param.description, - }); - //@ts-ignore - params[param.name] = await booleanPrompt.run(); - } else if (param.type === CommandParameterTypes.SELECT) { - const selectPrompt = new Select({ - name: param.name, - message: param.description, - choices: [ - //@ts-ignore - ...param.params.map((val: any) => val) - ] - }); - (params as any)[param.name] = await selectPrompt.run(); - } + ) + ); + + const commandSelect = new Select({ + name: "command", + message: "What do you want to do?", + choices: [...Commands.map((command, index) => `${index + 1}. ${command.description}`)], + }); + + selectedCommandDescription = await commandSelect.run(); + selectedCommandIndex = Number(selectedCommandDescription.split(".")[0]) - 1; + selectedCommand = Commands[selectedCommandIndex]; + + for (let param of selectedCommand.params) { + if (param.name === "branch") { + const spinner = ora("Branches fetching").start(); + + const branches = await getBranches({ profileId: params.profileId || "" }); + if (!branches || branches.length === 0) { + spinner.text = "No branches available"; + spinner.fail(); + return; } - - console.info(''); - } else { - const argv = minimist(process.argv.slice(2)); - selectedCommandDescription = argv['_'][0]; - selectedCommand = Commands.find(x => x.command === selectedCommandDescription) || { command: '', description: '', params: [] }; - selectedCommandIndex = Commands.indexOf(selectedCommand); - - params = { - ...params, - ...argv - }; - delete (params as any)['_']; + //@ts-ignore + param.params = branches.map((branch: any) => ({ name: branch.name, description: branch.name })); + + spinner.text = "Branches fetched"; + spinner.succeed(); + } else if (param.name === "entProfileId") { + const spinner = ora("Enterprise Profiles fetching").start(); + const profiles = await getEnterpriseProfiles(); + if (!profiles || profiles.length === 0) { + spinner.text = "No enterprise profile available"; + spinner.fail(); + return; + } + //@ts-ignore + param.params = profiles.map((profile: any) => ({ name: profile.id, message: profile.name })); + spinner.text = "Enterprise Profiles fetched"; + spinner.succeed(); + } else if (param.name === "entVersionId") { + const spinner = ora("Enterprise Versions fetching").start(); + const profiles = await getEnterpriseAppVersions({ entProfileId: params.entProfileId, publishType: "" }); + if (!profiles || profiles.length === 0) { + spinner.text = "No version available"; + spinner.fail(); + return; + } + //@ts-ignore + param.params = profiles.map((profile: any) => ({ name: profile.id, message: `${profile.version} (${profile.versionCode})` })); + spinner.text = "Enterprise Versions fetched"; + spinner.succeed(); + } else if (param.name === "workflow") { + const spinner = ora("Workflow fetching").start(); + const workflows = await getWorkflows({ profileId: params.profileId || "" }); + if (!workflows || workflows.length === 0) { + spinner.text = "No workflows available"; + spinner.fail(); + return; + } + //@ts-ignore + param.params = workflows.map((workflow: any) => ({ name: workflow.workflowName, description: workflow.workflowName })); + spinner.text = "Workflows fetched"; + spinner.succeed(); + } else if (param.name === "value" && params.isSecret) { + param.type = CommandParameterTypes.PASSWORD; + } + + if ([CommandParameterTypes.STRING, CommandParameterTypes.PASSWORD].includes(param.type)) { + const stringPrompt = await prompt([ + { + type: param.type, + name: param.name, + message: param.description, + validate(value) { + //@ts-ignore + if (value.length === 0 && param.required !== false) { + return "This field is required"; + } else if (["app"].includes(param.name)) { + return fs.existsSync(value) ? true : "File not exists"; + } + return true; + }, + }, + ]); + (params as any)[param.name] = (stringPrompt as any)[Object.keys(stringPrompt)[0]]; + } else if (param.type === CommandParameterTypes.BOOLEAN) { + const booleanPrompt = new BooleanPrompt({ + name: param.name, + message: param.description, + }); + //@ts-ignore + params[param.name] = await booleanPrompt.run(); + } else if (param.type === CommandParameterTypes.SELECT) { + const selectPrompt = new Select({ + name: param.name, + message: param.description, + choices: [ + //@ts-ignore + ...param.params.map((val: any) => val), + ], + }); + (params as any)[param.name] = await selectPrompt.run(); + } } - - switch (selectedCommand.command) { - case CommandTypes.LOGIN: - getToken(params); - break; - case CommandTypes.LIST_BUILD_PROFILES: - getBuildProfiles(params); - break; - case CommandTypes.LIST_BUILD_PROFILE_BRANCHES: - getBranches(params); - break; - case CommandTypes.LIST_BUILD_PROFILE_WORKFLOWS: - getWorkflows(params); - break; - case CommandTypes.LIST_BUILD_PROFILE_COMMITS: - getCommits(params); - break; - case CommandTypes.LIST_BUILD_PROFILE_BUILDS_OF_COMMIT: - getBuildsOfCommit(params); - break; - case CommandTypes.LIST_DISTRIBUTION_PROFILES: - getDistributionProfiles(params); - break; - case CommandTypes.BUILD: - startBuild(params); - break; - case CommandTypes.DOWNLOAD: - downloadArtifact(params); - break; - case CommandTypes.UPLOAD: - uploadArtifact(params); - break; - case CommandTypes.CREATE_DISTRIBUTION_PROFILE: - createDistributionProfile(params); - break; - case CommandTypes.LIST_ENVIRONMENT_VARIABLE_GROUPS: - getEnvironmentVariableGroups(params); - break; - case CommandTypes.CREATE_ENVIRONMENT_VARIABLE_GROUP: - createEnvironmentVariableGroup(params); - break; - case CommandTypes.LIST_ENVIRONMENT_VARIABLES: - getEnvironmentVariables(params); - break; - case CommandTypes.CREATE_ENVIRONMENT_VARIABLE: - createEnvironmentVariable((params as any)); - break; - case CommandTypes.LIST_ENTERPRISE_PROFILES: - getEnterpriseProfiles(); - break; - case CommandTypes.LIST_ENTERPRISE_APP_VERSIONS: - getEnterpriseAppVersions(params); - break; - case CommandTypes.PUBLISH_ENTERPRISE_APP_VERSION: - publishEnterpriseAppVersion(params); - break; - case CommandTypes.UNPUBLISH_ENTERPRISE_APP_VERSION: - unpublishEnterpriseAppVersion(params); - break; - case CommandTypes.REMOVE_ENTERPRISE_APP_VERSION: - removeEnterpriseAppVersion(params); - break; - case CommandTypes.NOTIFY_ENTERPRISE_APP_VERSION: - notifyEnterpriseAppVersion(params); - break; - case CommandTypes.UPLOAD_ENTERPRISE_APP: - uploadEnterpriseApp(params); - break; - case CommandTypes.UPLOAD_ENTERPRISE_APP_VERSION: - uploadEnterpriseAppVersion(params); - break; - case CommandTypes.GET_ENTERPRISE_DOWNLOAD_LINK: - getEnterpriseDownloadLink(params); - break; - default: - console.error('Command not found'); - break; + runCommandHelper({ name: () => selectedCommand.command, args: [], opts: () => params }); + } else { + program.onCommandRun(runCommandHelper); + try { + program.parse(); + } catch (err) { + //hadnling command error + process.exit(1); } -})().catch(() => { }) \ No newline at end of file + } +})().catch(() => {}); diff --git a/src/program.ts b/src/program.ts new file mode 100644 index 0000000..5bda346 --- /dev/null +++ b/src/program.ts @@ -0,0 +1,33 @@ +import { Commands } from "./command"; + +const { createCommand } = require("commander"); + +export type ProgramCommand = { name: () => string; args: any; opts: () => { [key: string]: any } }; + +export const createProgram = () => { + const program = createCommand(); + let actionCb = (cmd: ProgramCommand) => {}; + + program.version(require("../package.json").version, "-v, --version", "output the version number"); + program.option("-i, --interactive", "interactive mode"); + program.option("-o, --output ", "output type", "plain"); + + Commands.forEach((command) => { + let comandPrg = program.command(command.command).description(command.description); + command.params.forEach((param) => { + param.required !== false + ? comandPrg.requiredOption(`--${param.name} <${param.valueType}>`, param.description) + : comandPrg.option(`--${param.name} <${param.valueType}>`, param.description); + }); + comandPrg.action(() => actionCb); + }); + + program.exitOverride(); + program.hook("preAction", (thisCommand: any, actionCommand: ProgramCommand) => { + actionCb({ name: () => actionCommand.name(), args: () => actionCommand.args(), opts: () => ({ ...thisCommand.opts(), ...actionCommand.opts() }) }); + }); + return { + parse: () => program.parse(process.argv), + onCommandRun: (cb: typeof actionCb) => (actionCb = cb), + }; +}; diff --git a/src/services.ts b/src/services.ts index eb884a5..467ddce 100644 --- a/src/services.ts +++ b/src/services.ts @@ -73,7 +73,9 @@ function getHeaders(withToken = true): AxiosRequestConfig['headers'] { return response; } -export async function getToken(options: { pat: string }) { +export type OptionsType = Record & { output?: 'json' | 'plain' } & T; + +export async function getToken(options: OptionsType<{ pat: string }> ) { try { const response = await axios.post(`${AUTH_HOSTNAME}/auth/v1/token`, qs.stringify(options), { headers: { @@ -106,7 +108,7 @@ export async function getDistributionProfiles(options: { }) { console.info('No distribution profiles available.'); return; } - + console.table(distributionProfiles.data .map((distributionProfile: any) => ({ 'Profile Id': distributionProfile.id, @@ -121,7 +123,6 @@ export async function getDistributionProfiles(options: { }) { 'Auto Send': distributionProfile.testingGroupIds ? 'Enabled' : 'Disabled' })) ); - return distributionProfiles.data } catch (error) { handleError(error); @@ -163,7 +164,7 @@ export async function getTestingGroups(options: { }) { } } -export async function getBuildProfiles(options: { }) { +export async function getBuildProfiles(options: OptionsType) { try { const buildProfiles = await axios.get(`${API_HOSTNAME}/build/v2/profiles`, { @@ -174,7 +175,9 @@ export async function getBuildProfiles(options: { }) { console.info('No build profiles available.'); return; } - + if(options.output === 'json'){ + console.log(JSON.stringify(buildProfiles.data)); + }else{ console.table(buildProfiles.data .map((buildProfile: any) => ({ 'Profile Id': buildProfile.id, @@ -188,6 +191,7 @@ export async function getBuildProfiles(options: { }) { 'Auto Build': buildProfile.autoBuildCount === 0 ? 'Disabled' : 'Enabled' })) ); + } return buildProfiles.data; } catch (error) { handleError(error); diff --git a/yarn.lock b/yarn.lock index 3b8365c..0093c15 100644 --- a/yarn.lock +++ b/yarn.lock @@ -115,10 +115,10 @@ combined-stream@^1.0.8: dependencies: delayed-stream "~1.0.0" -commander@^7.1.0: - version "7.2.0" - resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7" - integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw== +commander@^11.1.0: + version "11.1.0" + resolved "https://registry.yarnpkg.com/commander/-/commander-11.1.0.tgz#62fdce76006a68e5c1ab3314dc92e800eb83d906" + integrity sha512-yPVavfyCcRhmorC7rWlkHn15b4wDVgVmBA7kV4QVBsF7kv/9TKJAbAXVTxvTnwP8HHKjRCJDClKbciiYS7p0DQ== defaults@^1.0.3: version "1.0.3"