Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/app-cli-base-coomand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { resolve } from "path";
import { existsSync, readFileSync } from "fs";

import config from "./config";
import { AppManifest } from "./types";
import { BaseCommand } from "./base-command";

export abstract class AppCLIBaseCommand extends BaseCommand<
typeof AppCLIBaseCommand
> {
protected manifestPath!: string;
protected manifestData!: AppManifest & Record<string, any>;

public async init(): Promise<void> {
await super.init();
this.getManifestData();
}

getManifestData() {
this.manifestPath = resolve(process.cwd(), `${config.defaultAppFileName}.json`);
if (existsSync(this.manifestPath)) {
try {
this.manifestData = JSON.parse(
readFileSync(this.manifestPath, {
encoding: "utf-8",
})
);
} catch (error) {
throw error;
}
}
}
}
15 changes: 15 additions & 0 deletions src/base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
managementSDKInitiator,
InquirePayload,
cliux,
isAuthenticated,
} from "@contentstack/cli-utilities";

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

ux.registerSearchPlugin();
this.registerConfig();
this.validateRegionAndAuth();

this.developerHubBaseUrl =
this.sharedConfig.developerHubBaseUrl || (await getDeveloperHubUrl());
Expand Down Expand Up @@ -154,4 +156,17 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
message: message as string,
});
}

/**
* The `validateRegionAndAuth` function verify whether region is set and user is logged in or not
*/
validateRegionAndAuth() {
//Step1: check region
if (this.region) {
//Step2: user logged in or not
if (!isAuthenticated()) {
throw new Error(this.messages.CLI_APP_CLI_LOGIN_FAILED);
}
}
}
}
7 changes: 4 additions & 3 deletions src/commands/app/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
writeFileSync,
createWriteStream,
} from "fs";
import { ux, cliux, flags, HttpClient } from "@contentstack/cli-utilities";
import { ux, cliux, flags, HttpClient, configHandler } from "@contentstack/cli-utilities";

import { BaseCommand } from "../../base-command";
import { AppManifest, AppType } from "../../types";
Expand Down Expand Up @@ -132,10 +132,11 @@ export default class Create extends BaseCommand<typeof Create> {
);
}

this.sharedConfig.org = await getOrg(this.flags, {
//Auto select org in case of oauth
this.sharedConfig.org = configHandler.get('oauthOrgUid') ?? (await getOrg(this.flags, {
log: this.log,
managementSdk: this.managementSdk,
});
}));
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/commands/app/delete.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { BaseCommand } from "../../base-command";
import { cliux, flags } from "@contentstack/cli-utilities";

import {AppCLIBaseCommand} from "../../app-cli-base-coomand";
import { $t, commonMsg, deleteAppMsg } from "../../messages";
import { getOrg, fetchAppInstallations, deleteApp, getApp } from "../../util";

