Skip to content

Commit 2c4fbd2

Browse files
authored
Merge pull request #224 from contentstack/feat/DX-540-reinstall-command-implementation
dx-540 | reinstall command implementation
2 parents 5674a1a + bc39ece commit 2c4fbd2

File tree

11 files changed

+315
-20
lines changed

11 files changed

+315
-20
lines changed

README.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ $ npm install -g @contentstack/apps-cli
2020
$ csdx COMMAND
2121
running command...
2222
$ csdx (--version|-v)
23-
@contentstack/apps-cli/1.0.7 darwin-arm64 node-v18.16.0
23+
@contentstack/apps-cli/1.1.0 darwin-arm64 node-v18.16.0
2424
$ csdx --help [COMMAND]
2525
USAGE
2626
$ csdx COMMAND
@@ -35,6 +35,7 @@ USAGE
3535
* [`csdx app:delete`](#csdx-appdelete)
3636
* [`csdx app:get`](#csdx-appget)
3737
* [`csdx app:install`](#csdx-appinstall)
38+
* [`csdx app:reinstall`](#csdx-appreinstall)
3839
* [`csdx app:uninstall`](#csdx-appuninstall)
3940
* [`csdx app:update`](#csdx-appupdate)
4041

@@ -61,9 +62,11 @@ EXAMPLES
6162
$ csdx app:install
6263
6364
$ csdx app:uninstall
65+
66+
$ csdx app:reinstall
6467
```
6568

66-
_See code: [src/commands/app/index.ts](https://github.com/contentstack/apps-cli/blob/v1.0.7/src/commands/app/index.ts)_
69+
_See code: [src/commands/app/index.ts](https://github.com/contentstack/apps-cli/blob/v1.1.0/src/commands/app/index.ts)_
6770

6871
## `csdx app:create`
6972

@@ -94,7 +97,7 @@ EXAMPLES
9497
$ csdx app:create --name App-3 --app-type organization --org <UID> -d ./boilerplate -c ./external-config.json
9598
```
9699

97-
_See code: [src/commands/app/create.ts](https://github.com/contentstack/apps-cli/blob/v1.0.7/src/commands/app/create.ts)_
100+
_See code: [src/commands/app/create.ts](https://github.com/contentstack/apps-cli/blob/v1.1.0/src/commands/app/create.ts)_
98101

99102
## `csdx app:delete`
100103

@@ -119,7 +122,7 @@ EXAMPLES
119122
$ csdx app:delete --app-uid <value> --org <value> -d ./boilerplate
120123
```
121124

122-
_See code: [src/commands/app/delete.ts](https://github.com/contentstack/apps-cli/blob/v1.0.7/src/commands/app/delete.ts)_
125+
_See code: [src/commands/app/delete.ts](https://github.com/contentstack/apps-cli/blob/v1.1.0/src/commands/app/delete.ts)_
123126

124127
## `csdx app:get`
125128

@@ -149,7 +152,7 @@ EXAMPLES
149152
$ csdx app:get --org <value> --app-uid <value> --app-type organization
150153
```
151154

152-
_See code: [src/commands/app/get.ts](https://github.com/contentstack/apps-cli/blob/v1.0.7/src/commands/app/get.ts)_
155+
_See code: [src/commands/app/get.ts](https://github.com/contentstack/apps-cli/blob/v1.1.0/src/commands/app/get.ts)_
153156

154157
## `csdx app:install`
155158

@@ -175,7 +178,33 @@ EXAMPLES
175178
$ csdx app:install --org <UID> --app-uid <APP-UID-1> --stack-api-key <STACK-API-KEY-1>
176179
```
177180

178-
_See code: [src/commands/app/install.ts](https://github.com/contentstack/apps-cli/blob/v1.0.7/src/commands/app/install.ts)_
181+
_See code: [src/commands/app/install.ts](https://github.com/contentstack/apps-cli/blob/v1.1.0/src/commands/app/install.ts)_
182+
183+
## `csdx app:reinstall`
184+
185+
Reinstall an app from the marketplace
186+
187+
```
188+
USAGE
189+
$ csdx app:reinstall [--org <value>] [--app-uid <value>] [--stack-api-key <value>]
190+
191+
FLAGS
192+
--app-uid=<value> Provide the app UID of an existing app.
193+
--org=<value> Provide the organization UID to fetch the app details for the desired operation.
194+
--stack-api-key=<value> API key of the stack where the app is to be installed.
195+
196+
DESCRIPTION
197+
Reinstall an app from the marketplace
198+
199+
EXAMPLES
200+
$ csdx app:reinstall
201+
202+
$ csdx app:reinstall --org <UID> --app-uid <APP-UID-1>
203+
204+
$ csdx app:reinstall --org <UID> --app-uid <APP-UID-1> --stack-api-key <STACK-API-KEY-1>
205+
```
206+
207+
_See code: [src/commands/app/reinstall.ts](https://github.com/contentstack/apps-cli/blob/v1.1.0/src/commands/app/reinstall.ts)_
179208

180209
## `csdx app:uninstall`
181210

@@ -202,7 +231,7 @@ EXAMPLES
202231
$ csdx app:uninstall --org <UID> --app-uid <APP-UID-1> --installation-uid <INSTALLATION-UID-1>
203232
```
204233

205-
_See code: [src/commands/app/uninstall.ts](https://github.com/contentstack/apps-cli/blob/v1.0.7/src/commands/app/uninstall.ts)_
234+
_See code: [src/commands/app/uninstall.ts](https://github.com/contentstack/apps-cli/blob/v1.1.0/src/commands/app/uninstall.ts)_
206235

207236
## `csdx app:update`
208237

@@ -225,5 +254,5 @@ EXAMPLES
225254
$ csdx app:update --app-manifest ./boilerplate/manifest.json
226255
```
227256

228-
_See code: [src/commands/app/update.ts](https://github.com/contentstack/apps-cli/blob/v1.0.7/src/commands/app/update.ts)_
257+
_See code: [src/commands/app/update.ts](https://github.com/contentstack/apps-cli/blob/v1.1.0/src/commands/app/update.ts)_
229258
<!-- commandsstop -->

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/apps-cli",
3-
"version": "1.0.7",
3+
"version": "1.1.0",
44
"description": "App ClI",
55
"author": "Contentstack CLI",
66
"homepage": "https://github.com/contentstack/contentstack-apps-cli",
@@ -96,7 +96,8 @@
9696
"app:update": "APUPT",
9797
"app:delete": "APDLT",
9898
"app:install": "API",
99-
"app:uninstall": "APUI"
99+
"app:uninstall": "APUI",
100+
"app:reinstall": "APRI"
100101
}
101102
}
102103
}

src/commands/app/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export default class App extends Command {
1414
"$ <%= config.bin %> <%= command.id %>:delete",
1515
"$ <%= config.bin %> <%= command.id %>:install",
1616
"$ <%= config.bin %> <%= command.id %>:uninstall",
17+
"$ <%= config.bin %> <%= command.id %>:reinstall",
1718
];
1819

1920
async run(): Promise<void> {

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: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import { cliux, flags } from "@contentstack/cli-utilities";
2+
import { AppCLIBaseCommand } from "../../app-cli-base-coomand";
3+
import { $t, commonMsg, reinstallAppMsg } from "../../messages";
4+
import {
5+
getOrg,
6+
getApp,
7+
getStack,
8+
reinstallApp,
9+
fetchApp,
10+
fetchStack,
11+
} from "../../util";
12+
13+
export default class Reinstall extends AppCLIBaseCommand {
14+
static description: string | undefined =
15+
"Reinstall an app from the marketplace";
16+
17+
static examples = [
18+
"$ <%= config.bin %> <%= command.id %>",
19+
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1>",
20+
"$ <%= config.bin %> <%= command.id %> --org <UID> --app-uid <APP-UID-1> --stack-api-key <STACK-API-KEY-1>",
21+
];
22+
23+
static flags = {
24+
"app-uid": flags.string({
25+
description: commonMsg.APP_UID,
26+
}),
27+
"stack-api-key": flags.string({
28+
description: commonMsg.STACK_API_KEY,
29+
}),
30+
};
31+
32+
async run(): Promise<void> {
33+
try {
34+
let app, stack, appType;
35+
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];
36+
37+
if (this.flags["stack-api-key"]) {
38+
stack = await fetchStack(this.flags, {
39+
managementSdk: this.managementSdk,
40+
log: this.log,
41+
});
42+
}
43+
44+
this.sharedConfig.org =
45+
this.manifestData?.organization_uid ??
46+
(await getOrg(this.flags, {
47+
managementSdk: this.managementSdk,
48+
log: this.log,
49+
}));
50+
51+
if (!this.flags["app-uid"]) {
52+
app = await getApp(this.flags, this.sharedConfig.org, {
53+
managementSdk: this.managementAppSdk,
54+
log: this.log,
55+
});
56+
} else {
57+
app = await fetchApp(this.flags, this.sharedConfig.org, {
58+
managementSdk: this.managementAppSdk,
59+
log: this.log,
60+
});
61+
}
62+
appType = app?.["target_type"];
63+
this.flags["app-uid"] = app?.uid;
64+
65+
if (appType === "organization" && this.flags["stack-api-key"]) {
66+
appType = "organization";
67+
const confirmation =
68+
this.flags["yes"] ||
69+
(await cliux.inquire({
70+
type: "confirm",
71+
message: $t(reinstallAppMsg.REINSTALL_ORG_APP_TO_STACK, {
72+
app: app?.name || app?.uid,
73+
}),
74+
name: "confirmation",
75+
}));
76+
if (!confirmation) {
77+
throw new Error(commonMsg.USER_TERMINATION);
78+
}
79+
}
80+
81+
if (appType === "stack" && !this.flags["stack-api-key"]) {
82+
appType = "stack";
83+
84+
this.log(
85+
$t(reinstallAppMsg.MISSING_STACK_API_KEY, {
86+
app: app?.name || app?.uid,
87+
}),
88+
"warn"
89+
);
90+
stack = await getStack(this.sharedConfig.org, {
91+
managementSdk: this.managementSdk,
92+
log: this.log,
93+
});
94+
this.flags["stack-api-key"] = stack?.["api_key"];
95+
}
96+
97+
this.log(
98+
$t(reinstallAppMsg.REINSTALLING_APP_NOTICE, {
99+
app: app?.name || app?.uid,
100+
type: appType,
101+
target: this.flags["stack-api-key"] || this.sharedConfig.org,
102+
}),
103+
"info"
104+
);
105+
await reinstallApp({
106+
flags: this.flags,
107+
type: appType,
108+
developerHubBaseUrl: this.developerHubBaseUrl,
109+
orgUid: this.sharedConfig.org,
110+
manifestUid: this.manifestData.uid,
111+
});
112+
this.log(
113+
$t(reinstallAppMsg.APP_REINSTALLED_SUCCESSFULLY, {
114+
app: app?.name || (this.flags["app-uid"] as string),
115+
target: stack?.name || this.sharedConfig.org,
116+
}),
117+
"info"
118+
);
119+
120+
this.displayStackUrl();
121+
} catch (error: any) {
122+
this.log(error?.errorMessage || error?.message || error, "error");
123+
this.exit(1);
124+
}
125+
}
126+
127+
displayStackUrl(): void {
128+
const stackPath = `${this.uiHost}/#!/stack/${this.flags["stack-api-key"]}/dashboard`;
129+
this.log(
130+
`Please use the following URL to start using the stack: ${stackPath}`,
131+
"info"
132+
);
133+
}
134+
}

src/messages/index.ts

Lines changed: 15 additions & 3 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 = {
@@ -100,22 +101,33 @@ const uninstallAppMsg = {
100101
UNINSTALL_ALL: "Please select stacks from where the app must be uninstalled.",
101102
}
102103

104+
const reinstallAppMsg = {
105+
CHOOSE_A_STACK: "Please select a stack",
106+
APP_REINSTALLED_SUCCESSFULLY: "{app} reinstalled successfully in {target}.",
107+
REINSTALL_ORG_APP_TO_STACK: "{app} is an organization app. It cannot be reinstalled to a stack. Do you want to proceed?",
108+
MISSING_STACK_API_KEY: "As {app} is a stack app, it can only be reinstalled in a stack. Please select a stack.",
109+
REINSTALLING_APP_NOTICE: "Reinstalling {app} on {type} {target}.",
110+
APP_UID: "Provide the app UID of an existing app to be reinstalled.",
111+
}
112+
103113
const messages: typeof errors &
104114
typeof commonMsg &
105115
typeof appCreate &
106116
typeof appUpdate &
107117
typeof getAppMsg &
108118
typeof deleteAppMsg &
109119
typeof installAppMsg &
110-
typeof uninstallAppMsg = {
120+
typeof uninstallAppMsg &
121+
typeof reinstallAppMsg = {
111122
...errors,
112123
...commonMsg,
113124
...appCreate,
114125
...appUpdate,
115126
...getAppMsg,
116127
...deleteAppMsg,
117128
...installAppMsg,
118-
...uninstallAppMsg
129+
...uninstallAppMsg,
130+
...reinstallAppMsg
119131
};
120132

121133
const $t = (msg: string, args: Record<string, string>): string => {
@@ -130,4 +142,4 @@ const $t = (msg: string, args: Record<string, string>): string => {
130142
};
131143

132144
export default messages;
133-
export { $t, errors, commonMsg, appCreate, appUpdate, getAppMsg, deleteAppMsg, installAppMsg, uninstallAppMsg };
145+
export { $t, errors, commonMsg, appCreate, appUpdate, getAppMsg, deleteAppMsg, installAppMsg, uninstallAppMsg, reinstallAppMsg };

src/types/app.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { FlagInput } from "@contentstack/cli-utilities";
2+
import { ConfigType } from "./utils";
3+
14
export interface TokenConfiguration {
25
enabled?: boolean;
36
scopes?: string[];
@@ -113,3 +116,12 @@ export interface AppManifest {
113116
export interface AppManifestWithUiLocation extends AppManifest {
114117
ui_location: LocationConfiguration;
115118
}
119+
120+
export interface ReinstallParams {
121+
flags: FlagInput,
122+
type: string,
123+
orgUid: string,
124+
manifestUid: string,
125+
configType: ConfigType,
126+
developerHubBaseUrl: string
127+
};

0 commit comments

Comments
 (0)