Skip to content

Commit

Permalink
Merge pull request #256 from contentstack/fix/deploy-command
Browse files Browse the repository at this point in the history
fix: deploy command
  • Loading branch information
aman19K authored Jun 19, 2024
2 parents fabe102 + 2f923bc commit cef7d91
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 68 deletions.
115 changes: 54 additions & 61 deletions src/commands/app/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ApolloClient } from "@apollo/client/core";
import { Flags, FlagInput, cliux } from "@contentstack/cli-utilities";
import { Flags, FlagInput } from "@contentstack/cli-utilities";
import config from "@contentstack/cli-launch/dist/config";
import { GraphqlApiClient } from "@contentstack/cli-launch/dist/util";
import Launch from "@contentstack/cli-launch/dist/commands/launch/index";

import { UpdateHostingParams } from "../../types";
import { LaunchProjectRes, UpdateHostingParams } from "../../types";
import { commonMsg, deployAppMsg } from "../../messages";
import { AppCLIBaseCommand } from "../../app-cli-base-command";
import {
Expand All @@ -31,8 +31,9 @@ export default class Deploy extends AppCLIBaseCommand {
"$ <%= config.bin %> <%= command.id %>",
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1>",
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Custom Hosting> --app-url <https://localhost:3000>",
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project-type <existing-project>",
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project-type <existing-project> --config <config-path>",
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project <existing>",
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project <new>",
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --hosting-type <Hosting with Launch> --launch-project <new> --config <config-path>",
];

static flags: FlagInput = {
Expand All @@ -50,10 +51,10 @@ export default class Deploy extends AppCLIBaseCommand {
description: deployAppMsg.FORCE_DISCONNECT,
default: false,
}),
"launch-project-type": Flags.string({
"launch-project": Flags.string({
multiple: false,
options: ["existing-project", "new-project"],
description: deployAppMsg.LAUNCH_PROJECT_TYPE,
options: ["existing", "new"],
description: deployAppMsg.LAUNCH_PROJECT,
}),
config: Flags.string({
char: "c",
Expand All @@ -68,42 +69,44 @@ export default class Deploy extends AppCLIBaseCommand {
this.sharedConfig.org = await this.getOrganization();
const app = await this.fetchAppDetails();

const apolloClient = await this.getApolloClient();
const projects = await getProjects(apolloClient);
await this.handleAppDisconnect(projects);

flags["hosting-type"] = flags["hosting-type"] || (await getHostingType());
const updateHostingPayload: UpdateHostingParams = {
provider: "external",
deployment_url: "",
environment_uid: "",
project_uid: "",
};

switch (flags["hosting-type"]) {
case "Custom Hosting":
flags["app-url"] = flags["app-url"] || (await getAppUrl());
this.flags["app-url"] = formatUrl(flags["app-url"]);
updateHostingPayload["deployment_url"] = this.flags["app-url"];
break;
case "Hosting with Launch":
updateHostingPayload["provider"] = "launch";
const config = setupConfig(flags["config"]);
config["name"] = config["name"] || app?.name;
await this.handleHostingWithLaunch(config, updateHostingPayload);
this.flags["launch-project"] =
this.flags["launch-project"] || (await askProjectType());
await this.handleHostingWithLaunch(config, updateHostingPayload, projects);
break;
default:
this.log("Invalid hosting type", "error");
return;
}

if (flags["app-url"]) {
const spinner = cliux.loaderV2("Updating App...");
await updateApp(
flags,
this.sharedConfig.org,
{
managementSdk: this.managementAppSdk,
log: this.log,
},
updateHostingPayload
);
cliux.loaderV2("done", spinner);
}
await updateApp(
flags,
this.sharedConfig.org,
{
managementSdk: this.managementAppSdk,
log: this.log,
},
updateHostingPayload
);

this.log(
this.$t(deployAppMsg.APP_DEPLOYED, {
Expand Down Expand Up @@ -173,58 +176,23 @@ export default class Deploy extends AppCLIBaseCommand {
}).apolloClient;
}

/**
* Handles hosting with launch for the application deployment.
*
* @param config - The configuration object.
* @param updateHostingPayload - The payload for updating hosting.
* @returns A Promise that resolves when the hosting with launch is handled.
*/
async handleHostingWithLaunch(
config: Record<string, string>,
updateHostingPayload: UpdateHostingParams
): Promise<void> {
const apolloClient = await this.getApolloClient();
const projects = await getProjects(apolloClient);
const isProjectConnected = projects.filter(
(project) => project?.developerHubAppUid === this.flags["app-uid"]
);

if (isProjectConnected?.length) {
this.flags["yes"] = this.flags["yes"] || (await askConfirmation());
if (!this.flags["yes"]) {
throw new Error(deployAppMsg.APP_UPDATE_TERMINATION_MSG);
}
const spinner = cliux.loaderV2("Disconnecting launch project...");
await disconnectApp(
this.flags,
this.sharedConfig.org,
this.developerHubBaseUrl
);
cliux.loaderV2("disconnected...", spinner);
}
this.flags["launch-project-type"] =
this.flags["launch-project-type"] || (await askProjectType());
await this.handleProjectType(config, updateHostingPayload, projects);
}

/**
* Handles the project type based on the provided configuration, update hosting payload, and projects.
* @param config - The configuration object.
* @param updateHostingPayload - The update hosting payload.
* @param projects - The list of projects.
* @returns A Promise that resolves to void.
*/
async handleProjectType(
async handleHostingWithLaunch(
config: Record<string, string>,
updateHostingPayload: UpdateHostingParams,
projects: any[]
): Promise<void> {
let url: string = "";

if (this.flags["launch-project-type"] === "existing-project") {
if (this.flags["launch-project"] === "existing") {
url = await this.handleExistingProject(updateHostingPayload, projects);
} else if (this.flags["launch-project-type"] === "new-project") {
} else if (this.flags["launch-project"] === "new") {
config["name"] = await handleProjectNameConflict(
config["name"],
projects
Expand Down Expand Up @@ -298,4 +266,29 @@ export default class Deploy extends AppCLIBaseCommand {
}
return "";
}

/**
* Handles the disconnection of an app from projects.
*
* @param projects - An array of LaunchProjectRes objects representing the projects.
* @returns {Promise<void>} - A promise that resolves when the disconnection is complete.
* @throws {Error} - Throws an error if the user chooses not to disconnect the app.
*/
async handleAppDisconnect(projects: LaunchProjectRes[]): Promise<void> {
const isProjectConnected = projects.filter(
(project) => project?.developerHubAppUid === this.flags["app-uid"]
);

if (isProjectConnected?.length) {
this.flags["yes"] = this.flags["yes"] || (await askConfirmation());
if (!this.flags["yes"]) {
throw new Error(deployAppMsg.APP_UPDATE_TERMINATION_MSG);
}
await disconnectApp(
this.flags,
this.sharedConfig.org,
this.developerHubBaseUrl
);
}
}
}
2 changes: 1 addition & 1 deletion src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ const reinstallAppMsg = {
const deployAppMsg = {
APP_DEPLOYED: "{app} deployed successfully.",
FORCE_DISCONNECT: "Force disconnect launch project by skipping the confirmation",
LAUNCH_PROJECT_TYPE: "Launch Project Type",
LAUNCH_PROJECT: "Launch Project",
APP_URL: "App URL",
HOSTING_TYPE: "Hosting Type",
CONFIG_FILE: "[optional] path of config file",
Expand Down
4 changes: 2 additions & 2 deletions src/types/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ export interface ReinstallParams {
export interface UpdateHostingParams {
provider: string;
deployment_url: string;
environment_uid: string;
project_uid: string;
environment_uid?: string;
project_uid?: string;
}

export interface LaunchProjectRes {
Expand Down
8 changes: 4 additions & 4 deletions src/util/inquirer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,11 @@ async function selectProject(
const askProjectType = async (): Promise<string> => {
return await cliux.inquire<string>({
type: "list",
name: "selected_project_type",
message: "Launch Project type",
name: "selected_launch_project",
message: "Launch Project",
choices: [
{ name: "Existing", value: "existing-project" },
{ name: "New", value: "new-project" },
{ name: "Existing", value: "existing" },
{ name: "New", value: "new" },
],
});
};
Expand Down

0 comments on commit cef7d91

Please sign in to comment.