@@ -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
1515import config from "../config" ;
1616import 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
201212async 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