Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Development to staging #1080

Merged
merged 19 commits into from
Oct 5, 2023
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
44,861 changes: 24,660 additions & 20,201 deletions package-lock.json

Large diffs are not rendered by default.

33 changes: 14 additions & 19 deletions packages/contentstack-auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,40 @@ It is Contentstack’s CLI plugin to perform authentication-related activities.
[![License](https://img.shields.io/npm/l/@contentstack/cli)](https://github.com/contentstack/cli/blob/main/LICENSE)

<!-- toc -->

- [@contentstack/cli-auth](#contentstackcli-auth)
- [Usage](#usage)
- [Commands](#commands)
* [@contentstack/cli-auth](#contentstackcli-auth)
* [Usage](#usage)
* [Commands](#commands)
<!-- tocstop -->

# Usage

<!-- usage -->

```sh-session
$ npm install -g @contentstack/cli-auth
$ csdx COMMAND
running command...
$ csdx (--version)
@contentstack/cli-auth/1.3.13 darwin-arm64 node-v20.7.0
@contentstack/cli-auth/1.3.14 darwin-x64 node-v20.8.0
$ csdx --help [COMMAND]
USAGE
$ csdx COMMAND
...
```

<!-- usagestop -->

# Commands

<!-- commands -->

- [`csdx auth:login`](#csdx-authlogin)
- [`csdx auth:logout`](#csdx-authlogout)
- [`csdx auth:tokens`](#csdx-authtokens)
- [`csdx auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]`](#csdx-authtokensadd--a-value---delivery---management--e-value--k-value--y---token-value)
- [`csdx auth:tokens:remove`](#csdx-authtokensremove)
- [`csdx auth:whoami`](#csdx-authwhoami)
- [`csdx login`](#csdx-login)
- [`csdx logout`](#csdx-logout)
- [`csdx tokens`](#csdx-tokens)
- [`csdx whoami`](#csdx-whoami)
* [`csdx auth:login`](#csdx-authlogin)
* [`csdx auth:logout`](#csdx-authlogout)
* [`csdx auth:tokens`](#csdx-authtokens)
* [`csdx auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]`](#csdx-authtokensadd--a-value---delivery---management--e-value--k-value--y---token-value)
* [`csdx auth:tokens:remove`](#csdx-authtokensremove)
* [`csdx auth:whoami`](#csdx-authwhoami)
* [`csdx login`](#csdx-login)
* [`csdx logout`](#csdx-logout)
* [`csdx tokens`](#csdx-tokens)
* [`csdx whoami`](#csdx-whoami)

## `csdx auth:login`

Expand Down Expand Up @@ -328,5 +324,4 @@ ALIASES
EXAMPLES
$ csdx auth:whoami
```

<!-- commandsstop -->
6 changes: 3 additions & 3 deletions packages/contentstack-auth/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@contentstack/cli-auth",
"description": "Contentstack CLI plugin for authentication activities",
"version": "1.3.13",
"version": "1.3.14",
"author": "Contentstack",
"bugs": "https://github.com/contentstack/cli/issues",
"scripts": {
Expand All @@ -22,8 +22,8 @@
"test:unit:report": "nyc --extension .ts mocha --forbid-only \"test/unit/**/*.test.ts\""
},
"dependencies": {
"@contentstack/cli-command": "~1.2.12",
"@contentstack/cli-utilities": "~1.5.2",
"@contentstack/cli-command": "~1.2.13",
"@contentstack/cli-utilities": "~1.5.3",
"chalk": "^4.0.0",
"debug": "^4.1.1",
"inquirer": "8.2.4",
Expand Down
59 changes: 59 additions & 0 deletions packages/contentstack-auth/src/base-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { Command } from '@contentstack/cli-command';
import { FlagInput, Flags, Interfaces, LoggerService } from '@contentstack/cli-utilities';

export type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;
export type Flags<T extends typeof Command> = Interfaces.InferredFlags<(typeof BaseCommand)['baseFlags'] & T['flags']>;

export abstract class BaseCommand<T extends typeof Command> extends Command {
public logger!: LoggerService;
protected args!: Args<T>;
protected flags!: Flags<T>;

// NOTE define flags that can be inherited by any command that extends BaseCommand
static baseFlags: FlagInput = {};

/**
* The `init` function initializes the command by parsing arguments and flags, registering search
* plugins, registering the configuration, and initializing the logger.
*/
public async init(): Promise<void> {
await super.init();
const { args, flags } = await this.parse({
flags: this.ctor.flags,
baseFlags: (super.ctor as typeof BaseCommand).baseFlags,
args: this.ctor.args,
strict: this.ctor.strict,
});
this.flags = flags as Flags<T>;
this.args = args as Args<T>;

// Init logger
this.logger = new LoggerService(process.cwd(), 'cli-log');
}

/**
* The catch function is used to handle errors from a command, either by adding custom logic or
* returning the parent class error handling.
* @param err - The `err` parameter is of type `Error & { exitCode?: number }`. This means that it is
* an object that extends the `Error` class and may also have an optional property `exitCode` of type
* `number`.
* @returns The parent class error handling is being returned.
*/
protected async catch(err: Error & { exitCode?: number }): Promise<any> {
// add any custom logic to handle errors from the command
// or simply return the parent class error handling
return super.catch(err);
}

/**
* The `finally` function is called after the `run` and `catch` functions, regardless of whether or not
* an error occurred.
* @param {Error | undefined} _ - The parameter "_" represents an error object or undefined.
* @returns The `finally` method is returning the result of calling the `finally` method of the
* superclass, which is a promise.
*/
protected async finally(_: Error | undefined): Promise<any> {
// called after run and catch regardless of whether or not the command errored
return super.finally(_);
}
}
11 changes: 5 additions & 6 deletions packages/contentstack-auth/src/commands/auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Command } from '@contentstack/cli-command';
import {
logger,
cliux,
CLIError,
authHandler as oauthHandler,
flags,
managementSDKClient,
FlagInput
} from '@contentstack/cli-utilities';

import { User } from '../../interfaces';
import { authHandler, interactive } from '../../utils';
import { BaseCommand } from '../../base-command';

export default class LoginCommand extends Command {
export default class LoginCommand extends BaseCommand<typeof LoginCommand> {
static run; // to fix the test issue
static description = 'User sessions login';

Expand Down Expand Up @@ -61,7 +60,7 @@ export default class LoginCommand extends Command {
} else {
const username = loginFlags?.username || (await interactive.askUsername());
const password = loginFlags?.password || (await interactive.askPassword());
logger.debug('username', username);
this.logger.debug('username', username);
await this.login(username, password);
}
} catch (error) {
Expand All @@ -77,7 +76,7 @@ export default class LoginCommand extends Command {
errorMessage = error;
}
}
logger.error('login failed', errorMessage);
this.logger.error('login failed', errorMessage);
cliux.error('CLI_AUTH_LOGIN_FAILED');
cliux.error(errorMessage);
process.exit();
Expand All @@ -91,7 +90,7 @@ export default class LoginCommand extends Command {
throw new CLIError('Failed to login - invalid response');
}
await oauthHandler.setConfigData('basicAuth', user);
logger.info('successfully logged in');
this.logger.info('successfully logged in');
cliux.success('CLI_AUTH_LOGIN_SUCCESS');
} catch (error) {
throw error;
Expand Down
9 changes: 4 additions & 5 deletions packages/contentstack-auth/src/commands/auth/logout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Command } from '@contentstack/cli-command';
import {
logger,
cliux,
configHandler,
printFlagDeprecation,
Expand All @@ -11,8 +10,8 @@ import {
} from '@contentstack/cli-utilities';

import { authHandler } from '../../utils';

export default class LogoutCommand extends Command {
import { BaseCommand } from '../../base-command';
export default class LogoutCommand extends BaseCommand<typeof LogoutCommand> {
static run;
static description = 'User session logout';
static examples = ['$ csdx auth:logout', '$ csdx auth:logout -y', '$ csdx auth:logout --yes'];
Expand Down Expand Up @@ -58,7 +57,7 @@ export default class LogoutCommand extends Command {
await oauthHandler.oauthLogout()
}
cliux.loader('');
logger.info('successfully logged out');
this.logger.info('successfully logged out');
cliux.success('CLI_AUTH_LOGOUT_SUCCESS');
} else {
cliux.success('CLI_AUTH_LOGOUT_ALREADY');
Expand All @@ -77,7 +76,7 @@ export default class LogoutCommand extends Command {
}
}

logger.error('Logout failed', errorMessage);
this.logger.error('Logout failed', errorMessage);
cliux.print('CLI_AUTH_LOGOUT_FAILED', { color: 'yellow' });
cliux.print(errorMessage, { color: 'red' });
} finally {
Expand Down
15 changes: 7 additions & 8 deletions packages/contentstack-auth/src/commands/auth/tokens/add.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { Command } from '@contentstack/cli-command';
import {
logger,
cliux,
configHandler,
printFlagDeprecation,
flags,
FlagInput,
HttpClient,
messageHandler,
Flags,
} from '@contentstack/cli-utilities';
import { askTokenType } from '../../../utils/interactive';
export default class TokensAddCommand extends Command {
import { BaseCommand } from '../../../base-command';
export default class TokensAddCommand extends BaseCommand<typeof TokensAddCommand> {
static description = 'Adds management/delivery tokens to your session to use it with other CLI commands';

static examples = [
Expand All @@ -28,7 +29,7 @@ export default class TokensAddCommand extends Command {
];

static flags: FlagInput = {
alias: flags.string({ char: 'a', description: 'Name of the token alias' }),
alias: Flags.string({ char: 'a', description: 'Name of the token alias' }),
delivery: flags.boolean({
char: 'd',
description: 'Set this flag to save delivery token',
Expand Down Expand Up @@ -79,7 +80,6 @@ export default class TokensAddCommand extends Command {
'auth:tokens:add [-a <value>] [--delivery] [--management] [-e <value>] [-k <value>] [-y] [--token <value>]';

async run(): Promise<any> {
// @ts-ignore
const { flags: addTokenFlags } = await this.parse(TokensAddCommand);
let isAliasExist = false;
const skipAliasReplaceConfirmation = addTokenFlags.force || addTokenFlags.yes;
Expand All @@ -89,7 +89,6 @@ export default class TokensAddCommand extends Command {
let isDelivery = addTokenFlags.delivery;
let isManagement = addTokenFlags.management;
let environment = addTokenFlags.environment;
let branch = addTokenFlags.branch;
const configKeyTokens = 'tokens';

if (!isDelivery && !isManagement && !Boolean(environment)) {
Expand All @@ -100,7 +99,7 @@ export default class TokensAddCommand extends Command {

const type = isDelivery || Boolean(environment) ? 'delivery' : 'management';

logger.info(`adding ${type} token`);
this.logger.info(`adding ${type} token`);

try {
if (!alias) {
Expand All @@ -114,7 +113,7 @@ export default class TokensAddCommand extends Command {
name: 'confirm',
});
if (!shouldAliasReplace) {
logger.info('Exiting from the process of replacing the token');
this.logger.info('Exiting from the process of replacing the token');
cliux.print('CLI_AUTH_EXIT_PROCESS');
return;
}
Expand Down Expand Up @@ -160,7 +159,7 @@ export default class TokensAddCommand extends Command {
cliux.success('CLI_AUTH_TOKENS_ADD_SUCCESS');
}
} catch (error) {
logger.error('token add error', error.message);
this.logger.error('token add error', error.message);
cliux.print('CLI_AUTH_TOKENS_ADD_FAILED', { color: 'yellow' });
cliux.error(error.message.message ? error.message.message : error.message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Command } from '@contentstack/cli-command';
import { logger, cliux, configHandler } from '@contentstack/cli-utilities';

export default class TokensListCommand extends Command {
import { cliux, configHandler } from '@contentstack/cli-utilities';
import { BaseCommand } from '../../../base-command';
export default class TokensListCommand extends BaseCommand<typeof TokensListCommand> {
static aliases = ['tokens'];
static examples = ['$ csdx auth:tokens'];
static description = 'Lists all existing tokens added to the session';
Expand Down Expand Up @@ -52,7 +52,7 @@ export default class TokensListCommand extends Command {
cliux.print('CLI_AUTH_TOKENS_LIST_NO_TOKENS');
}
} catch (error) {
logger.error('Token list error', error.message);
this.logger.error('Token list error', error.message);
cliux.print('CLI_AUTH_TOKENS_LIST_FAILED', { color: 'yellow' });
cliux.print(error.message, { color: 'red' });
}
Expand Down
14 changes: 10 additions & 4 deletions packages/contentstack-auth/src/commands/auth/tokens/remove.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Command } from '@contentstack/cli-command';
import { logger, cliux, configHandler, flags, FlagInput } from '@contentstack/cli-utilities';
import { cliux, configHandler, flags, FlagInput } from '@contentstack/cli-utilities';
import { BaseCommand } from '../../../base-command';

export default class TokensRemoveCommand extends Command {
export default class TokensRemoveCommand extends BaseCommand<typeof TokensRemoveCommand> {
static description = 'Removes selected tokens';
static examples = ['$ csdx auth:tokens:remove', '$ csdx auth:tokens:remove -a <alias>'];
static flags: FlagInput = {
Expand Down Expand Up @@ -42,17 +43,22 @@ export default class TokensRemoveCommand extends Command {
choices: tokenOptions,
});

logger.debug('selected tokens', selectedTokens);
if (selectedTokens.length === 0) {
return;
}

selectedTokens.forEach((ele)=>{
this.logger.info('selected tokens',ele);
})

selectedTokens.forEach((element) => {
const selectedToken = element.split(':')[0];
configHandler.delete(`tokens.${selectedToken}`);
cliux.success('CLI_AUTH_TOKENS_REMOVE_SUCCESS');
this.logger.info('Token removed successfully !!', element);
});
} catch (error) {
logger.error('Token remove error', error.message);
this.logger.error('Token remove error', error.message);
cliux.print('CLI_AUTH_TOKENS_REMOVE_FAILED', { color: 'yellow' });
cliux.print(error.message, { color: 'red' });
}
Expand Down
9 changes: 5 additions & 4 deletions packages/contentstack-auth/src/commands/auth/whoami.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Command } from '@contentstack/cli-command';
import { logger, cliux } from '@contentstack/cli-utilities';
import { cliux } from '@contentstack/cli-utilities';
import { BaseCommand } from '../../base-command';

export default class WhoamiCommand extends Command {
export default class WhoamiCommand extends BaseCommand<typeof WhoamiCommand> {
static description = 'Display current users email address';

static examples = ['$ csdx auth:whoami'];
Expand All @@ -13,12 +14,12 @@ export default class WhoamiCommand extends Command {
if (this.email) {
cliux.print('CLI_AUTH_WHOAMI_LOGGED_IN_AS', { color: 'white' });
cliux.print(this.email, { color: 'green' });
logger.info('Currently logged in user', this.email);
this.logger.info('Currently logged in user', this.email);
} else {
cliux.error('CLI_AUTH_WHOAMI_FAILED');
}
} catch (error) {
logger.error('whoami error', error.message);
this.logger.error('whoami error', error.message);
cliux.print('CLI_AUTH_WHOAMI_FAILED', { color: 'yellow' });
cliux.print(error.message, { color: 'red' });
}
Expand Down
Loading
Loading