# nestjs-console-oclif

> A NestJS module that integrate oclif to build nice cli

Latest version **1.0.5** (published 2020-01-21) · MIT license · 0 weekly downloads

## Install

```sh
npm install nestjs-console-oclif
pnpm add nestjs-console-oclif
yarn add nestjs-console-oclif
bun add nestjs-console-oclif
```

## Health

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

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.5 |
| Published | 2020-01-21 |
| First published | 2019-10-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 331.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | F. Boisselier |
| Maintainers | fboisselier52 |
| Keywords | nestjs, module, cli, console, oclif |

## Links

- npm: https://www.npmjs.com/package/nestjs-console-oclif
- Repository: https://gitlab.com/fboisselier52/nestjs-console-oclif
- Homepage: https://gitlab.com/fboisselier52/nestjs-console-oclif#readme
- Issues: https://gitlab.com/fboisselier52/nestjs-console-oclif/issues
- npm.io page: https://npm.io/package/nestjs-console-oclif

## 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

- 1.0.5 (latest) — 2020-01-21
- 1.0.4 — 2020-01-18
- 1.0.3 — 2019-10-18
- 1.0.2 — 2019-10-15
- 1.0.1 — 2019-10-15
- 1.0.0 — 2019-10-14
- 0.0.1 — 2019-10-14

## README

![NestJS + Oclif](./doc/nestJS-plus-oclif.png)

[![pipeline status](https://gitlab.com/fboisselier52/nestjs-console-oclif/badges/master/pipeline.svg)](https://gitlab.com/fboisselier52/nestjs-console-oclif/commits/master)

`nestjs-console-oclif` is a [NestJS](https://nestjs.com/) module that provides a CLI with [Oclif](https://oclif.io).

## Getting started

### Generate your NestJS application

The architecture is based on NestJS architecture. So you can generate your application with nestcli : https://docs.nestjs.com/first-steps

### Install from NPM
```bash
npm install @oclif/command @oclif/config @oclif/errors @oclif/parser nestjs-console-oclif
# or unig yarn
yarn add @oclif/command @oclif/config @oclif/errors @oclif/parser nestjs-console-oclif
```

:warning: Don't forget to specify Oclif options in `package.json` :
```json
{
  // ...
  "scripts": {
    "build": "nest build && oclif-dev manifest"
  },
  // ...
  "devDependencies": {
    // ...
    "@oclif/dev-cli": "^1.22.2",
    // ...
  },
  "bin": {
    "myCli": "./dist/main.js"
  },
  // ...
  "oclif": {
    "commands": "./dist/commands",
    "bin": "myCli",
    "plugins": [
      "@oclif/plugin-help"
    ]
  }
  // ...
}
```

### Override NestJS bootstrap

Replace in file `src/main.ts` with `AppModule` as your main NestJS module:

```typescript
#!/usr/bin/env node

import { bootstrap } from 'nestjs-console-oclif';
import { AppModule } from './app.module';

bootstrap({
  nestModule: AppModule
});

```
Options :
* nestModule : The class of the NestJS module
* singleCommand: The class of the single command
* enableShutdownHooks: `true` if you want enable shutdown hooks on the NestJS app. See [NestJS lifecycle events](https://docs.nestjs.com/fundamentals/lifecycle-events)

### Import OclifModule

Import `OclifModule` in your main NestJS module (in file `src/app.module.ts`).

```typescript
import { Module } from '@nestjs/common';
import { OclifModule } from 'nestjs-console-oclif';

@Module({
  imports: [OclifModule.forRoot()],
  providers: [...],
})
export class AppModule {
}
```

### Define your commands

#### Multi commands
You define your command in `src/commands/myCommand.ts` like [oclif](https://oclif.io/docs/commands).

But :
- Extends your command from `BaseCommand`
- You don't need to implement the `run` function.

```typescript
import { BaseCommand } from 'nestjs-console-oclif';
import { flags } from '@oclif/command';

export class MyCommand extends BaseCommand {
  static description = 'description of this example command';

  static flags: = {
    myFlag: flags.string({
      char: 'f',
      description: 'My flag',
    })
  };

  static args = [
    {name: 'myArg'}
  ];
}
```

#### Single command

Define your command in `src/commands/myCommand.ts`, but specify the command in bootstrap function :

```typescript
#!/usr/bin/env node

import { bootstrap } from 'nestjs-console-oclif';
import { AppModule } from './app.module';
import { MyCommand } from './commands/myCommand';

bootstrap({
  nestModule: AppModule,
  singleCommand: MyCommand
});

```

### Register your commands entrypoints

Define the entrypoint of the command defined in `MyCommand`.

:warning: The entrypoint does not take arguments.

```typescript
import { Injectable } from '@nestjs/common';
import { OclifCommand } from 'nestjs-console-oclif';

@Injectable()
export class AppService {

  @OclifCommand('myCommand')
  async run(): Promise<void> {
    // Do something
  }
}
```

### Read Args and flags

Load your parameters from NestJS context with the decorators `OclifArgs` and `OclifFlags` :

```typescript
import { OclifFlags, OclifCommand, OclifArgs } from 'nestjs-console-oclif';
import { Injectable } from '@nestjs/common';

@Injectable()
export class AppService {
  constructor(
        @OclifFlags('myFlag') protected myFlag: string,
        @OclifArgs('myArg') protected myArg: string) {
  }

  @OclifCommand('myCommand')
  async run(): Promise<void> {
    console.log(`my flag : ${this.myFlag}`);
    console.log(`my arg : ${this.myArg}`);
  }
}
```

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