Skip to content

Commit ed092e7

Browse files
Vikram KaltaVikram Kalta
authored andcommitted
fix: allow only installed apps to be shown in uninstall command
1 parent e7f5b5d commit ed092e7

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

src/commands/app/uninstall.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BaseCommand } from "./base-command";
22
import { flags } from "@contentstack/cli-utilities";
3-
import { getOrg, getApp, fetchApp } from "../../util";
3+
import { getOrg, fetchApp, getInstalledApps } from "../../util";
44
import { commonMsg, uninstallAppMsg } from "../../messages";
55
import { UninstallAppFactory } from "../../factories/uninstall-app-factory";
66

@@ -34,7 +34,7 @@ export default class Uninstall extends BaseCommand<typeof Uninstall> {
3434

3535
// fetch app details
3636
if (!this.flags['app-uid']) {
37-
app = await getApp(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log})
37+
app = await getInstalledApps(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log})
3838
} else {
3939
app = await fetchApp(this.flags, this.sharedConfig.org, {managementSdk: this.managementAppSdk, log: this.log})
4040
}

src/util/common-utils.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,41 @@ function uninstallApp(flags: FlagInput, orgUid: string, options: CommonOptions,
179179
.uninstall()
180180
}
181181

182+
async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) {
183+
const { log } = options;
184+
const apps = (await fetchApps(flags, orgUid, options)) || [];
185+
let batchRequests = [];
186+
// Make calls in batch. 10 requests per batch allowed.
187+
while (apps.length) {
188+
batchRequests.push(apps.splice(0, 10));
189+
}
190+
const results = [];
191+
for (const batch of batchRequests) {
192+
const promises = batch.map(async (app) => {
193+
try {
194+
const installations = await app.listInstallations();
195+
return installations.items.length ? installations.items : null;
196+
}
197+
catch (error) {
198+
log("Unable to fetch installations.", "warn");
199+
log(error, "error");
200+
throw error;
201+
}
202+
});
203+
results.push(await Promise.all(promises));
204+
}
205+
for (let i = batchRequests.length - 1; i >= 0; i--) {
206+
const batchLength = batchRequests[i].length;
207+
for (let j = batchLength - 1; j >= 0; j--) {
208+
if (!results[i][j]) {
209+
batchRequests[i].splice(j, 1);
210+
results[i].splice(j, 1);
211+
}
212+
}
213+
}
214+
return batchRequests.flat();
215+
}
216+
182217
export {
183218
getOrganizations,
184219
getOrgAppUiLocation,
@@ -189,5 +224,6 @@ export {
189224
installApp,
190225
getStacks,
191226
fetchStack,
192-
uninstallApp
227+
uninstallApp,
228+
fetchInstalledApps,
193229
};

src/util/inquirer.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import messages, { $t, commonMsg, errors, uninstallAppMsg } from "../messages";
1616
import {
1717
CommonOptions,
1818
getOrganizations,
19-
fetchApps,
2019
getStacks,
21-
fetchAppInstallations
20+
fetchAppInstallations,
21+
fetchInstalledApps,
22+
fetchApps
2223
} from "./common-utils";
2324

2425
/**
@@ -103,7 +104,28 @@ async function getOrg(flags: FlagInput, options: CommonOptions) {
103104

104105
async function getApp(flags: FlagInput, orgUid: string, options: CommonOptions) : Promise<Record<string, any> | undefined> {
105106
cliux.loader("Loading Apps");
106-
const apps = (await fetchApps(flags, orgUid, options)) || [];
107+
const apps = (await fetchApps(flags, orgUid, options));
108+
cliux.loader("done");
109+
110+
if (apps.length === 0) {
111+
throw new Error(messages.APPS_NOT_FOUND)
112+
}
113+
114+
flags.app = await cliux
115+
.inquire({
116+
type: "search-list",
117+
name: "App",
118+
choices: apps,
119+
message: messages.CHOOSE_APP
120+
})
121+
.then((name) => apps.find(app => app.name === name)?.uid)
122+
123+
return apps.find(app => app.uid === flags.app);
124+
}
125+
126+
async function getInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) : Promise<Record<string, any> | undefined> {
127+
cliux.loader("Loading Apps");
128+
const apps = (await fetchInstalledApps(flags, orgUid, options));
107129
cliux.loader("done");
108130

109131
if (apps.length === 0) {
@@ -201,7 +223,7 @@ async function getInstallation(
201223
// fetch stacks from where the app has to be uninstalled
202224
cliux.loader("done");
203225
const stacks: Stack[] = await getStacks({managementSdk: managementSdkForStacks, log: options.log}, orgUid);
204-
installations = populateMissingDataInInstallations(installations, stacks)
226+
installations = populateMissingDataInInstallations(installations as [Installation], stacks)
205227
// To support uninstall all flag
206228
if (uninstallAll) {
207229
return installations.map(installation => installation.uid).join(',')
@@ -249,6 +271,7 @@ export {
249271
getDirName,
250272
getDeveloperHubUrl,
251273
getApp,
274+
getInstalledApps,
252275
getStack,
253-
getInstallation
276+
getInstallation,
254277
};

0 commit comments

Comments
 (0)