diff --git a/src/commands/app/uninstall.ts b/src/commands/app/uninstall.ts index bc045db..679c33b 100644 --- a/src/commands/app/uninstall.ts +++ b/src/commands/app/uninstall.ts @@ -1,6 +1,6 @@ import { BaseCommand } from "./base-command"; import { flags } from "@contentstack/cli-utilities"; -import { getOrg, getApp, fetchApp } from "../../util"; +import { getOrg, fetchApp, getInstalledApps } from "../../util"; import { commonMsg, uninstallAppMsg } from "../../messages"; import { UninstallAppFactory } from "../../factories/uninstall-app-factory"; @@ -34,7 +34,7 @@ export default class Uninstall extends BaseCommand { // fetch app details if (!this.flags['app-uid']) { - app = await getApp(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log}) + app = await getInstalledApps(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log}) } else { app = await fetchApp(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log}) } diff --git a/src/util/common-utils.ts b/src/util/common-utils.ts index b45f566..39aef40 100644 --- a/src/util/common-utils.ts +++ b/src/util/common-utils.ts @@ -179,6 +179,45 @@ function uninstallApp(flags: FlagInput, orgUid: string, options: CommonOptions, .uninstall() } +async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) { + const { managementSdk, log } = options; + const apps = (await fetchApps(flags, orgUid, options)) || []; + let batchRequests = []; + // Make calls in batch. 10 requests per batch allowed. + while (apps.length) { + batchRequests.push(apps.splice(0, 10)); + } + const results = []; + for (const batch of batchRequests) { + const promises = batch.map(async (app) => { + try { + const installations = await managementSdk + .organization(orgUid) + .app(app.uid) + .installation() + .findAll(); + return installations.items.length ? installations.items : null; + } + catch (error) { + log("Unable to fetch installations.", "warn"); + log(error, "error"); + throw error; + } + }); + results.push(await Promise.all(promises)); + } + for (let i = batchRequests.length - 1; i >= 0; i--) { + const batchLength = batchRequests[i].length; + for (let j = batchLength - 1; j >= 0; j--) { + if (!results[i][j]) { + batchRequests[i].splice(j, 1); + results[i].splice(j, 1); + } + } + } + return batchRequests.flat(); +} + export { getOrganizations, getOrgAppUiLocation, @@ -189,5 +228,6 @@ export { installApp, getStacks, fetchStack, - uninstallApp + uninstallApp, + fetchInstalledApps, }; diff --git a/src/util/inquirer.ts b/src/util/inquirer.ts index 4fa1498..df3eeab 100644 --- a/src/util/inquirer.ts +++ b/src/util/inquirer.ts @@ -16,9 +16,10 @@ import messages, { $t, commonMsg, errors, uninstallAppMsg } from "../messages"; import { CommonOptions, getOrganizations, - fetchApps, getStacks, - fetchAppInstallations + fetchAppInstallations, + fetchInstalledApps, + fetchApps } from "./common-utils"; /** @@ -103,7 +104,28 @@ async function getOrg(flags: FlagInput, options: CommonOptions) { async function getApp(flags: FlagInput, orgUid: string, options: CommonOptions) : Promise | undefined> { cliux.loader("Loading Apps"); - const apps = (await fetchApps(flags, orgUid, options)) || []; + const apps = (await fetchApps(flags, orgUid, options)); + cliux.loader("done"); + + if (apps.length === 0) { + throw new Error(messages.APPS_NOT_FOUND) + } + + flags.app = await cliux + .inquire({ + type: "search-list", + name: "App", + choices: apps, + message: messages.CHOOSE_APP + }) + .then((name) => apps.find(app => app.name === name)?.uid) + + return apps.find(app => app.uid === flags.app); +} + +async function getInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) : Promise | undefined> { + cliux.loader("Loading Apps"); + const apps = (await fetchInstalledApps(flags, orgUid, options)); cliux.loader("done"); if (apps.length === 0) { @@ -249,6 +271,7 @@ export { getDirName, getDeveloperHubUrl, getApp, + getInstalledApps, getStack, - getInstallation + getInstallation, };