-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeploy.ts
More file actions
306 lines (283 loc) · 10.2 KB
/
deploy.ts
File metadata and controls
306 lines (283 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import { Flags, FlagInput } from "@contentstack/cli-utilities";
import {
GraphqlApiClient,
Launch,
config,
ApolloClient,
} from "@contentstack/cli-launch";
import { LaunchProjectRes, UpdateHostingParams } from "../../types";
import { commonMsg, deployAppMsg } from "../../messages";
import { AppCLIBaseCommand } from "../../app-cli-base-command";
import {
getOrg,
fetchApp,
getHostingType,
updateApp,
getAppUrl,
selectProject,
getProjects,
getApp,
askConfirmation,
askProjectType,
setupConfig,
disconnectApp,
formatUrl,
handleProjectNameConflict,
getLaunchHubUrl,
} from "../../util";
export default class Deploy extends AppCLIBaseCommand {
static description = "Deploy an app";
static examples = [
"$ <%= 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 <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 = {
"app-uid": Flags.string({
description: commonMsg.APP_UID,
}),
"hosting-type": Flags.string({
multiple: false,
options: ["hosting-with-launch", "custom-hosting"],
description: deployAppMsg.HOSTING_TYPE,
}),
"app-url": Flags.string({
description: deployAppMsg.APP_URL,
}),
"launch-project": Flags.string({
multiple: false,
options: ["existing", "new"],
description: deployAppMsg.LAUNCH_PROJECT,
}),
config: Flags.string({
char: "c",
description: deployAppMsg.CONFIG_FILE,
}),
...AppCLIBaseCommand.baseFlags,
};
async run(): Promise<void> {
try {
const { flags } = this;
flags["app-uid"] = this.manifestData?.uid ?? flags["app-uid"];
this.sharedConfig.org = await this.getOrganization();
const app = await this.fetchAppDetails();
this.flags["app-uid"] = app?.uid || "";
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: "",
};
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;
this.flags["launch-project"] =
this.flags["launch-project"] || (await askProjectType());
await this.handleHostingWithLaunch(
config,
updateHostingPayload,
projects
);
break;
default:
this.log("Provide a valid hosting type.", "error");
return;
}
if (this.flags["app-uid"]) {
await updateApp(
flags,
this.sharedConfig.org,
{
marketplaceSdk: this.marketplaceAppSdk,
log: this.log,
},
updateHostingPayload
);
}
this.log(
this.$t(deployAppMsg.APP_DEPLOYED, {
app: app?.name || flags["app-uid"],
}),
"info"
);
this.log(`App URL: ${flags["app-url"]}`, "info");
} catch (error: any) {
this.log(error?.errorMessage || error?.message || error, "error");
process.exit(1);
}
}
/**
* Retrieves the organization UID for the app deployment.
* If the organization UID is already available in the manifest data, it is returned.
* Otherwise, it makes an API call to retrieve the organization UID using the provided flags and management SDK.
*
* @returns A Promise that resolves to the organization UID.
*/
async getOrganization(): Promise<string> {
const organizationUid = this.manifestData?.organization_uid ?? "";
if (!organizationUid) {
return await getOrg(this.flags, {
managementSdk: this.managementSdk,
log: this.log,
});
}
return organizationUid;
}
/**
* Fetches the details of an app.
* If the "app-uid" flag is provided, it fetches the app details using the fetchApp function.
* Otherwise, it fetches the app details using the getApp function.
* @returns A Promise that resolves to the fetched app details.
*/
async fetchAppDetails(): Promise<any> {
if (!this.flags["app-uid"]) {
return await getApp(this.flags, this.sharedConfig.org, {
marketplaceSdk: this.marketplaceAppSdk,
log: this.log,
});
}
return await fetchApp(this.flags, this.sharedConfig.org, {
marketplaceSdk: this.marketplaceAppSdk,
log: this.log,
});
}
/**
* Retrieves the Apollo Client instance for making GraphQL API requests.
* @returns {Promise<ApolloClient>} The Apollo Client instance.
*/
async getApolloClient(projectUid = ""): Promise<ApolloClient<any>> {
const baseUrl = config.launchBaseUrl || getLaunchHubUrl();
const manageApiBaseUrl = `${baseUrl}/${config.manageApiEndpoint}`;
return await new GraphqlApiClient({
headers: {
"X-CS-CLI": this.context.analyticsInfo,
"x-project-uid": projectUid,
organization_uid: this.sharedConfig.org,
},
baseUrl: manageApiBaseUrl,
}).apolloClient;
}
/**
* 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 handleHostingWithLaunch(
config: Record<string, string>,
updateHostingPayload: UpdateHostingParams,
projects: any[]
): Promise<void> {
let url: string = "";
if (this.flags["launch-project"] === "existing") {
url = await this.handleExistingProject(updateHostingPayload, projects);
} else if (this.flags["launch-project"] === "new") {
config["name"] = await handleProjectNameConflict(
config["name"],
projects
);
url = await this.handleNewProject(config, updateHostingPayload);
} else {
this.log("Provide a valid Launch project.", "error");
return;
}
this.flags["app-url"] = formatUrl(url);
updateHostingPayload["deployment_url"] = this.flags["app-url"];
}
/**
* Handles an existing project by updating the hosting payload and returning the project URL.
* @param updateHostingPayload - The payload containing the updated hosting information.
* @param projects - An array of projects to choose from.
* @returns A Promise that resolves to the project URL if a project is selected, otherwise an empty string.
*/
async handleExistingProject(
updateHostingPayload: UpdateHostingParams,
projects: any[]
): Promise<string> {
const selectedProject = await selectProject(projects);
if (selectedProject) {
updateHostingPayload["environment_uid"] = selectedProject.environmentUid;
updateHostingPayload["project_uid"] = selectedProject.uid;
return selectedProject.url || "";
}
return "";
}
/**
* Handles the deployment of a new project.
*
* @param config - The configuration object containing project details.
* @param updateHostingPayload - The payload for updating hosting parameters.
* @returns The URL of the deployed project.
*/
async handleNewProject(
config: Record<string, string>,
updateHostingPayload: UpdateHostingParams
): Promise<string> {
const args = [];
const configMappings = {
org: this.sharedConfig.org,
name: config["name"],
type: config["type"],
environment: config["environment"],
framework: config["framework"],
"build-command": config["build-command"],
"out-dir": config["out-dir"],
branch: config["branch"],
"variable-type": config["variable-type"],
alias: config["alias"],
"redeploy-atest": config["redeploy-latest"],
"redeploy-last-upload": config["redeploy-last-upload"],
"env-variables": config["env-variables"],
};
for (const [key, value] of Object.entries(configMappings)) {
if (config[key]) {
args.push(`--${key}`, value);
}
}
await Launch.run(args);
const apolloClient = await this.getApolloClient();
const projects = await getProjects(apolloClient);
const project = projects.find((project) => project.name === config["name"]);
if (project) {
updateHostingPayload["environment_uid"] = project.environmentUid;
updateHostingPayload["project_uid"] = project.uid;
return project.url || "";
}
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, {
marketplaceSdk: this.marketplaceAppSdk,
log: this.log,
});
}
}
}