@@ -7,20 +7,20 @@ import {
7
7
FlagInput ,
8
8
configHandler ,
9
9
Stack ,
10
- ContentstackClient
10
+ ContentstackClient ,
11
11
} 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" ;
14
14
15
15
import config from "../config" ;
16
16
import messages , { $t , commonMsg , errors , uninstallAppMsg } from "../messages" ;
17
- import {
18
- CommonOptions ,
19
- getOrganizations ,
17
+ import {
18
+ CommonOptions ,
19
+ getOrganizations ,
20
20
getStacks ,
21
21
fetchAppInstallations ,
22
22
fetchInstalledApps ,
23
- fetchApps
23
+ fetchApps ,
24
24
} from "./common-utils" ;
25
25
26
26
/**
@@ -87,7 +87,7 @@ async function getOrg(flags: FlagInput, options: CommonOptions) {
87
87
88
88
if ( ! ( flags . org && find ( organizations , { uid : flags . org } ) ) ) {
89
89
if ( flags . org ) {
90
- throw new Error ( messages . ORG_UID_NOT_FOUND )
90
+ throw new Error ( messages . ORG_UID_NOT_FOUND ) ;
91
91
}
92
92
93
93
flags . org = await cliux
@@ -103,46 +103,54 @@ async function getOrg(flags: FlagInput, options: CommonOptions) {
103
103
return flags . org ;
104
104
}
105
105
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 > {
107
111
cliux . loader ( "Loading Apps" ) ;
108
- const apps = ( await fetchApps ( flags , orgUid , options ) ) ;
112
+ const apps = await fetchApps ( flags , orgUid , options ) ;
109
113
cliux . loader ( "done" ) ;
110
-
114
+
111
115
if ( apps . length === 0 ) {
112
- throw new Error ( messages . APPS_NOT_FOUND )
116
+ throw new Error ( messages . APPS_NOT_FOUND ) ;
113
117
}
114
-
118
+
115
119
flags . app = await cliux
116
120
. inquire ( {
117
121
type : "search-list" ,
118
122
name : "App" ,
119
123
choices : apps ,
120
- message : messages . CHOOSE_APP
124
+ message : messages . CHOOSE_APP ,
121
125
} )
122
- . then ( ( name ) => apps . find ( app => app . name === name ) ?. uid )
126
+ . then ( ( name ) => apps . find ( ( app ) => app . name === name ) ?. uid ) ;
123
127
124
- return apps . find ( app => app . uid === flags . app ) ;
128
+ return apps . find ( ( app ) => app . uid === flags . app ) ;
125
129
}
126
130
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 > {
128
136
cliux . loader ( "Loading Apps" ) ;
129
- const apps = ( await fetchInstalledApps ( flags , orgUid , options ) ) ;
137
+ const apps = await fetchInstalledApps ( flags , orgUid , options ) ;
130
138
cliux . loader ( "done" ) ;
131
-
139
+
132
140
if ( apps . length === 0 ) {
133
- throw new Error ( messages . APPS_NOT_FOUND )
141
+ throw new Error ( messages . APPS_NOT_FOUND ) ;
134
142
}
135
-
143
+
136
144
flags . app = await cliux
137
145
. inquire ( {
138
146
type : "search-list" ,
139
147
name : "App" ,
140
148
choices : apps ,
141
- message : messages . CHOOSE_APP
149
+ message : messages . CHOOSE_APP ,
142
150
} )
143
- . then ( ( name ) => apps . find ( app => app . name === name ) ?. uid )
151
+ . then ( ( name ) => apps . find ( ( app ) => app . name === name ) ?. uid ) ;
144
152
145
- return apps . find ( app => app . uid === flags . app ) ;
153
+ return apps . find ( ( app ) => app . uid === flags . app ) ;
146
154
}
147
155
148
156
/**
@@ -176,107 +184,125 @@ async function getDeveloperHubUrl(): Promise<string> {
176
184
return developerHubBaseUrl ;
177
185
}
178
186
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 > {
180
191
cliux . loader ( "Loading Stacks" ) ;
181
192
const stacks = ( await getStacks ( options , orgUid ) ) || [ ] ;
182
193
cliux . loader ( "done" ) ;
183
-
194
+
184
195
if ( stacks . length === 0 ) {
185
196
// change this to stacks not found
186
- throw new Error ( messages . APPS_NOT_FOUND )
197
+ throw new Error ( messages . APPS_NOT_FOUND ) ;
187
198
}
188
-
199
+
189
200
const selectedStack = await cliux
190
201
. inquire ( {
191
202
type : "search-list" ,
192
203
name : "Stack" ,
193
204
choices : stacks ,
194
- message : messages . CHOOSE_A_STACK
205
+ message : messages . CHOOSE_A_STACK ,
195
206
} )
196
- . then ( ( name ) => stacks . find ( stack => stack . name === name ) )
207
+ . then ( ( name ) => stacks . find ( ( stack ) => stack . name === name ) ) ;
197
208
198
209
return selectedStack ;
199
210
}
200
211
201
212
async function getInstallation (
202
- flags : FlagInput ,
203
- orgUid : string ,
213
+ flags : FlagInput ,
214
+ orgUid : string ,
204
215
managementSdkForStacks : ContentstackClient ,
205
216
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" ) {
211
222
cliux . loader ( "Loading App Installations" ) ;
212
223
}
213
- let { items : installations } = ( await fetchAppInstallations ( flags , orgUid , options ) ) || [ ] ;
224
+ let { items : installations } = ( await fetchAppInstallations (
225
+ flags ,
226
+ orgUid ,
227
+ options
228
+ ) ) || { items : [ ] } ;
214
229
215
- // console.log(installations)
216
230
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 ) ;
219
233
}
220
234
221
235
let selectedInstallation : string ;
222
236
223
- if ( appType === ' stack' ) {
237
+ if ( appType === " stack" ) {
224
238
// fetch stacks from where the app has to be uninstalled
225
239
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
+ ) ;
228
248
// To support uninstall all flag
229
249
if ( uninstallAll ) {
230
- return installations . map ( installation => installation . uid ) . join ( ',' )
250
+ return installations . map ( ( installation ) => installation . uid ) . join ( "," ) ;
231
251
}
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" ,
236
255
choices : installations ,
237
256
message : messages . CHOOSE_AN_INSTALLATION ,
238
257
validate : ( input ) => {
239
258
if ( isEmpty ( input ) ) {
240
- return $t ( errors . NOT_EMPTY , { value : "stack value" } ) ;
259
+ return $t ( errors . NOT_EMPTY , { value : "stack value" } ) ;
241
260
}
242
261
return true ;
243
- }
244
- } )
245
- selectedInstallation = _selectedInstallation . join ( ',' )
262
+ } ,
263
+ } ) ;
264
+ selectedInstallation = _selectedInstallation . join ( "," ) ;
246
265
} else {
247
266
// as this is an organization app, and it is supposed to only be installed on the source organization
248
267
// it will be uninstalled from the selected organization
249
- selectedInstallation = installations . pop ( ) ?. uid || ""
268
+ selectedInstallation = installations . pop ( ) ?. uid || "" ;
250
269
}
251
270
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 ;
257
279
}
258
280
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 ( ) ;
262
289
installation . name = stack ?. name || installation . target . uid ;
263
290
installation . value = installation . uid ;
264
291
return installation ;
265
292
} ) as [ Installation ] ;
266
293
267
294
if ( result . length > 0 ) {
268
- return result
295
+ return result ;
269
296
}
270
297
271
- return installations
272
-
298
+ return installations ;
273
299
}
274
300
275
- export {
276
- getOrg ,
277
- getAppName ,
278
- getDirName ,
279
- getDeveloperHubUrl ,
301
+ export {
302
+ getOrg ,
303
+ getAppName ,
304
+ getDirName ,
305
+ getDeveloperHubUrl ,
280
306
getApp ,
281
307
getInstalledApps ,
282
308
getStack ,
0 commit comments