Skip to content

Commit 06dfb61

Browse files
authored
Merge pull request #160 from contentstack/fix/CS-42992
fix: handled login & region error, use manifest fle as default input
2 parents 4512d41 + 149afee commit 06dfb61

File tree

10 files changed

+108
-30
lines changed

10 files changed

+108
-30
lines changed

src/app-cli-base-coomand.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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 "./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+
public async init(): Promise<void> {
15+
await super.init();
16+
this.getManifestData();
17+
}
18+
19+
getManifestData() {
20+
this.manifestPath = resolve(process.cwd(), `${config.defaultAppFileName}.json`);
21+
if (existsSync(this.manifestPath)) {
22+
try {
23+
this.manifestData = JSON.parse(
24+
readFileSync(this.manifestPath, {
25+
encoding: "utf-8",
26+
})
27+
);
28+
} catch (error) {
29+
throw error;
30+
}
31+
}
32+
}
33+
}

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: 7 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,12 @@ 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.sharedConfig.org = this.manifestData?.organization_uid ?? (await getOrg(this.flags, {
2526
managementSdk: this.managementSdk,
2627
log: this.log,
27-
});
28+
}));
29+
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];
30+
2831
if (!this.flags["app-uid"]) {
2932
app = await getApp(this.flags, this.sharedConfig.org, {
3033
managementSdk: this.managementAppSdk,

src/commands/app/get.ts

Lines changed: 9 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,13 @@ 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.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];
36+
37+
this.sharedConfig.org = this.manifestData?.organization_uid ?? (await getOrg(this.flags, {
3538
managementSdk: this.managementSdk,
3639
log: this.log,
37-
});
40+
}));
41+
3842
if (!this.flags["app-uid"]) {
3943
appData = await getApp(this.flags, this.sharedConfig.org, {
4044
managementSdk: this.managementAppSdk,

src/commands/app/install.ts

Lines changed: 23 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,7 @@ export default class Install extends BaseCommand<typeof Install> {
3233
async run(): Promise<void> {
3334
try {
3435
let app, stack, appType;
36+
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"]; //manifest file first preference
3537

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

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

5155
// fetch app details
5256
if (!this.flags["app-uid"]) {
@@ -114,9 +118,22 @@ export default class Install extends BaseCommand<typeof Install> {
114118
}),
115119
"info"
116120
);
121+
122+
this.displayStackUrl();
117123
} catch (error: any) {
118124
this.log(error?.errorMessage || error?.message || error, "error");
119125
this.exit(1);
120126
}
121127
}
128+
129+
/**
130+
* @method displayStackUrl - show guid to stack after installing app successfully in the stack
131+
*/
132+
displayStackUrl(): void {
133+
const stackPath = `${this.uiHost}/#!/stack/${this.flags["stack-api-key"]}/dashboard`;
134+
this.log(
135+
`Start using the stack using the following url: ${stackPath}`,
136+
"info"
137+
);
138+
}
122139
}

src/commands/app/uninstall.ts

Lines changed: 7 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,10 @@ export default class Uninstall extends BaseCommand<typeof Uninstall> {
2829
async run(): Promise<void> {
2930
try {
3031
let app, appType
32+
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];
33+
3134
// get organization to be used
32-
this.sharedConfig.org = await getOrg(this.flags, {managementSdk: this.managementSdk, log: this.log});
35+
this.sharedConfig.org = this.manifestData?.organization_uid ?? (await getOrg(this.flags, {managementSdk: this.managementSdk, log: this.log}));
3336

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

src/commands/app/update.ts

Lines changed: 8 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,12 @@ 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+
//if working directory isn't app directory
32+
if(!this.manifestData){
33+
await this.validateManifest();
34+
}
35+
this.flags["app-manifest"] = this.manifestPath ?? this.flags["app-manifest"];
36+
this.orgUid = this.flags.org ?? this.manifestData?.organization_uid;
3537
this.sharedConfig.org = await getOrg(
3638
{ org: this.orgUid as any },
3739
{

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)