export default class Delete extends BaseCommand<typeof Delete> {
export default class Delete extends AppCLIBaseCommand {
static description = "Delete app from marketplace";

static examples = [
Expand All @@ -21,10 +22,12 @@ export default class Delete extends BaseCommand<typeof Delete> {
async run(): Promise<void> {
try {
let app;
this.sharedConfig.org = await getOrg(this.flags, {
this.sharedConfig.org = this.manifestData?.organization_uid ?? (await getOrg(this.flags, {
managementSdk: this.managementSdk,
log: this.log,
});
}));
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];

if (!this.flags["app-uid"]) {
app = await getApp(this.flags, this.sharedConfig.org, {
managementSdk: this.managementAppSdk,
Expand Down
14 changes: 9 additions & 5 deletions src/commands/app/get.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { BaseCommand } from "../../base-command";
import { getOrg, getApp, writeFile, fetchApp } from "../../util";
import { flags } from "@contentstack/cli-utilities";

import { commonMsg } from "../../messages";
import {AppCLIBaseCommand} from "../../app-cli-base-coomand";
import { getOrg, getApp, writeFile, fetchApp } from "../../util";

export default class Get extends BaseCommand<typeof Get> {
export default class Get extends AppCLIBaseCommand {
static description = "Get details of an app in developer hub";

static examples = [
Expand Down Expand Up @@ -31,10 +32,13 @@ export default class Get extends BaseCommand<typeof Get> {
async run(): Promise<void> {
try {
let appData;
this.sharedConfig.org = await getOrg(this.flags, {
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];

this.sharedConfig.org = this.manifestData?.organization_uid ?? (await getOrg(this.flags, {
managementSdk: this.managementSdk,
log: this.log,
});
}));

if (!this.flags["app-uid"]) {
appData = await getApp(this.flags, this.sharedConfig.org, {
managementSdk: this.managementAppSdk,
Expand Down
29 changes: 23 additions & 6 deletions src/commands/app/install.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseCommand } from "../../base-command";
import { cliux, flags } from "@contentstack/cli-utilities";

import { AppCLIBaseCommand } from "../../app-cli-base-coomand";
import { $t, commonMsg, installAppMsg } from "../../messages";
import {
getOrg,
Expand All @@ -10,7 +11,7 @@ import {
fetchStack,
} from "../../util";

export default class Install extends BaseCommand<typeof Install> {
export default class Install extends AppCLIBaseCommand {
static description: string | undefined =
"Install an app from the marketplace";

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

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

// get organization to be used
this.sharedConfig.org = await getOrg(this.flags, {
managementSdk: this.managementSdk,
log: this.log,
});
this.sharedConfig.org =
this.manifestData?.organization_uid ??
(await getOrg(this.flags, {
managementSdk: this.managementSdk,
log: this.log,
}));

// fetch app details
if (!this.flags["app-uid"]) {
Expand Down Expand Up @@ -114,9 +118,22 @@ export default class Install extends BaseCommand<typeof Install> {
}),
"info"
);

this.displayStackUrl();
} catch (error: any) {
this.log(error?.errorMessage || error?.message || error, "error");
this.exit(1);
}
}

/**
* @method displayStackUrl - show guid to stack after installing app successfully in the stack
*/
displayStackUrl(): void {
const stackPath = `${this.uiHost}/#!/stack/${this.flags["stack-api-key"]}/dashboard`;
this.log(
`Start using the stack using the following url: ${stackPath}`,
"info"
);
}
}
11 changes: 7 additions & 4 deletions src/commands/app/uninstall.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { BaseCommand } from "../../base-command";
import { flags } from "@contentstack/cli-utilities";
import { getOrg, fetchApp, getInstalledApps } from "../../util";

import { commonMsg, uninstallAppMsg } from "../../messages";
import {AppCLIBaseCommand} from "../../app-cli-base-coomand";
import { getOrg, fetchApp, getInstalledApps } from "../../util";
import { UninstallAppFactory } from "../../factories/uninstall-app-factory";

export default class Uninstall extends BaseCommand<typeof Uninstall> {
export default class Uninstall extends AppCLIBaseCommand {
static description = "Uninstall an app";

static examples = [
Expand All @@ -28,8 +29,10 @@ export default class Uninstall extends BaseCommand<typeof Uninstall> {
async run(): Promise<void> {
try {
let app, appType
this.flags["app-uid"] = this.manifestData?.uid ?? this.flags["app-uid"];

// get organization to be used
this.sharedConfig.org = await getOrg(this.flags, {managementSdk: this.managementSdk, log: this.log});
this.sharedConfig.org = this.manifestData?.organization_uid ?? (await getOrg(this.flags, {managementSdk: this.managementSdk, log: this.log}));

// fetch app details
if (!this.flags['app-uid']) {
Expand Down
14 changes: 8 additions & 6 deletions src/commands/app/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import { flags } from "@contentstack/cli-utilities";
import { App } from "@contentstack/management/types/app";
import { existsSync, readFileSync, writeFileSync } from "fs";

import { AppManifest } from "../../types";
import { BaseCommand } from "../../base-command";
import { $t, appUpdate } from "../../messages";
import { fetchApp, getApp, getOrg } from "../../util";
import {AppCLIBaseCommand} from "../../app-cli-base-coomand";

export default class Update extends BaseCommand<typeof Update> {
export default class Update extends AppCLIBaseCommand {
private orgUid!: string;
private manifestPathRetry: number = 0;
private manifestData!: AppManifest & Record<string, any>;

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

Expand All @@ -30,8 +28,12 @@ export default class Update extends BaseCommand<typeof Update> {

async run(): Promise<void> {
try {
await this.validateManifest();
this.orgUid = this.flags.org || this.manifestData.organization_uid;
//if working directory isn't app directory
if(!this.manifestData){
await this.validateManifest();
}
this.flags["app-manifest"] = this.manifestPath ?? this.flags["app-manifest"];
this.orgUid = this.flags.org ?? this.manifestData?.organization_uid;
this.sharedConfig.org = await getOrg(
{ org: this.orgUid as any },
{
Expand Down
3 changes: 2 additions & 1 deletion src/messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const commonMsg = {
APP_TYPE_DESCRIPTION: "Type of App",
CONTACT_SUPPORT: "Please contact the support team.",
STACK_API_KEY: "API key of the stack where the app is to be installed.",
USER_TERMINATION: "Process terminated by the user."
USER_TERMINATION: "Process terminated by the user.",
CLI_APP_CLI_LOGIN_FAILED: 'You are not logged in. Please login with command $ csdx auth:login'
};

const appCreate = {
Expand Down
1 change: 0 additions & 1 deletion src/util/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ async function fetchApps(
.catch((error) => {
cliux.loader("failed");
log("Some error occurred while fetching apps.", "warn");
log(error.errorMessage, "error");
throw error;
});

Expand Down