jmcdo29/nest-commander

How to run a command from CLI?

MrsBookik opened this issue · 4 comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I'm curious how to execute a command in CLI?

I have seen code like this in some blogs (which are outdated and refer to older versions):

TS_NODE_PROJECT=./tsconfig.json ts-node -r tsconfig-paths/register ./src/main-cli.ts basic

and this seems to work in order to execute a command called "basic" (This command was taken from the examples at https://docs.nestjs.com/recipes/nest-commander).

The issue with this code line is that it stopped working recently properly and requires a removed dist-directory.

This means_

  1. check out nestJS application with basic command
  2. execute the command with the line above -> will work okay
  3. run some "yarn build", "yarn start:dev" which produces also a dist-folder, this command above will lead to "exit command -1"

What is the official recommendation to execute a command? I can't see anything in README nor in further Docs.

Minimum reproduction code

main.ts

import { CommandFactory } from 'nest-commander';
import { AppModule } from './app.module';

async function bootstrap() {
  await CommandFactory.run(AppModule);
}

bootstrap();

src/command/basic.command.ts:

import { Command, CommandRunner, Option } from 'nest-commander';
import { LogService } from './log.service';

interface BasicCommandOptions {
  string?: string;
  boolean?: boolean;
  number?: number;
}

@Command({ name: 'basic', description: 'A parameter parse' })
export class BasicCommand extends CommandRunner {
  constructor(private readonly logService: LogService) {
    super()
  }

  async run(
    passedParam: string[],
    options?: BasicCommandOptions,
  ): Promise<void> {
    if (options?.boolean !== undefined && options?.boolean !== null) {
      this.runWithBoolean(passedParam, options.boolean);
    } else if (options?.number) {
      this.runWithNumber(passedParam, options.number);
    } else if (options?.string) {
      this.runWithString(passedParam, options.string);
    } else {
      this.runWithNone(passedParam);
    }
  }

  @Option({
    flags: '-n, --number [number]',
    description: 'A basic number parser',
  })
  parseNumber(val: string): number {
    return Number(val);
  }

  @Option({
    flags: '-s, --string [string]',
    description: 'A string return',
  })
  parseString(val: string): string {
    return val;
  }

  @Option({
    flags: '-b, --boolean [boolean]',
    description: 'A boolean parser',
  })
  parseBoolean(val: string): boolean {
    return JSON.parse(val);
  }

  runWithString(param: string[], option: string): void {
    this.logService.log({ param, string: option });
  }

  runWithNumber(param: string[], option: number): void {
    this.logService.log({ param, number: option });
  }

  runWithBoolean(param: string[], option: boolean): void {
    this.logService.log({ param, boolean: option });
  }

  runWithNone(param: string[]): void {
    this.logService.log({ param });
  }
}

Expected behavior

I want to execute this command per CLI and need to know how?

Package

  • nest-commander
  • nest-commander-schematics
  • nest-commander-testing

Package version

3.1

Node.js version

16.13.1

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

Does the documentation on this page help you?

Page not found. 😢

I'm looking for this too. The README never clearly indicates how to run a command once everything is in place.

Page not found. 😢

Here's the updated page link. Pages got changed when I switched from Docusarus to Astro.

Thank you.

For the record, I'm working with a NestJS monorepo, and I ended up doing this for debugging locally :

npx nest start app-name -- basic -n 8

...which works fine, including the args.