Skip to content

Commit bc39ece

Browse files
committed
refactor: code clean up & added reinstall msg guide
1 parent 0946d3a commit bc39ece

File tree

5 files changed

+49
-32
lines changed

5 files changed

+49
-32
lines changed

src/commands/app/install.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ export default class Install extends AppCLIBaseCommand {
122122
this.displayStackUrl();
123123
} catch (error: any) {
124124
this.log(error?.errorMessage || error?.message || error, "error");
125+
if (
126+
error?.errorMessage === "Installation for app is already done" &&
127+
error?.status === 400
128+
) {
129+
this.displayReInstallMsg();
130+
}
125131
this.exit(1);
126132
}
127133
}
@@ -136,4 +142,11 @@ export default class Install extends AppCLIBaseCommand {
136142
"info"
137143
);
138144
}
139-
}
145+
146+
/**
147+
* @method displayStackUrl - show guid to stack after installing app successfully in the stack
148+
*/
149+
displayReInstallMsg(): void {
150+
this.log(this.messages.APP_ALREADY_INSTALLED, "info");
151+
}
152+
}

src/commands/app/reinstall.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export default class Reinstall extends AppCLIBaseCommand {
6363
this.flags["app-uid"] = app?.uid;
6464

6565
if (appType === "organization" && this.flags["stack-api-key"]) {
66-
appType = 'organization'
66+
appType = "organization";
6767
const confirmation =
6868
this.flags["yes"] ||
6969
(await cliux.inquire({
@@ -79,7 +79,7 @@ export default class Reinstall extends AppCLIBaseCommand {
7979
}
8080

8181
if (appType === "stack" && !this.flags["stack-api-key"]) {
82-
appType = 'stack'
82+
appType = "stack";
8383

8484
this.log(
8585
$t(reinstallAppMsg.MISSING_STACK_API_KEY, {
@@ -106,9 +106,9 @@ export default class Reinstall extends AppCLIBaseCommand {
106106
flags: this.flags,
107107
type: appType,
108108
developerHubBaseUrl: this.developerHubBaseUrl,
109-
},
110-
{orgUid: this.sharedConfig.org, manifestUid: this.manifestData.uid}
111-
);
109+
orgUid: this.sharedConfig.org,
110+
manifestUid: this.manifestData.uid,
111+
});
112112
this.log(
113113
$t(reinstallAppMsg.APP_REINSTALLED_SUCCESSFULLY, {
114114
app: app?.name || (this.flags["app-uid"] as string),

src/messages/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const installAppMsg = {
8989
INSTALL_ORG_APP_TO_STACK: "{app} is an organization app. It cannot be installed to a stack. Do you want to proceed?",
9090
MISSING_STACK_API_KEY: "As {app} is a stack app, it can only be installed in a stack. Please select a stack.",
9191
INSTALLING_APP_NOTICE: "Installing {app} on {type} {target}.",
92+
APP_ALREADY_INSTALLED: "Please use $ csdx app:reinstall to reinstall the app.",
9293
}
9394

9495
const uninstallAppMsg = {

src/util/api-request-handler.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,31 @@ export async function apiRequestHandler(params: RequestParams): Promise<any> {
2727

2828
try {
2929
let response;
30-
if (method === "GET") {
31-
response = await httpClient.get(url);
32-
} else if (method === "POST") {
33-
response = await httpClient.post(url, payload);
34-
} else if (method === "PUT") {
35-
response = await httpClient.put(url, payload);
36-
} else if (method === "DELETE") {
37-
response = await httpClient.delete(url);
38-
} else {
39-
throw new Error(`Unsupported HTTP method: ${method}`);
30+
switch (method) {
31+
case "GET":
32+
response = await httpClient.get(url);
33+
break;
34+
case "POST":
35+
response = await httpClient.post(url, payload);
36+
break;
37+
case "PUT":
38+
response = await httpClient.put(url, payload);
39+
break;
40+
case "DELETE":
41+
response = await httpClient.delete(url);
42+
break;
43+
default:
44+
throw new Error(`Unsupported HTTP method: ${method}`);
4045
}
4146

4247
const { status, data } = response;
4348
if (status >= 200 && status < 300) {
4449
return data;
4550
}
46-
const error_message = data?.error
51+
const errorMessage = data?.error
4752
? formatErrors(data)
4853
: data?.error_message || "Something went wrong";
49-
throw error_message;
54+
throw errorMessage;
5055
} catch (error) {
5156
throw error;
5257
}

src/util/common-utils.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,10 @@ async function reinstallApp(params: {
139139
flags: FlagInput;
140140
type: string;
141141
developerHubBaseUrl: string;
142-
},
143-
headers: { orgUid: string, manifestUid: string }
144-
): Promise<void> {
145-
const { type, developerHubBaseUrl, flags } = params;
146-
const {orgUid, manifestUid } = headers;
142+
orgUid: string;
143+
manifestUid: string;
144+
}): Promise<void> {
145+
const { type, developerHubBaseUrl, flags, orgUid, manifestUid } = params;
147146
const payload = {
148147
target_type: type,
149148
target_uid: (flags["stack-api-key"] as any) || orgUid,
@@ -152,14 +151,14 @@ headers: { orgUid: string, manifestUid: string }
152151
const url = `https://${developerHubBaseUrl}/manifests/${manifestUid}/reinstall`;
153152
try {
154153
const result = await apiRequestHandler({
155-
orgUid,
154+
orgUid,
156155
payload,
157156
url,
158-
method: "PUT"
159-
})
160-
return result
157+
method: "PUT",
158+
});
159+
return result;
161160
} catch (err) {
162-
throw err
161+
throw err;
163162
}
164163
}
165164

@@ -254,9 +253,8 @@ async function fetchInstalledApps(
254253
return batchRequests.flat();
255254
}
256255

257-
258-
// To remove the relative path
259-
const sanitizePath = (str: string) => str?.replace(/^(\.\.(\/|\\|$))+/, '');
256+
// To remove the relative path
257+
const sanitizePath = (str: string) => str?.replace(/^(\.\.(\/|\\|$))+/, "");
260258

261259
export {
262260
getOrganizations,
@@ -271,5 +269,5 @@ export {
271269
uninstallApp,
272270
fetchInstalledApps,
273271
reinstallApp,
274-
sanitizePath
272+
sanitizePath,
275273
};

0 commit comments

Comments
 (0)