1
1
import { ApolloClient } from "@apollo/client/core" ;
2
- import { Flags , FlagInput , cliux } from "@contentstack/cli-utilities" ;
2
+ import { Flags , FlagInput } from "@contentstack/cli-utilities" ;
3
3
import config from "@contentstack/cli-launch/dist/config" ;
4
4
import { GraphqlApiClient } from "@contentstack/cli-launch/dist/util" ;
5
5
import Launch from "@contentstack/cli-launch/dist/commands/launch/index" ;
6
6
7
- import { UpdateHostingParams } from "../../types" ;
7
+ import { LaunchProjectRes , UpdateHostingParams } from "../../types" ;
8
8
import { commonMsg , deployAppMsg } from "../../messages" ;
9
9
import { AppCLIBaseCommand } from "../../app-cli-base-command" ;
10
10
import {
@@ -31,8 +31,9 @@ export default class Deploy extends AppCLIBaseCommand {
31
31
"$ <%= config.bin %> <%= command.id %>" ,
32
32
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1>" ,
33
33
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Custom Hosting> --app-url <https://localhost:3000>" ,
34
- "$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project-type <existing-project>" ,
35
- "$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project-type <existing-project> --config <config-path>" ,
34
+ "$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project <existing>" ,
35
+ "$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project <new>" ,
36
+ "$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project <new> --config <config-path>" ,
36
37
] ;
37
38
38
39
static flags : FlagInput = {
@@ -50,10 +51,10 @@ export default class Deploy extends AppCLIBaseCommand {
50
51
description : deployAppMsg . FORCE_DISCONNECT ,
51
52
default : false ,
52
53
} ) ,
53
- "launch-project-type " : Flags . string ( {
54
+ "launch-project" : Flags . string ( {
54
55
multiple : false ,
55
- options : [ "existing-project " , "new-project " ] ,
56
- description : deployAppMsg . LAUNCH_PROJECT_TYPE ,
56
+ options : [ "existing" , "new" ] ,
57
+ description : deployAppMsg . LAUNCH_PROJECT ,
57
58
} ) ,
58
59
config : Flags . string ( {
59
60
char : "c" ,
@@ -68,42 +69,44 @@ export default class Deploy extends AppCLIBaseCommand {
68
69
this . sharedConfig . org = await this . getOrganization ( ) ;
69
70
const app = await this . fetchAppDetails ( ) ;
70
71
72
+ const apolloClient = await this . getApolloClient ( ) ;
73
+ const projects = await getProjects ( apolloClient ) ;
74
+ await this . handleAppDisconnect ( projects ) ;
75
+
71
76
flags [ "hosting-type" ] = flags [ "hosting-type" ] || ( await getHostingType ( ) ) ;
72
77
const updateHostingPayload : UpdateHostingParams = {
73
78
provider : "external" ,
74
79
deployment_url : "" ,
75
- environment_uid : "" ,
76
- project_uid : "" ,
77
80
} ;
78
81
79
82
switch ( flags [ "hosting-type" ] ) {
80
83
case "Custom Hosting" :
81
84
flags [ "app-url" ] = flags [ "app-url" ] || ( await getAppUrl ( ) ) ;
85
+ this . flags [ "app-url" ] = formatUrl ( flags [ "app-url" ] ) ;
86
+ updateHostingPayload [ "deployment_url" ] = this . flags [ "app-url" ] ;
82
87
break ;
83
88
case "Hosting with Launch" :
84
89
updateHostingPayload [ "provider" ] = "launch" ;
85
90
const config = setupConfig ( flags [ "config" ] ) ;
86
91
config [ "name" ] = config [ "name" ] || app ?. name ;
87
- await this . handleHostingWithLaunch ( config , updateHostingPayload ) ;
92
+ this . flags [ "launch-project" ] =
93
+ this . flags [ "launch-project" ] || ( await askProjectType ( ) ) ;
94
+ await this . handleHostingWithLaunch ( config , updateHostingPayload , projects ) ;
88
95
break ;
89
96
default :
90
97
this . log ( "Invalid hosting type" , "error" ) ;
91
98
return ;
92
99
}
93
100
94
- if ( flags [ "app-url" ] ) {
95
- const spinner = cliux . loaderV2 ( "Updating App..." ) ;
96
- await updateApp (
97
- flags ,
98
- this . sharedConfig . org ,
99
- {
100
- managementSdk : this . managementAppSdk ,
101
- log : this . log ,
102
- } ,
103
- updateHostingPayload
104
- ) ;
105
- cliux . loaderV2 ( "done" , spinner ) ;
106
- }
101
+ await updateApp (
102
+ flags ,
103
+ this . sharedConfig . org ,
104
+ {
105
+ managementSdk : this . managementAppSdk ,
106
+ log : this . log ,
107
+ } ,
108
+ updateHostingPayload
109
+ ) ;
107
110
108
111
this . log (
109
112
this . $t ( deployAppMsg . APP_DEPLOYED , {
@@ -173,58 +176,23 @@ export default class Deploy extends AppCLIBaseCommand {
173
176
} ) . apolloClient ;
174
177
}
175
178
176
- /**
177
- * Handles hosting with launch for the application deployment.
178
- *
179
- * @param config - The configuration object.
180
- * @param updateHostingPayload - The payload for updating hosting.
181
- * @returns A Promise that resolves when the hosting with launch is handled.
182
- */
183
- async handleHostingWithLaunch (
184
- config : Record < string , string > ,
185
- updateHostingPayload : UpdateHostingParams
186
- ) : Promise < void > {
187
- const apolloClient = await this . getApolloClient ( ) ;
188
- const projects = await getProjects ( apolloClient ) ;
189
- const isProjectConnected = projects . filter (
190
- ( project ) => project ?. developerHubAppUid === this . flags [ "app-uid" ]
191
- ) ;
192
-
193
- if ( isProjectConnected ?. length ) {
194
- this . flags [ "yes" ] = this . flags [ "yes" ] || ( await askConfirmation ( ) ) ;
195
- if ( ! this . flags [ "yes" ] ) {
196
- throw new Error ( deployAppMsg . APP_UPDATE_TERMINATION_MSG ) ;
197
- }
198
- const spinner = cliux . loaderV2 ( "Disconnecting launch project..." ) ;
199
- await disconnectApp (
200
- this . flags ,
201
- this . sharedConfig . org ,
202
- this . developerHubBaseUrl
203
- ) ;
204
- cliux . loaderV2 ( "disconnected..." , spinner ) ;
205
- }
206
- this . flags [ "launch-project-type" ] =
207
- this . flags [ "launch-project-type" ] || ( await askProjectType ( ) ) ;
208
- await this . handleProjectType ( config , updateHostingPayload , projects ) ;
209
- }
210
-
211
179
/**
212
180
* Handles the project type based on the provided configuration, update hosting payload, and projects.
213
181
* @param config - The configuration object.
214
182
* @param updateHostingPayload - The update hosting payload.
215
183
* @param projects - The list of projects.
216
184
* @returns A Promise that resolves to void.
217
185
*/
218
- async handleProjectType (
186
+ async handleHostingWithLaunch (
219
187
config : Record < string , string > ,
220
188
updateHostingPayload : UpdateHostingParams ,
221
189
projects : any [ ]
222
190
) : Promise < void > {
223
191
let url : string = "" ;
224
192
225
- if ( this . flags [ "launch-project-type " ] === "existing-project " ) {
193
+ if ( this . flags [ "launch-project" ] === "existing" ) {
226
194
url = await this . handleExistingProject ( updateHostingPayload , projects ) ;
227
- } else if ( this . flags [ "launch-project-type " ] === "new-project " ) {
195
+ } else if ( this . flags [ "launch-project" ] === "new" ) {
228
196
config [ "name" ] = await handleProjectNameConflict (
229
197
config [ "name" ] ,
230
198
projects
@@ -298,4 +266,29 @@ export default class Deploy extends AppCLIBaseCommand {
298
266
}
299
267
return "" ;
300
268
}
269
+
270
+ /**
271
+ * Handles the disconnection of an app from projects.
272
+ *
273
+ * @param projects - An array of LaunchProjectRes objects representing the projects.
274
+ * @returns {Promise<void> } - A promise that resolves when the disconnection is complete.
275
+ * @throws {Error } - Throws an error if the user chooses not to disconnect the app.
276
+ */
277
+ async handleAppDisconnect ( projects : LaunchProjectRes [ ] ) : Promise < void > {
278
+ const isProjectConnected = projects . filter (
279
+ ( project ) => project ?. developerHubAppUid === this . flags [ "app-uid" ]
280
+ ) ;
281
+
282
+ if ( isProjectConnected ?. length ) {
283
+ this . flags [ "yes" ] = this . flags [ "yes" ] || ( await askConfirmation ( ) ) ;
284
+ if ( ! this . flags [ "yes" ] ) {
285
+ throw new Error ( deployAppMsg . APP_UPDATE_TERMINATION_MSG ) ;
286
+ }
287
+ await disconnectApp (
288
+ this . flags ,
289
+ this . sharedConfig . org ,
290
+ this . developerHubBaseUrl
291
+ ) ;
292
+ }
293
+ }
301
294
}
0 commit comments