|
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 {
|
@@ -32,7 +32,8 @@ export default class Deploy extends AppCLIBaseCommand {
|
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 | 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>", |
| 35 | + "$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project-type <new-project>", |
| 36 | + "$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project-type <new-project> --config <config-path>", |
36 | 37 | ];
|
37 | 38 |
|
38 | 39 | static flags: FlagInput = {
|
@@ -72,38 +73,40 @@ export default class Deploy extends AppCLIBaseCommand {
|
72 | 73 | const updateHostingPayload: UpdateHostingParams = {
|
73 | 74 | provider: "external",
|
74 | 75 | deployment_url: "",
|
75 |
| - environment_uid: "", |
76 |
| - project_uid: "", |
77 | 76 | };
|
| 77 | + const apolloClient = await this.getApolloClient(); |
| 78 | + const projects = await getProjects(apolloClient); |
78 | 79 |
|
79 | 80 | switch (flags["hosting-type"]) {
|
80 | 81 | case "Custom Hosting":
|
| 82 | + await this.handleAppDisconnect(projects); |
81 | 83 | flags["app-url"] = flags["app-url"] || (await getAppUrl());
|
| 84 | + this.flags["app-url"] = formatUrl(flags["app-url"]); |
| 85 | + updateHostingPayload["deployment_url"] = this.flags["app-url"]; |
82 | 86 | break;
|
83 | 87 | case "Hosting with Launch":
|
84 | 88 | updateHostingPayload["provider"] = "launch";
|
85 | 89 | const config = setupConfig(flags["config"]);
|
86 | 90 | config["name"] = config["name"] || app?.name;
|
87 |
| - await this.handleHostingWithLaunch(config, updateHostingPayload); |
| 91 | + await this.handleAppDisconnect(projects); |
| 92 | + this.flags["launch-project-type"] = |
| 93 | + this.flags["launch-project-type"] || (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,49 +176,14 @@ 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[]
|
@@ -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