Skip to content

Commit 09754e4

Browse files
committed
fix: deploy command
1 parent fabe102 commit 09754e4

File tree

2 files changed

+50
-57
lines changed

2 files changed

+50
-57
lines changed

src/commands/app/deploy.ts

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { ApolloClient } from "@apollo/client/core";
2-
import { Flags, FlagInput, cliux } from "@contentstack/cli-utilities";
2+
import { Flags, FlagInput } from "@contentstack/cli-utilities";
33
import config from "@contentstack/cli-launch/dist/config";
44
import { GraphqlApiClient } from "@contentstack/cli-launch/dist/util";
55
import Launch from "@contentstack/cli-launch/dist/commands/launch/index";
66

7-
import { UpdateHostingParams } from "../../types";
7+
import { LaunchProjectRes, UpdateHostingParams } from "../../types";
88
import { commonMsg, deployAppMsg } from "../../messages";
99
import { AppCLIBaseCommand } from "../../app-cli-base-command";
1010
import {
@@ -32,7 +32,8 @@ export default class Deploy extends AppCLIBaseCommand {
3232
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1>",
3333
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Custom Hosting> --app-url <https://localhost:3000>",
3434
"$ <%= 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>",
3637
];
3738

3839
static flags: FlagInput = {
@@ -72,38 +73,40 @@ export default class Deploy extends AppCLIBaseCommand {
7273
const updateHostingPayload: UpdateHostingParams = {
7374
provider: "external",
7475
deployment_url: "",
75-
environment_uid: "",
76-
project_uid: "",
7776
};
77+
const apolloClient = await this.getApolloClient();
78+
const projects = await getProjects(apolloClient);
7879

7980
switch (flags["hosting-type"]) {
8081
case "Custom Hosting":
82+
await this.handleAppDisconnect(projects);
8183
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"];
8286
break;
8387
case "Hosting with Launch":
8488
updateHostingPayload["provider"] = "launch";
8589
const config = setupConfig(flags["config"]);
8690
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);
8895
break;
8996
default:
9097
this.log("Invalid hosting type", "error");
9198
return;
9299
}
93100

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+
);
107110

108111
this.log(
109112
this.$t(deployAppMsg.APP_DEPLOYED, {
@@ -173,49 +176,14 @@ export default class Deploy extends AppCLIBaseCommand {
173176
}).apolloClient;
174177
}
175178

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-
211179
/**
212180
* Handles the project type based on the provided configuration, update hosting payload, and projects.
213181
* @param config - The configuration object.
214182
* @param updateHostingPayload - The update hosting payload.
215183
* @param projects - The list of projects.
216184
* @returns A Promise that resolves to void.
217185
*/
218-
async handleProjectType(
186+
async handleHostingWithLaunch(
219187
config: Record<string, string>,
220188
updateHostingPayload: UpdateHostingParams,
221189
projects: any[]
@@ -298,4 +266,29 @@ export default class Deploy extends AppCLIBaseCommand {
298266
}
299267
return "";
300268
}
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+
}
301294
}

src/types/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ export interface ReinstallParams {
129129
export interface UpdateHostingParams {
130130
provider: string;
131131
deployment_url: string;
132-
environment_uid: string;
133-
project_uid: string;
132+
environment_uid?: string;
133+
project_uid?: string;
134134
}
135135

136136
export interface LaunchProjectRes {

0 commit comments

Comments
 (0)