Skip to content

Commit 21feba6

Browse files
committed
fix: handled login & region error, use manifest fle as default input
1 parent 4512d41 commit 21feba6

File tree

10 files changed

+116
-30
lines changed

10 files changed

+116
-30
lines changed

src/app-cli-base-coomand.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { resolve } from "path";
2+
import { existsSync, readFileSync } from "fs";
3+
4+
import config from "./config";
5+
import { AppManifest } from "./types";
6+
import { BaseCommand } from "./commands/app/base-command";
7+
8+
export abstract class AppCLIBaseCommand extends BaseCommand<
9+
typeof AppCLIBaseCommand
10+
> {
11+
protected manifestPath!: string;
12+
protected manifestData!: AppManifest & Record<string, any>;
13+
14+
/**
15+
* The `start` function call getManifestData which reads manifest file is current working directory is app directory
16+
*/
17+
start() {
18+
this.getManifestData();
19+
}
20+
21+
//move this into abstract command
22+
getManifestData() {
23+
this.manifestPath = resolve(process.cwd(), `${config.defaultAppFileName}.json`);
24+
if (existsSync(this.manifestPath)) {
25+
try {
26+
this.manifestData = JSON.parse(
27+
readFileSync(this.manifestPath, {
28+
encoding: "utf-8",
29+
})
30+
);
31+
} catch (error) {
32+
throw error;
33+
}
34+
}
35+
}
36+
}

src/base-command.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
managementSDKInitiator,
1313
InquirePayload,
1414
cliux,
15+
isAuthenticated,
1516
} from "@contentstack/cli-utilities";
1617

1718
import config from "./config";
@@ -67,6 +68,7 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
6768

6869
ux.registerSearchPlugin();
6970
this.registerConfig();
71+
this.validateRegionAndAuth();
7072

7173
this.developerHubBaseUrl =
7274
this.sharedConfig.developerHubBaseUrl || (await getDeveloperHubUrl());
@@ -154,4 +156,17 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
154156
message: message as string,
155157
});
156158
}
159+
160+
/**
161+
* The `validateRegionAndAuth` function verify whether region is set and user is logged in or not
162+
*/
163+
validateRegionAndAuth() {
164+
//Step1: check region
165+
if (this.region) {
166+
//Step2: user logged in or not
167+
if (!isAuthenticated()) {
168+
throw new Error(this.messages.CLI_APP_CLI_LOGIN_FAILED);
169+
}
170+
}
171+
}
157172
}

src/commands/app/create.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
writeFileSync,
1515
createWriteStream,
1616
} from "fs";
17-
import { ux, cliux, flags, HttpClient } from "@contentstack/cli-utilities";
17+
import { ux, cliux, flags, HttpClient, configHandler } from "@contentstack/cli-utilities";
1818

1919
import { BaseCommand } from "../../base-command";
2020
import { AppManifest, AppType } from "../../types";
@@ -132,10 +132,11 @@ export default class Create extends BaseCommand<typeof Create> {
132132
);
133133
}
134134

135-
this.sharedConfig.org = await getOrg(this.flags, {
135+
//Auto select org in case of oauth
136+
this.sharedConfig.org = configHandler.get('oauthOrgUid') ?? (await getOrg(this.flags, {
136137
log: this.log,
137138
managementSdk: this.managementSdk,
138-
});
139+
}));
139140
}
140141

