Skip to content

Commit a9539ba

Browse files
Merge branch 'development' of github.com:contentstack/contentstack-apps-cli into development
2 parents e088fdc + 6ebfd4f commit a9539ba

4 files changed

Lines changed: 111 additions & 46 deletions

File tree

src/commands/app/update.ts

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import pick from "lodash/pick";
22
import { resolve } from "path";
33
import merge from "lodash/merge";
44
import { flags } from "@contentstack/cli-utilities";
5+
import { App } from "@contentstack/management/types/app";
56
import { existsSync, readFileSync, writeFileSync } from "fs";
67

7-
import { getOrg } from "../../util";
88
import { AppManifest } from "../../types";
99
import { BaseCommand } from "./base-command";
10+
import { fetchApp, getApp, getOrg } from "../../util";
1011
import { $t, appUpdate, commonMsg } from "../../messages";
1112

1213
export default class Update extends BaseCommand<typeof Update> {
@@ -51,7 +52,10 @@ export default class Update extends BaseCommand<typeof Update> {
5152
}
5253

5354
try {
54-
await this.flagsPromptQueue();
55+
this.sharedConfig.org = await getOrg(this.flags, {
56+
log: this.log,
57+
managementSdk: this.managementSdk,
58+
});
5559
await this.validateManifest();
5660
await this.validateAppUid();
5761
await this.appVersionValidation();
@@ -63,54 +67,29 @@ export default class Update extends BaseCommand<typeof Update> {
6367
}
6468

6569
/**
66-
* @method flagsPromptQueue
70+
* @method validateManifest
6771
*
6872
* @return {*} {Promise<void>}
6973
* @memberof Create
7074
*/
71-
async flagsPromptQueue(): Promise<void> {
72-
const validate = (value: string) => {
73-
return (val: string) => {
74-
if (!val) return this.$t(this.messages.NOT_EMPTY, { value });
75-
return true;
76-
};
77-
};
78-
79-
if (!this.sharedConfig.orgValidated) {
80-
this.sharedConfig.org = await getOrg(this.flags, {
81-
log: this.log,
82-
managementSdk: this.managementSdk,
83-
});
84-
this.sharedConfig.orgValidated = true;
85-
}
86-
87-
if (!this.flags["app-uid"]) {
88-
this.flags["app-uid"] = (await this.getValPrompt({
89-
name: "appUid",
90-
validate: validate("App UID"),
91-
message: this.messages.APP_UID,
92-
})) as string;
93-
}
94-
75+
async validateManifest(): Promise<void> {
9576
if (!this.flags["app-manifest"]) {
9677
this.flags["app-manifest"] = (await this.getValPrompt({
9778
name: "appManifest",
98-
validate: validate("App manifest path"),
79+
validate: (val: string) => {
80+
if (!val)
81+
return this.$t(this.messages.NOT_EMPTY, {
82+
value: "App manifest path",
83+
});
84+
return true;
85+
},
9986
message: this.$t(this.messages.FILE_PATH, {
10087
fileName: "app manifest.json",
10188
}),
10289
})) as string;
10390
}
104-
}
10591

106-
/**
107-
* @method validateManifest
108-
*
109-
* @return {*} {Promise<void>}
110-
* @memberof Create
111-
*/
112-
async validateManifest(): Promise<void> {
113-
const manifestPath = this.flags["app-manifest"] as string;
92+
const manifestPath = this.flags["app-manifest"];
11493
let hasError = false;
11594
if (existsSync(manifestPath)) {
11695
try {
@@ -135,7 +114,6 @@ export default class Update extends BaseCommand<typeof Update> {
135114

136115
if (this.manifestPathRetry < 3) {
137116
this.flags["app-manifest"] = "";
138-
await this.flagsPromptQueue();
139117
await this.validateManifest();
140118
} else {
141119
this.log(this.messages.MAX_RETRY_LIMIT, "warn");
@@ -151,13 +129,28 @@ export default class Update extends BaseCommand<typeof Update> {
151129
* @memberof Create
152130
*/
153131
async validateAppUid(): Promise<void> {
132+
let appData;
133+
134+
if (!this.flags["app-uid"]) {
135+
appData = (await getApp(this.flags, this.sharedConfig.org, {
136+
managementSdk: this.managementAppSdk,
137+
log: this.log,
138+
})) as App;
139+
} else {
140+
appData = await fetchApp(this.flags, this.sharedConfig.org, {
141+
managementSdk: this.managementAppSdk,
142+
log: this.log,
143+
});
144+
}
145+
146+
this.flags["app-uid"] = appData.uid;
147+
154148
if (this.flags["app-uid"] !== this.manifestData?.uid) {
155149
this.log(this.messages.APP_UID_NOT_MATCH, "error");
156150
this.appUidRetry++;
157151

158152
if (this.appUidRetry < 3) {
159153
this.flags["app-uid"] = "";
160-
await this.flagsPromptQueue();
161154
await this.validateAppUid();
162155
} else {
163156
this.log(this.messages.MAX_RETRY_LIMIT, "warn");

src/config/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const config = {
1717
},
1818
appBoilerplateGithubUrl:
1919
"https://github.com/contentstack/marketplace-app-boilerplate/archive/refs/tags/1.2.3.zip",
20-
defaultAppFileName: "app-manifest",
20+
defaultAppFileName: "manifest",
2121
};
2222

2323
export default config;

test/unit/commands/app/update.test.ts

Lines changed: 68 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ describe("app:update", () => {
2626
.stub(cliux, "inquire", async (...args: any) => {
2727
const [prompt]: any = args;
2828
const cases = {
29-
appUid: "app-uid-1",
29+
App: "App 1",
3030
Organization: "test org 1",
3131
appManifest: "test-manifest",
3232
};
@@ -38,6 +38,13 @@ describe("app:update", () => {
3838
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")
3939
.reply(200, { organizations: mock.organizations })
4040
)
41+
.nock(`https://${developerHubBaseUrl}`, (api) =>
42+
api
43+
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
44+
.reply(200, {
45+
data: mock.apps,
46+
})
47+
)
4148
.nock(`https://${developerHubBaseUrl}`, (api) =>
4249
api.get("/manifests/app-uid-1").reply(200, {
4350
data: { ...manifestData, name: "test-app", version: 1 },
@@ -58,6 +65,7 @@ describe("app:update", () => {
5865
)
5966
.it("should update a app");
6067
});
68+
6169
describe("Update app with `--data-dir` flag", () => {
6270
test
6371
.stdout({ print: process.env.PRINT === "true" || false })
@@ -67,7 +75,7 @@ describe("app:update", () => {
6775
.stub(cliux, "inquire", async (...args: any) => {
6876
const [prompt]: any = args;
6977
const cases = {
70-
appUid: "app-uid-1",
78+
App: "App 1",
7179
Organization: "test org 1",
7280
appManifest: "test-manifest",
7381
};
@@ -79,6 +87,13 @@ describe("app:update", () => {
7987
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")
8088
.reply(200, { organizations: mock.organizations })
8189
)
90+
.nock(`https://${developerHubBaseUrl}`, (api) =>
91+
api
92+
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
93+
.reply(200, {
94+
data: mock.apps,
95+
})
96+
)
8297
.nock(`https://${developerHubBaseUrl}`, (api) =>
8398
api.get("/manifests/app-uid-1").reply(200, {
8499
data: { ...manifestData, name: "test-app", version: 1 },
@@ -99,6 +114,7 @@ describe("app:update", () => {
99114
)
100115
.it("should update a app");
101116
});
117+
102118
describe("Update app with wrong `manifest.json` path", () => {
103119
test
104120
.stdout({ print: process.env.PRINT === "true" || false })
@@ -125,6 +141,7 @@ describe("app:update", () => {
125141
.do(({ stdout }) => expect(stdout).to.contain(messages.MAX_RETRY_LIMIT))
126142
.it("should fail with manifest max retry message");
127143
});
144+
128145
describe("Update app with wrong `app-uid`", () => {
129146
test
130147
.stdout({ print: process.env.PRINT === "true" || false })
@@ -134,7 +151,7 @@ describe("app:update", () => {
134151
.stub(cliux, "inquire", async (...args: any) => {
135152
const [prompt]: any = args;
136153
const cases = {
137-
appUid: "app-uid-2",
154+
App: "App 2",
138155
Organization: "test org 1",
139156
appManifest: "test-manifest",
140157
};
@@ -146,6 +163,27 @@ describe("app:update", () => {
146163
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")
147164
.reply(200, { organizations: mock.organizations })
148165
)
166+
.nock(`https://${developerHubBaseUrl}`, (api) =>
167+
api
168+
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
169+
.reply(200, {
170+
data: mock.apps,
171+
})
172+
)
173+
.nock(`https://${developerHubBaseUrl}`, (api) =>
174+
api
175+
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
176+
.reply(200, {
177+
data: mock.apps,
178+
})
179+
)
180+
.nock(`https://${developerHubBaseUrl}`, (api) =>
181+
api
182+
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
183+
.reply(200, {
184+
data: mock.apps,
185+
})
186+
)
149187
.command([
150188
"app:update",
151189
"--app-manifest",
@@ -155,6 +193,7 @@ describe("app:update", () => {
155193
.do(({ stdout }) => expect(stdout).to.contain(messages.MAX_RETRY_LIMIT))
156194
.it("should fail with max retry message");
157195
});
196+
158197
describe("Update app with wrong `app version`", () => {
159198
test
160199
.stdout({ print: process.env.PRINT === "true" || false })
@@ -164,7 +203,7 @@ describe("app:update", () => {
164203
.stub(cliux, "inquire", async (...args: any) => {
165204
const [prompt]: any = args;
166205
const cases = {
167-
appUid: "app-uid-1",
206+
App: "App 1",
168207
Organization: "test org 1",
169208
appManifest: "test-manifest",
170209
};
@@ -176,6 +215,13 @@ describe("app:update", () => {
176215
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")
177216
.reply(200, { organizations: mock.organizations })
178217
)
218+
.nock(`https://${developerHubBaseUrl}`, (api) =>
219+
api
220+
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
221+
.reply(200, {
222+
data: mock.apps,
223+
})
224+
)
179225
.nock(`https://${developerHubBaseUrl}`, (api) =>
180226
api.get("/manifests/app-uid-1").reply(200, {
181227
data: { ...manifestData, name: "test-app", version: 2 },
@@ -192,6 +238,7 @@ describe("app:update", () => {
192238
)
193239
.it("should fail with version miss match error message");
194240
});
241+
195242
describe("Update app wrong app-uid API failure", () => {
196243
test
197244
.stdout({ print: process.env.PRINT === "true" || false })
@@ -201,7 +248,7 @@ describe("app:update", () => {
201248
.stub(cliux, "inquire", async (...args: any) => {
202249
const [prompt]: any = args;
203250
const cases = {
204-
appUid: "app-uid-1",
251+
App: "App 1",
205252
Organization: "test org 1",
206253
appManifest: "test-manifest",
207254
};
@@ -213,6 +260,13 @@ describe("app:update", () => {
213260
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")
214261
.reply(200, { organizations: mock.organizations })
215262
)
263+
.nock(`https://${developerHubBaseUrl}`, (api) =>
264+
api
265+
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
266+
.reply(200, {
267+
data: mock.apps,
268+
})
269+
)
216270
.nock(`https://${developerHubBaseUrl}`, (api) =>
217271
api.get("/manifests/app-uid-1").reply(200, {
218272
data: { ...manifestData, name: "test-app", version: 1 },
@@ -232,6 +286,7 @@ describe("app:update", () => {
232286
.do(({ stdout }) => expect(stdout).to.contain(messages.INVALID_APP_ID))
233287
.it("update app should fail with 400 status code");
234288
});
289+
235290
describe("Update app wrong org-uid API failure", () => {
236291
test
237292
.stdout({ print: process.env.PRINT === "true" || false })
@@ -241,7 +296,7 @@ describe("app:update", () => {
241296
.stub(cliux, "inquire", async (...args: any) => {
242297
const [prompt]: any = args;
243298
const cases = {
244-
appUid: "app-uid-1",
299+
App: "App 1",
245300
Organization: "test org 1",
246301
appManifest: "test-manifest",
247302
};
@@ -253,6 +308,13 @@ describe("app:update", () => {
253308
.get("/v3/organizations?limit=100&asc=name&include_count=true&skip=0")
254309
.reply(200, { organizations: mock.organizations })
255310
)
311+
.nock(`https://${developerHubBaseUrl}`, (api) =>
312+
api
313+
.get("/manifests?limit=50&asc=name&include_count=true&skip=0")
314+
.reply(200, {
315+
data: mock.apps,
316+
})
317+
)
256318
.nock(`https://${developerHubBaseUrl}`, (api) =>
257319
api.get("/manifests/app-uid-1").reply(200, {
258320
data: { ...manifestData, name: "test-app", version: 1 },

test/unit/mock/common.mock.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,15 @@
88
"uid": "test-uid-2",
99
"name": "test org 2"
1010
}
11+
],
12+
"apps": [
13+
{
14+
"uid": "app-uid-1",
15+
"name": "App 1"
16+
},
17+
{
18+
"uid": "app-uid-2",
19+
"name": "App 2"
20+
}
1121
]
1222
}

0 commit comments

Comments
 (0)