jmcdo29 / nest-commander

A module for using NestJS to build up CLI applications
https://nest-commander.jaymcdoniel.dev/
MIT License
435 stars 53 forks source link

Class inheritance does not extend all functionality #942

Closed DanHulton closed 5 months ago

DanHulton commented 1 year ago

Is there an existing issue for this?

Current behavior

If you want to have a "parent command class" for global options, a common logging config, shared functions or constructor behaviour, it does not work as expected. Basic class functionality works, i.e. if you create a protected logger instance, that works, and shared functions work as expected, but trying to create an @Options definition that would be "global" among Commands that inherit this class does not work.

Minimum reproduction code

base-command.ts:

import { CommandRunner } from 'nest-commander';
import { InjectPinoLogger, PinoLogger } from 'nestjs-pino';

export abstract class BaseCommand extends CommandRunner {
  @InjectPinoLogger()
  protected readonly logger: PinoLogger;

  @Option({
    flags: '-c, --common <common>',
    description: 'A common flag',
  })
  parseCommon(val: string) {
    return val;
  }
}

test-command.ts:

import { Command, Option } from 'nest-commander';
import { BaseCommand } from './base-command';

@Command({
  name: 'test',
})
export class TestCommand extends BaseCommand {
  async run(inputs: string[], options: Record<string, any>): Promise<void> {
    this.logger.info(options, 'Options');
  }
}

Expected behavior

When running this code with the --common=something flag set, I would expect to see {'common': 'something'} in the logged options, but instead, the logged object is empty.

Package

Package version

3.7.1

Node.js version

18.16.0

In which operating systems have you tested?

Other

No response

jmcdo29 commented 1 year ago

I believe this most likely has to do with my use of @golevelup/nestjs-discovery and how it discovers metadata of each class. I'll see if I can reproduce the functionality away from nest-commander and report it to that library, or adjust my use if necessary

ruscon commented 1 year ago

@jmcdo29 any ideas how to do it ?

jmcdo29 commented 1 year ago

Hmm, I was not able to reproduce this functionality in an e2e test I created for this issue. Can someone provide me a minimum reproduction