Skip to content

Commit 7e5b1a6

Browse files
authored
Merge pull request #102 from contentstack/fix/CS-41123
fix: allow only installed apps to be shown in uninstall command
2 parents 6dd3e88 + 7339efb commit 7e5b1a6

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
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: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,45 @@ function uninstallApp(flags: FlagInput, orgUid: string, options: CommonOptions,
179179
.uninstall()
180180
}
181181

182+
async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) {
183+
const { managementSdk, 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 managementSdk
195+
.organization(orgUid)
196+
.app(app.uid)
197+
.installation()
198+
.findAll();
199+
return installations.items.length ? installations.items : null;
200+
}
201+
catch (error) {
202+
log("Unable to fetch installations.", "warn");
203+
log(error, "error");
204+
throw error;
205+
}
206+
});
207+
results.push(await Promise.all(promises));
208+
}
209+
for (let i = batchRequests.length - 1; i >= 0; i--) {
210+
const batchLength = batchRequests[i].length;
211+
for (let j = batchLength - 1; j >= 0; j--) {
212+
if (!results[i][j]) {
213+
batchRequests[i].splice(j, 1);
214+
results[i].splice(j, 1);
215+
}
216+
}
217+
}
218+
return batchRequests.flat();
219+
}
220+
182221
export {
183222
getOrganizations,
184223
getOrgAppUiLocation,
@@ -189,5 +228,6 @@ export {
189228
installApp,
190229
getStacks,
191230
fetchStack,
192-
uninstallApp
231+
uninstallApp,
232+
fetchInstalledApps,
193233
};

src/util/inquirer.ts

Lines changed: 27 additions & 4 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) {
@@ -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)