# rx-console

> Rx Console is a small cli framework developed with Typescript.

Latest version **0.0.1** (published 2020-12-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install rx-console
pnpm add rx-console
yarn add rx-console
bun add rx-console
```

Provides the command `rxc`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2020-12-01 |
| First published | 2020-12-01 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 11 |
| Unpacked size | 32.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Ahmet ATAY |
| Maintainers | atayahmet |
| Keywords | console, cli, application |

## Links

- npm: https://www.npmjs.com/package/rx-console
- npm.io page: https://npm.io/package/rx-console

## Dependencies (11)

- [rxjs](https://npm.io/package/rxjs.md) ^6.6.3
- [chalk](https://npm.io/package/chalk.md) ^4.1.0
- [redux](https://npm.io/package/redux.md) ^4.0.5
- [yargs](https://npm.io/package/yargs.md) ^16.1.1
- [is-promise](https://npm.io/package/is-promise.md) ^4.0.0
- [typescript](https://npm.io/package/typescript.md) ^4.0.5
- [@types/yargs](https://npm.io/package/@types/yargs.md) ^15.0.10
- [lodash.isstring](https://npm.io/package/lodash.isstring.md) ^4.0.1
- [lodash.isfunction](https://npm.io/package/lodash.isfunction.md) ^3.0.9
- [lodash.isplainobject](https://npm.io/package/lodash.isplainobject.md) ^4.0.6
- [@util-funcs/object-get](https://npm.io/package/@util-funcs/object-get.md) ^0.0.2

## Alternatives

- [@salesforce/cli](https://npm.io/package/@salesforce/cli.md) — 389.7K weekly downloads
- [@mintlify/cli](https://npm.io/package/@mintlify/cli.md) — 208.9K weekly downloads
- [@grafana/e2e-selectors](https://npm.io/package/@grafana/e2e-selectors.md) — 128.7K weekly downloads
- [mintlify](https://npm.io/package/mintlify.md) — 112.0K weekly downloads
- [@intlayer/cli](https://npm.io/package/@intlayer/cli.md) — 22.8K weekly downloads

## Recent versions

- 0.0.1 (latest) — 2020-12-01

## README

# Konsole JS

**KonsoleJS** is a small cli framework developed with Typescript. You can develop reactive applications by [Redux](https://redux.js.org/) and [RxJS](https://www.learnrxjs.io/) in Konsole JS.

**Install**

```sh
$ yarn add konsolejs
```

or

```sh
$ npm i konsolejs --save
```

## Quick Start

**hello.ts**

```ts
import { Command } from 'konsolejs';

@Command()
class HelloCommand {
  name = 'hello';
  description = 'The hello world command';

  handle() {
    console.log('Hello World!');
  }
}

export default HelloCommand;
```

**index.ts**

```ts
import Konsole from 'konsolejs';
import './login';

Konsole.run({
  scriptName: 'console-app',
});
```

**Terminal**

```sh
$ npx ts-node index.ts hello
```

## Command arguments

Konsole JS uses [yargs](http://yargs.js.org/) for command arguments. Created command builder will be inject to command class constructor. So you can be detail your command arguments.

**Example:**

```ts
import { Command } from 'konsolejs';
import { Argv, Arguments } from 'yargs';

@Command()
class LoginCommand {
  name = 'login';
  description = 'Loging to platformm';

  before(builder: Argv) {
    builder
      .positional('username', {
        type: 'string',
        describe: 'The user name',
      })
      .positional('password', {
        type: 'string',
        describe: 'The user password',
      });
  }

  handle(args: Arguments) {
    console.log('username:', args.username, 'password:', args.password);
  }
}

export default LoginCommand;
```

**Terminal**

```sh
$ npx ts-node index.ts login --username test@example.com --password 1234
```

For more advanced usage, please visit: http://yargs.js.org

## Dependency Injection

With the `@Command` decorator you can inject parameters into the command class constructor.

```ts
@Command({
  inject: [auth, user],
})
class LoginCommand {
  constructor(auth, user) {
    // ...
  }
}
```

## Use Redux

You can manage state of objects using pure ReduxJS library. For this first of all, you should be configure the redux then pass the store to KonsoleJS.

**Install Redux:**

```sh
$ yarn add redux
```

**Create store:**

```ts
import Konsole from 'konsolejs';
import { createStore } from 'redux';

const store = createStore(reducers);

Konsole.run({
  scriptName: 'console-app',
  store,
});
```

That's all.

### Actions and Subscribers

**Action creator**

```ts
const fetchUser = (email: string) => async (dispatch) => {
  try {
    dispatch({ type: 'FETCH_USER_PENDING' });
    const result = await fetch(/**api request**/);
    dispatch({ type: 'FETCH_USER_FULFILLED', data: result });
  } catch (e) {
    dispatch({ type: 'FETCH_USER_REJECTED', error: e });
  }
};
```

**Reducer**

```ts
const initialState = {
  pending: false,
  error: null,
  user: null,
};

const userReducer = (state = initialState, action) => {
  switch (action.type) {
    case 'FETCH_USER_PENDING': {
      return {
        ...state,
        pending: true,
      };
    },
    case 'FETCH_USER_FULFILLED': {
      return {
        ...state,
        pending: false,
        user: action.data
      }
    },
    case 'FETCH_USER_REJECTED': {
      return {
        error: action.error,
        pending: false,
        user: null,
      }
    }
    default:
      return state;
  }
};
```

**Command**

```ts
import { Command, Subscribe, dispatch, getState } from 'konsolejs';

@Command()
class CreateUserCommand {
  name = 'create-user [email]';
  description = 'Create a new user';
  args = {};

  @Subscribe('FETCH_USER_FULFILLED')
  userAlreadyExists() {
    console.log('The user already created!');
  }

  @Subscribe('FETCH_USER_REJECTED')
  fetchUserRejected() {
    const { user } = getState();
    if (user.error.message === 'user not found') {
      this.createNewUser();
    }
  }

  @Subscribe('CREATE_USER_FULFILLED')
  createUserFulfilled() {
    console.log('User created!');
  }

  createNewUser() {
    const { email } = this.args;
    dispatch(createUser(email));
  }

  handle(args: Arguments) {
    this.args = args;
    dispatch(fetchUser(args.email));
  }
}
```

**Terminal**

```sh
$ npx ts-node index.ts create-user test@example.com
```

### RxJS Operators

```ts
import { Subject, Observable } from 'rxjs';
import { filter } from 'rxjs/operators';

@Command()
class CreateUserCommand {
  name = 'fetch-users';
  description = 'Fetch users and filter vip ones';

  @Subscribe({
    action: 'FETCH_USERS_FULFILLED',
    observer(subject: Subject<any>): Observable<any> {
      return subject.pipe(filter((user) => user.isVip));
    },
  })
  fetchUsersFulfilled(result) {
    console.log('The user already created!', result);
  }

  handle() {
    dispatch(fetchUsers());
  }
}
```

## Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

## License

[MIT](https://choosealicense.com/licenses/mit/)

---
_Source: https://npm.io/package/rx-console · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