141142
/**

src/commands/app/delete.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { BaseCommand } from "../../base-command";
21
import { cliux, flags } from "@contentstack/cli-utilities";
2+
3+
import {AppCLIBaseCommand} from "../../app-cli-base-coomand";
34
import { $t, commonMsg, deleteAppMsg } from "../../messages";
45
import { getOrg, fetchAppInstallations, deleteApp, getApp } from "../../util";
56

6-
export default class Delete extends BaseCommand<typeof Delete> {
7+
export default class Delete extends AppCLIBaseCommand {
78
static description = "Delete app from marketplace";
89

910
static examples = [
@@ -21,10 +22,13 @@ export default class Delete extends BaseCommand<typeof Delete> {
2122
async run(): Promise<void> {
2223
try {
2324
let app;
24-
this.sharedConfig.org = await getOrg(this.flags, {
25+
this.start();
26+
this.sharedConfig.org = this.manifestData?.organization_uid ?? (await getOrg(this.flags, {
2527
managementSdk: this.managementSdk,
2628
log: this.log,
27-
});
29+
}));
30+
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];
31+
2832
if (!this.flags["app-uid"]) {
2933
app = await getApp(this.flags, this.sharedConfig.org, {
3034
managementSdk: this.managementAppSdk,

src/commands/app/get.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { BaseCommand } from "../../base-command";
2-
import { getOrg, getApp, writeFile, fetchApp } from "../../util";
31
import { flags } from "@contentstack/cli-utilities";
2+
43
import { commonMsg } from "../../messages";
4+
import {AppCLIBaseCommand} from "../../app-cli-base-coomand";
5+
import { getOrg, getApp, writeFile, fetchApp } from "../../util";
56

6-
export default class Get extends BaseCommand<typeof Get> {
7+
export default class Get extends AppCLIBaseCommand {
78
static description = "Get details of an app in developer hub";
89

910
static examples = [
@@ -31,10 +32,14 @@ export default class Get extends BaseCommand<typeof Get> {
3132
async run(): Promise<void> {
3233
try {
3334
let appData;
34-
this.sharedConfig.org = await getOrg(this.flags, {
35+
this.start();
36+
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];
37+
38+
this.sharedConfig.org = this.manifestData?.organization_uid ?? (await getOrg(this.flags, {
3539
managementSdk: this.managementSdk,
3640
log: this.log,
37-
});
41+
}));
42+
3843
if (!this.flags["app-uid"]) {
3944
appData = await getApp(this.flags, this.sharedConfig.org, {
4045
managementSdk: this.managementAppSdk,

src/commands/app/install.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { BaseCommand } from "../../base-command";
21
import { cliux, flags } from "@contentstack/cli-utilities";
2+
3+
import { AppCLIBaseCommand } from "../../app-cli-base-coomand";
34
import { $t, commonMsg, installAppMsg } from "../../messages";
45
import {
56
getOrg,
@@ -10,7 +11,7 @@ import {
1011
fetchStack,
1112
} from "../../util";
1213

13-
export default class Install extends BaseCommand<typeof Install> {
14+
export default class Install extends AppCLIBaseCommand {
1415
static description: string | undefined =
1516
"Install an app from the marketplace";
1617

@@ -32,6 +33,8 @@ export default class Install extends BaseCommand<typeof Install> {
3233
async run(): Promise<void> {
3334
try {
3435
let app, stack, appType;
36+
this.start();
37+
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"]; //manifest file first preference
3538

3639
// validating user given stack, as installation API doesn't return appropriate errors if stack-api-key is invalid
3740
// validating this first, as orgUid is not required for fetching stack
@@ -43,10 +46,12 @@ export default class Install extends BaseCommand<typeof Install> {
4346
}
4447

4548
// get organization to be used
46-
this.sharedConfig.org = await getOrg(this.flags, {
47-
managementSdk: this.managementSdk,
48-
log: this.log,
49-
});
49+
this.sharedConfig.org =
50+
this.manifestData?.organization_uid ??
51+
(await getOrg(this.flags, {
52+
managementSdk: this.managementSdk,
53+
log: this.log,
54+
}));
5055

5156
// fetch app details
5257
if (!this.flags["app-uid"]) {
@@ -114,9 +119,22 @@ export default class Install extends BaseCommand<typeof Install> {
114119
}),
115120
"info"
116121
);
122+
123+
this.displayStackUrl();
117124
} catch (error: any) {
118125
this.log(error?.errorMessage || error?.message || error, "error");
119126
this.exit(1);
120127
}
121128
}
129+
130+
/**
131+
* @method displayStackUrl - show guid to stack after installing app successfully in the stack
132+
*/
133+
displayStackUrl(): void {
134+
const stackPath = `${this.uiHost}/#!/stack/${this.flags["stack-api-key"]}/dashboard`;
135+
this.log(
136+
`Start using the stack using the following url: ${stackPath}`,
137+
"info"
138+
);
139+
}
122140
}

