Skip to content

Commit

Permalink
Merge pull request #102 from contentstack/fix/CS-41123
Browse files Browse the repository at this point in the history
fix: allow only installed apps to be shown in uninstall command
  • Loading branch information
netrajpatel authored Oct 11, 2023
2 parents 59c6c16 + 5725dd7 commit 10a3789
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/commands/app/uninstall.ts
Original file line number Diff line number Diff line change
@@ -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";

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

// 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})
}
Expand Down
42 changes: 41 additions & 1 deletion src/util/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -189,5 +228,6 @@ export {
installApp,
getStacks,
fetchStack,
uninstallApp
uninstallApp,
fetchInstalledApps,
};
31 changes: 27 additions & 4 deletions src/util/inquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import messages, { $t, commonMsg, errors, uninstallAppMsg } from "../messages";
import {
CommonOptions,
getOrganizations,
fetchApps,
getStacks,
fetchAppInstallations
fetchAppInstallations,
fetchInstalledApps,
fetchApps
} from "./common-utils";

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

async function getApp(flags: FlagInput, orgUid: string, options: CommonOptions) : Promise<Record<string, any> | 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<Record<string, any> | undefined> {
cliux.loader("Loading Apps");
const apps = (await fetchInstalledApps(flags, orgUid, options));
cliux.loader("done");

if (apps.length === 0) {
Expand Down Expand Up @@ -249,6 +271,7 @@ export {
getDirName,
getDeveloperHubUrl,
getApp,
getInstalledApps,
getStack,
getInstallation
getInstallation,
};

0 comments on commit 10a3789

Please sign in to comment.