# @anchan828/nest-commands

> > TODO: description

Latest version **0.2.41** (published 2022-04-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @anchan828/nest-commands
pnpm add @anchan828/nest-commands
yarn add @anchan828/nest-commands
bun add @anchan828/nest-commands
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.41 |
| Published | 2022-04-10 |
| First published | 2019-10-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 49.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | anchan828 |
| Maintainers | anchan828 |
| Keywords | nestjs, command, cli |

## Links

- npm: https://www.npmjs.com/package/@anchan828/nest-commands
- Repository: https://github.com/anchan828/nest-commands
- Homepage: https://github.com/anchan828/nest-commands/tree/master/packages/commands#readme
- Issues: https://github.com/anchan828/nest-commands/issues
- npm.io page: https://npm.io/package/@anchan828/nest-commands

## Dependencies (4)

- [y18n](https://npm.io/package/y18n.md) ^5.0.0
- [yargs](https://npm.io/package/yargs.md) 17.4.1
- [param-case](https://npm.io/package/param-case.md) 3.0.4
- [cosmiconfig](https://npm.io/package/cosmiconfig.md) 7.0.1

## 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.2.41 (latest) — 2022-04-10
- 0.2.40 — 2022-04-03
- 0.2.39 — 2022-03-27
- 0.2.38 — 2022-03-20
- 0.2.37 — 2022-03-13
- 0.2.36 — 2022-03-06
- 0.2.35 — 2022-02-27
- 0.2.34 — 2022-02-20
- 0.2.33 — 2022-02-13
- 0.2.32 — 2022-02-06
- 0.2.31 — 2022-01-30
- 0.2.30 — 2022-01-23
- 0.2.29 — 2022-01-16
- 0.2.28 — 2022-01-09
- 0.2.27 — 2022-01-02
- … 140 more at https://npm.io/package/@anchan828/nest-commands/versions

## README

# @anchan828/nest-commands

Make command line tools based on yargs.

```bash
$ nest-commands-example --help
nest-commands-example <command>

Commands:
  nest-commands-example author  Show author information
  nest-commands-example file    Show file information

Options:
  --help     Show help                           [boolean]
  --version  Show version number                 [boolean]
```

## Install

```shell
$ npm i @anchan828/nest-commands
$ npm i --save-dev @types/yargs
```

## Usage

cli.ts

```typescript
import { Commander, Command, CommandModule, CommandService } from "@anchan828/nest-commands";

@Commander()
class TestCommander {
  @Command({ name: "basic" })
  public basic() {
    console.log("hello!");
  }
}

@Module({
  imports: [CommandModule.register()],
  providers: [TestCommander],
})
class TestAppModule {}

(async () => {
  const app = await NestFactory.createApplicationContext(TestAppModule, { logger: false });
  app.get(CommandService).exec();
})();
```

```
$ ts-node cli.ts basic
hello!
```

### CommandModule.register

You can set some options

```typescript
CommandModule.register({
   /**
   * Set to yargs.scriptName
   *
   * @type {string}
   * @memberof CommandModuleOptions
   */
  scriptName?: string;

  /**
   * Set to yargs.usage
   *
   * @type {string}
   * @memberof CommandModuleOptions
   */
  usage?: string;

  /**
   * Set to yargs.locale
   *
   * @type {string}
   * @memberof CommandModuleOptions
   */
  locale?: string;
})
```

### Nested command

If you want to use nested command, add name to commander.

```typescript
@Commander({ name: "nested" })
class TestCommander {
  @Command({ name: "command1" })
  public command1(): void {
    console.log("Run nested command1");
  }

  @Command({ name: "command2" })
  public command2(): void {
    console.log("Run nested command2");
  }
}
```

### Global options

You can set options to top level. Use `@CommanderOption` and no set commander name

```typescript
@Commander()
class GlobalOptions {
  @CommanderOption({
    description: "Output as json",
    name: "json",
    type: "boolean",
  })
  public json!: boolean;
}
```

### positional and option

```typescript
@Commander()
class TestCommander {
  // See: https://github.com/yargs/yargs#complex-example
  @Command({ describe: "start the server", name: "serve" })
  public serve(
    @CommandPositional({
      default: 5000,
      describe: "port to bind on",
      name: "port",
    })
    port: number,
    @CommandOption({
      alias: "v",
      default: false,
      description: "Run with verbose logging",
      name: "verbose",
      type: "boolean",
    })
    verbose: boolean,
  ): void {
    console.log(`port is ${port}`);
    console.log(`verbose is ${verbose}`);
  }
}
```

### Array positional

If you want to array positional, add `..` at the end.

```typescript
@Commander()
class TestCommander {
  @Command({ describe: "array positional", name: "list" })
  public serve(
    @CommandPositional({
      describe: "show files",
      name: "files..",
    })
    files: string[],
  ): void {
    console.log("Run array positional command");
    console.log(files);
  }
}
```

### Pipe

```typescript
@Injectable()
class StringPipe implements PipeTransform<string, string> {
  transform(value: string): string {
    return `updated ${value}`;
  }
}

@Commander()
class TestCommander {
  @Command({ describe: "start the server", name: "serve" })
  public serve(): void {
    // token is 'updated token'
    console.log(`token is '${this.token}'`);
  }

  @CommanderOption({ demandOption: true, name: "token", type: "string" }, StringPipe)
  public token!: string;
}
```

### Load config file (cosmiconfig)

You can load config file from user project.

If you want to load config, set configName. Then CLI will search and load them

- A {configName} property in a package.json file.
- A .{configName}rc file with JSON or YAML syntax.
- A .{configName}rc.json file.
- A .{configName}rc.yaml, .{configName}rc.yml, or .{configName}rc.js file.
- A {configName}.config.js JS file exporting the object.

Please see [cosmiconfig](https://github.com/davidtheclark/cosmiconfig#explorersearch) about more details

Also, You can add custom config filename

```ts
@Module({
  imports: [
    CommandModule.register({
      configName: "nest-commands",
      searchPlaces: ["custom-config-name.json"],
    }),
  ],
  providers: [TestCommander],
})
class TestAppModule {}
```

### Customize config object

You can customize config after loading it.

```ts
@Module({
  imports: [
    CommandModule.register({
      config: {
        name: "nest-commands",
        processor: async (config: TestConfig): Promise<TestConfig> => {
          if (config.date === "today") {
            config.date = new Date().toDateString();
          }
          return config;
        },
      },
    }),
  ],
  providers: [TestCommander],
})
class TestAppModule {}
```

Also, you can use decorators!

```ts
@GlobalConfig({ name: "nest-commands", searchPlaces: ["custom-config-name.json"] })
class TestGlobalConfig {
  @GlobalConfigProcessor()
  public async processor(config: TestConfig): Promise<TestConfig> {
    if (config.date === "today") {
      config.date = new Date().toDateString();
    }
    return config;
  }
}

@Module({
  imports: [CommandModule.register()],
  providers: [TestCommander, TestGlobalConfig],
})
class TestAppModule {}
```

## Example

You can try to run command!

```shell
npx ts-node -T ./examples/basic.ts basic --help
npx ts-node -T ./examples/nested-commands.ts nested show --help
npx ts-node -T ./examples/positional-and-option.ts serve --help
npx ts-node -T ./examples/many-modules.ts --help
npx ts-node -T ./examples/many-modules.ts user show
npx ts-node -T ./examples/array-positional.ts list test1 test2
npx ts-node -T ./examples/merge-commanders merge --help
npx ts-node -T ./examples/commander-option.ts --token token serve
npx ts-node -T ./examples/global-options.ts --json test show
npx ts-node -T ./examples/use-pipes.ts --token token serve
npx ts-node -T ./examples/config.ts test
npx ts-node -T ./examples/config.processor.ts test
```

## Tips

You can create single executable file using [ncc](https://github.com/zeit/ncc) / [pkg](https://github.com/zeit/pkg) / [nexe](https://github.com/nexe/nexe).

See: [@anchan828/nest-commands-example](../commands-example)

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