Skip to content

Commit cef7d91

Browse files
authored
Merge pull request #256 from contentstack/fix/deploy-command
fix: deploy command
2 parents fabe102 + 2f923bc commit cef7d91

File tree

4 files changed

+61
-68
lines changed

4 files changed

+61
-68
lines changed

src/commands/app/deploy.ts

Lines changed: 54 additions & 61 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 {
@@ -31,8 +31,9 @@ export default class Deploy extends AppCLIBaseCommand {
3131
"$ <%= config.bin %> <%= command.id %>",
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>",
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>",
3637
];
3738

3839
static flags: FlagInput = {
@@ -50,10 +51,10 @@ export default class Deploy extends AppCLIBaseCommand {
5051
description: deployAppMsg.FORCE_DISCONNECT,
5152
default: false,
5253
}),
53-
"launch-project-type": Flags.string({
54+
"launch-project": Flags.string({
5455
multiple: false,
55-
options: ["existing-project", "new-project"],
56-
description: deployAppMsg.LAUNCH_PROJECT_TYPE,
56+
options: ["existing", "new"],
57+
description: deployAppMsg.LAUNCH_PROJECT,
5758
}),
5859
config: Flags.string({
5960
char: "c",
@@ -68,42 +69,44 @@ export default class Deploy extends AppCLIBaseCommand {
6869
this.sharedConfig.org = await this.getOrganization();
6970
const app = await this.fetchAppDetails();
7071

72+
const apolloClient = await this.getApolloClient();
73+
const projects = await getProjects(apolloClient);
74+
await this.handleAppDisconnect(projects);
75+
7176
flags["hosting-type"] = flags["hosting-type"] || (await getHostingType());
7277
const updateHostingPayload: UpdateHostingParams = {
7378
provider: "external",
7479
deployment_url: "",
75-
environment_uid: "",
76-
project_uid: "",
7780
};
7881

7982
switch (flags["hosting-type"]) {
8083
case "Custom Hosting":
8184
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"];
8287
break;
8388
case "Hosting with Launch":
8489
updateHostingPayload["provider"] = "launch";
8590
const config = setupConfig(flags["config"]);
8691
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);
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,58 +176,23 @@ 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[]
222190
): Promise<void> {
223191
let url: string = "";
224192

225-
if (this.flags["launch-project-type"] === "existing-project") {
193+
if (this.flags["launch-project"] === "existing") {
226194
url = await this.handleExistingProject(updateHostingPayload, projects);
227-
} else if (this.flags["launch-project-type"] === "new-project") {
195+
} else if (this.flags["launch-project"] === "new") {
228196
config["name"] = await handleProjectNameConflict(
229197
config["name"],
230198
projects
@@ -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/messages/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const reinstallAppMsg = {
115115
const deployAppMsg = {
116116
APP_DEPLOYED: "{app} deployed successfully.",
117117
FORCE_DISCONNECT: "Force disconnect launch project by skipping the confirmation",
118-
LAUNCH_PROJECT_TYPE: "Launch Project Type",
118+
LAUNCH_PROJECT: "Launch Project",
119119
APP_URL: "App URL",
120120
HOSTING_TYPE: "Hosting Type",
121121
CONFIG_FILE: "[optional] path of config file",

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 {

src/util/inquirer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,11 +349,11 @@ async function selectProject(
349349
const askProjectType = async (): Promise<string> => {
350350
return await cliux.inquire<string>({
351351
type: "list",
352-
name: "selected_project_type",
353-
message: "Launch Project type",
352+
name: "selected_launch_project",
353+
message: "Launch Project",
354354
choices: [
355-
{ name: "Existing", value: "existing-project" },
356-
{ name: "New", value: "new-project" },
355+
{ name: "Existing", value: "existing" },
356+
{ name: "New", value: "new" },
357357
],
358358
});
359359
};

0 commit comments

Comments
 (0)