src/commands/app/uninstall.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import { BaseCommand } from "../../base-command";
21
import { flags } from "@contentstack/cli-utilities";
3-
import { getOrg, fetchApp, getInstalledApps } from "../../util";
2+
43
import { commonMsg, uninstallAppMsg } from "../../messages";
4+
import {AppCLIBaseCommand} from "../../app-cli-base-coomand";
5+
import { getOrg, fetchApp, getInstalledApps } from "../../util";
56
import { UninstallAppFactory } from "../../factories/uninstall-app-factory";
67

7-
export default class Uninstall extends BaseCommand<typeof Uninstall> {
8+
export default class Uninstall extends AppCLIBaseCommand {
89
static description = "Uninstall an app";
910

1011
static examples = [
@@ -28,8 +29,11 @@ export default class Uninstall extends BaseCommand<typeof Uninstall> {
2829
async run(): Promise<void> {
2930
try {
3031
let app, appType
32+
this.start();
33+
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];
34+
3135
// get organization to be used
32-
this.sharedConfig.org = await getOrg(this.flags, {managementSdk: this.managementSdk, log: this.log});
36+
this.sharedConfig.org = this.manifestData?.organization_uid ?? (await getOrg(this.flags, {managementSdk: this.managementSdk, log: this.log}));
3337

3438
// fetch app details
3539
if (!this.flags['app-uid']) {

src/commands/app/update.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@ import { flags } from "@contentstack/cli-utilities";
55
import { App } from "@contentstack/management/types/app";
66
import { existsSync, readFileSync, writeFileSync } from "fs";
77

8-
import { AppManifest } from "../../types";
9-
import { BaseCommand } from "../../base-command";
108
import { $t, appUpdate } from "../../messages";
119
import { fetchApp, getApp, getOrg } from "../../util";
10+
import {AppCLIBaseCommand} from "../../app-cli-base-coomand";
1211

13-
export default class Update extends BaseCommand<typeof Update> {
12+
export default class Update extends AppCLIBaseCommand {
1413
private orgUid!: string;
1514
private manifestPathRetry: number = 0;
16-
private manifestData!: AppManifest & Record<string, any>;
1715

1816
static description = "Update the existing app in developer hub";
1917

@@ -30,8 +28,13 @@ export default class Update extends BaseCommand<typeof Update> {
3028

3129
async run(): Promise<void> {
3230
try {
33-
await this.validateManifest();
34-
this.orgUid = this.flags.org || this.manifestData.organization_uid;
31+
this.start();
32+
//if working directory isn't app directory
33+
if(!this.manifestData){
34+
await this.validateManifest();
35+
}
36+
this.flags["app-manifest"] = this.manifestPath ?? this.flags["app-manifest"];
37+
this.orgUid = this.flags.org ?? this.manifestData?.organization_uid;
3538
this.sharedConfig.org = await getOrg(
3639
{ org: this.orgUid as any },
3740
{

src/messages/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ const commonMsg = {
3434
APP_TYPE_DESCRIPTION: "Type of App",
3535
CONTACT_SUPPORT: "Please contact the support team.",
3636
STACK_API_KEY: "API key of the stack where the app is to be installed.",
37-
USER_TERMINATION: "Process terminated by the user."
37+
USER_TERMINATION: "Process terminated by the user.",
38+
CLI_APP_CLI_LOGIN_FAILED: 'You are not logged in. Please login with command $ csdx auth:login'
3839
};
3940

4041
const appCreate = {

src/util/common-utils.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ async function fetchApps(
6767
.catch((error) => {
6868
cliux.loader("failed");
6969
log("Some error occurred while fetching apps.", "warn");
70-
log(error.errorMessage, "error");
7170
throw error;
7271
});
7372

0 commit comments

Comments
 (0)