Skip to content

Commit 2c7c819

Browse files
Fix: Code lint fix, formate applied, unit test fixes
1 parent e7b0ced commit 2c7c819

File tree

2 files changed

+131
-97
lines changed

2 files changed

+131
-97
lines changed

src/util/common-utils.ts

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,16 @@ function fetchAppInstallations(
9797
const { managementSdk } = options;
9898
const app: any = flags["app-uid"];
9999
return managementSdk
100-
.organization(orgUid)
101-
.app(app as string)
102-
.installation()
103-
.findAll()
104-
.catch(error => {
105-
const {log} = options;
106-
cliux.loader("failed");
107-
log("Some error occurred while fetching app installations.", "warn");
108-
throw error // throwing error here instead of removing the catch block, as the loader needs to stopped in case there is an error.
109-
})
100+
.organization(orgUid)
101+
.app(app as string)
102+
.installation()
103+
.findAll()
104+
.catch((error) => {
105+
const { log } = options;
106+
cliux.loader("failed");
107+
log("Some error occurred while fetching app installations.", "warn");
108+
throw error; // throwing error here instead of removing the catch block, as the loader needs to stopped in case there is an error.
109+
});
110110
}
111111

112112
function deleteApp(flags: FlagInput, orgUid: string, options: CommonOptions) {
@@ -144,10 +144,10 @@ function fetchStack(flags: FlagInput, options: CommonOptions) {
144144
async function getStacks(
145145
options: CommonOptions,
146146
orgUid: string,
147-
skip: number= 0,
148-
stacks: Stack[] = [],
147+
skip: number = 0,
148+
stacks: Stack[] = []
149149
): Promise<Stack[]> {
150-
const {log, managementSdk} = options;
150+
const { log, managementSdk } = options;
151151
const response = await managementSdk
152152
.organization(orgUid)
153153
.stacks({ include_count: true, limit: 100, asc: "name", skip: skip })
@@ -168,17 +168,26 @@ async function getStacks(
168168
return stacks;
169169
}
170170

171-
function uninstallApp(flags: FlagInput, orgUid: string, options: CommonOptions, installationUid: string) {
172-
const {managementSdk} = options;
173-
const app: unknown = flags['app-uid'];
171+
function uninstallApp(
172+
flags: FlagInput,
173+
orgUid: string,
174+
options: CommonOptions,
175+
installationUid: string
176+
) {
177+
const { managementSdk } = options;
178+
const app: unknown = flags["app-uid"];
174179
return managementSdk
175-
.organization(orgUid)
176-
.app(app as string)
177-
.installation(installationUid as string)
178-
.uninstall()
180+
.organization(orgUid)
181+
.app(app as string)
182+
.installation(installationUid as string)
183+
.uninstall();
179184
}
180185

181-
async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) {
186+
async function fetchInstalledApps(
187+
flags: FlagInput,
188+
orgUid: string,
189+
options: CommonOptions
190+
) {
182191
const { managementSdk, log } = options;
183192
const apps = (await fetchApps(flags, orgUid, options)) || [];
184193
let batchRequests = [];
@@ -196,8 +205,7 @@ async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: Com
196205
.installation()
197206
.findAll();
198207
return installations.items.length ? installations.items : null;
199-
}
200-
catch (error) {
208+
} catch (error) {
201209
log("Unable to fetch installations.", "warn");
202210
log(error, "error");
203211
throw error;
@@ -217,10 +225,10 @@ async function fetchInstalledApps(flags: FlagInput, orgUid: string, options: Com
217225
return batchRequests.flat();
218226
}
219227

220-
export {
221-
getOrganizations,
222-
getOrgAppUiLocation,
223-
fetchApps,
228+
export {
229+
getOrganizations,
230+
getOrgAppUiLocation,
231+
fetchApps,
224232
fetchApp,
225233
fetchAppInstallations,
226234
deleteApp,

src/util/inquirer.ts

Lines changed: 96 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ import {
77
FlagInput,
88
configHandler,
99
Stack,
10-
ContentstackClient
10+
ContentstackClient,
1111
} from "@contentstack/cli-utilities";
12-
import {Installation} from '@contentstack/management/types/app/installation'
13-
import {AppTarget} from '@contentstack/management/types/app/index'
12+
import { Installation } from "@contentstack/management/types/app/installation";
13+
import { AppTarget } from "@contentstack/management/types/app/index";
1414

1515
import config from "../config";
1616
import messages, { $t, commonMsg, errors, uninstallAppMsg } from "../messages";
17-
import {
18-
CommonOptions,
19-
getOrganizations,
17+
import {
18+
CommonOptions,
19+
getOrganizations,
2020
getStacks,
2121
fetchAppInstallations,
2222
fetchInstalledApps,
23-
fetchApps
23+
fetchApps,
2424
} from "./common-utils";
2525

2626
/**
@@ -87,7 +87,7 @@ async function getOrg(flags: FlagInput, options: CommonOptions) {
8787

8888
if (!(flags.org && find(organizations, { uid: flags.org }))) {
8989
if (flags.org) {
90-
throw new Error(messages.ORG_UID_NOT_FOUND)
90+
throw new Error(messages.ORG_UID_NOT_FOUND);
9191
}
9292

9393
flags.org = await cliux
@@ -103,46 +103,54 @@ async function getOrg(flags: FlagInput, options: CommonOptions) {
103103
return flags.org;
104104
}
105105

106-
async function getApp(flags: FlagInput, orgUid: string, options: CommonOptions) : Promise<Record<string, any> | undefined> {
106+
async function getApp(
107+
flags: FlagInput,
108+
orgUid: string,
109+
options: CommonOptions
110+
): Promise<Record<string, any> | undefined> {
107111
cliux.loader("Loading Apps");
108-
const apps = (await fetchApps(flags, orgUid, options));
112+
const apps = await fetchApps(flags, orgUid, options);
109113
cliux.loader("done");
110-
114+
111115
if (apps.length === 0) {
112-
throw new Error(messages.APPS_NOT_FOUND)
116+
throw new Error(messages.APPS_NOT_FOUND);
113117
}
114-
118+
115119
flags.app = await cliux
116120
.inquire({
117121
type: "search-list",
118122
name: "App",
119123
choices: apps,
120-
message: messages.CHOOSE_APP
124+
message: messages.CHOOSE_APP,
121125
})
122-
.then((name) => apps.find(app => app.name === name)?.uid)
126+
.then((name) => apps.find((app) => app.name === name)?.uid);
123127

124-
return apps.find(app => app.uid === flags.app);
128+
return apps.find((app) => app.uid === flags.app);
125129
}
126130

127-
async function getInstalledApps(flags: FlagInput, orgUid: string, options: CommonOptions) : Promise<Record<string, any> | undefined> {
131+
async function getInstalledApps(
132+
flags: FlagInput,
133+
orgUid: string,
134+
options: CommonOptions
135+
): Promise<Record<string, any> | undefined> {
128136
cliux.loader("Loading Apps");
129-
const apps = (await fetchInstalledApps(flags, orgUid, options));
137+
const apps = await fetchInstalledApps(flags, orgUid, options);
130138
cliux.loader("done");
131-
139+
132140
if (apps.length === 0) {
133-
throw new Error(messages.APPS_NOT_FOUND)
141+
throw new Error(messages.APPS_NOT_FOUND);
134142
}
135-
143+
136144
flags.app = await cliux
137145
.inquire({
138146
type: "search-list",
139147
name: "App",
140148
choices: apps,
141-
message: messages.CHOOSE_APP
149+
message: messages.CHOOSE_APP,
142150
})
143-
.then((name) => apps.find(app => app.name === name)?.uid)
151+
.then((name) => apps.find((app) => app.name === name)?.uid);
144152

145-
return apps.find(app => app.uid === flags.app);
153+
return apps.find((app) => app.uid === flags.app);
146154
}
147155

148156
/**
@@ -176,107 +184,125 @@ async function getDeveloperHubUrl(): Promise<string> {
176184
return developerHubBaseUrl;
177185
}
178186

179-
async function getStack(orgUid: string, options: CommonOptions): Promise<Record<string, any> | undefined> {
187+
async function getStack(
188+
orgUid: string,
189+
options: CommonOptions
190+
): Promise<Record<string, any> | undefined> {
180191
cliux.loader("Loading Stacks");
181192
const stacks = (await getStacks(options, orgUid)) || [];
182193
cliux.loader("done");
183-
194+
184195
if (stacks.length === 0) {
185196
// change this to stacks not found
186-
throw new Error(messages.APPS_NOT_FOUND)
197+
throw new Error(messages.APPS_NOT_FOUND);
187198
}
188-
199+
189200
const selectedStack = await cliux
190201
.inquire({
191202
type: "search-list",
192203
name: "Stack",
193204
choices: stacks,
194-
message: messages.CHOOSE_A_STACK
205+
message: messages.CHOOSE_A_STACK,
195206
})
196-
.then((name) => stacks.find(stack => stack.name === name))
207+
.then((name) => stacks.find((stack) => stack.name === name));
197208

198209
return selectedStack;
199210
}
200211

201212
async function getInstallation(
202-
flags: FlagInput,
203-
orgUid: string,
213+
flags: FlagInput,
214+
orgUid: string,
204215
managementSdkForStacks: ContentstackClient,
205216
appType: AppTarget,
206-
options:CommonOptions,
207-
uninstallAll?: boolean,
208-
) : Promise<string> {
209-
const {log} = options;
210-
if (appType === 'stack') {
217+
options: CommonOptions,
218+
uninstallAll?: boolean
219+
): Promise<string> {
220+
const { log } = options;
221+
if (appType === "stack") {
211222
cliux.loader("Loading App Installations");
212223
}
213-
let {items: installations} = (await fetchAppInstallations(flags, orgUid, options)) || [];
224+
let { items: installations } = (await fetchAppInstallations(
225+
flags,
226+
orgUid,
227+
options
228+
)) || { items: [] };
214229

215-
// console.log(installations)
216230
if (!installations?.length) {
217-
if (appType === "stack") cliux.loader("done")
218-
throw new Error(messages.NO_INSTALLATIONS_FOUND)
231+
if (appType === "stack") cliux.loader("done");
232+
throw new Error(messages.NO_INSTALLATIONS_FOUND);
219233
}
220234

221235
let selectedInstallation: string;
222236

223-
if (appType === 'stack') {
237+
if (appType === "stack") {
224238
// fetch stacks from where the app has to be uninstalled
225239
cliux.loader("done");
226-
const stacks: Stack[] = await getStacks({managementSdk: managementSdkForStacks, log: options.log}, orgUid);
227-
installations = populateMissingDataInInstallations(installations as [Installation], stacks)
240+
const stacks: Stack[] = await getStacks(
241+
{ managementSdk: managementSdkForStacks, log: options.log },
242+
orgUid
243+
);
244+
installations = populateMissingDataInInstallations(
245+
installations as [Installation],
246+
stacks
247+
);
228248
// To support uninstall all flag
229249
if (uninstallAll) {
230-
return installations.map(installation => installation.uid).join(',')
250+
return installations.map((installation) => installation.uid).join(",");
231251
}
232-
let _selectedInstallation: string[] = await cliux
233-
.inquire({
234-
type: 'checkbox',
235-
name: 'appInstallation',
252+
let _selectedInstallation: string[] = await cliux.inquire({
253+
type: "checkbox",
254+
name: "appInstallation",
236255
choices: installations,
237256
message: messages.CHOOSE_AN_INSTALLATION,
238257
validate: (input) => {
239258
if (isEmpty(input)) {
240-
return $t(errors.NOT_EMPTY, { value: "stack value"});
259+
return $t(errors.NOT_EMPTY, { value: "stack value" });
241260
}
242261
return true;
243-
}
244-
})
245-
selectedInstallation = _selectedInstallation.join(',')
262+
},
263+
});
264+
selectedInstallation = _selectedInstallation.join(",");
246265
} else {
247266
// as this is an organization app, and it is supposed to only be installed on the source organization
248267
// it will be uninstalled from the selected organization
249-
selectedInstallation = installations.pop()?.uid || ""
268+
selectedInstallation = installations.pop()?.uid || "";
250269
}
251270

252-
log($t(uninstallAppMsg.UNINSTALLING_APP, {
253-
type: appType
254-
}), "info")
255-
256-
return selectedInstallation
271+
log(
272+
$t(uninstallAppMsg.UNINSTALLING_APP, {
273+
type: appType,
274+
}),
275+
"info"
276+
);
277+
278+
return selectedInstallation;
257279
}
258280

259-
function populateMissingDataInInstallations(installations: [Installation], stacks: Stack[]): [Installation] {
260-
let result = installations.map(installation => {
261-
let stack = stacks.filter(stack => stack.api_key === installation.target.uid)?.pop()
281+
function populateMissingDataInInstallations(
282+
installations: [Installation],
283+
stacks: Stack[]
284+
): [Installation] {
285+
let result = installations.map((installation) => {
286+
let stack = stacks
287+
.filter((stack) => stack.api_key === installation.target.uid)
288+
?.pop();
262289
installation.name = stack?.name || installation.target.uid;
263290
installation.value = installation.uid;
264291
return installation;
265292
}) as [Installation];
266293

267294
if (result.length > 0) {
268-
return result
295+
return result;
269296
}
270297

271-
return installations
272-
298+
return installations;
273299
}
274300

275-
export {
276-
getOrg,
277-
getAppName,
278-
getDirName,
279-
getDeveloperHubUrl,
301+
export {
302+
getOrg,
303+
getAppName,
304+
getDirName,
305+
getDeveloperHubUrl,
280306
getApp,
281307
getInstalledApps,
282308
getStack,

0 commit comments

Comments
 (0)