#Question about nest-commander

19 messages · Page 1 of 1 (latest)

sterile plover
#

It seems it is not possible to keep the same big AppModule that i work with (graphql, mikro orm and lots of modules) to start nest-commander runner.
At some point, it will be trying to inject InquirerService in my main.ts (not the new cli.ts that i created for nest-commander) and it will fail.

#

@weary jasper fyi, i've read : https://github.com/jmcdo29/nest-commander/issues/582 (and posted)
Maybe i'm wrong but it seems impossible to use for myself.
I can't create a CommandModule dedicated for my commandRunner because it would be duplicating everything ..

GitHub

Is there an existing issue for this? I have searched the existing issues Current behavior Import InquirerService to constructor of Command class. Minimum reproduction code export class BasicCommand...

weary jasper
#

Can you provide a reproduction that shows your issue?

sterile plover
#

I can try tomorrow but just one quick question

#

how can i inject InquirerService inside a service that I should register as a provider in my AppModule ?
But my AppModule is the rootModule of my fastifyApp, and because of this, it will try to inject every service of every provider registed.
Isn't there a problem here ?

weary jasper
#

I would find some sort of way to keep your commands from being required by your server application first and foremost, as if I'm understanding things correctly that's the issue

sterile plover
#

But it is said right here : https://docs.nestjs.com/recipes/nest-commander#testing (a little below)
that my command file should be added in my module ?
How can i register my command if i dont add in my AppModule ?

weary jasper
#

Any reason you can't make a command module that imports the app module while also defining all of your commands?

sterile plover
#

It just does not work 😢

import { Module } from '@nestjs/common';
import { AppModule } from '../app.module';
import { CreateSpaceCommand } from './create-space.command';
import { SendNotifCommand } from './send-notif-test.command';
import { SendSmsbookingEndUpcomingCommand } from './send-sms-booking-end-upcoming.command';
@Module({
  imports: [AppModule],
  providers: [
    SendNotifCommand,
    SendSmsbookingEndUpcomingCommand,
    CreateSpaceCommand,
  ],
})
export class CommandModule {}

And my cli.ts

import { CommandModule } from './command/command.module';
import { CommandFactory } from 'nest-commander';
import { Logger } from '@nestjs/common';

async function bootstrap(): Promise<void> {
  await CommandFactory.run(CommandModule, {
    logger: new Logger(),
    errorHandler: (err) => {
      console.error(err);
      process.exit(1);
    },
  });
}

bootstrap().catch((err) => {
  console.error(err);
  process.exit(1);
});

But i'm getting errors like theses

] Nest can't resolve dependencies of the SendNotifCommand (?). Please make sure that the argument FCMService at index [0] is available in the CommandModule context.

Potential solutions:
- Is CommandModule a valid NestJS module?
- If FCMService is a provider, is it part of the current CommandModule?
- If FCMService is exported from a separate @Module, is that module imported within CommandModule?
  @Module({
    imports: [ /* the Module containing FCMService */ ]
  })
weary jasper
#

Where does FCMService come from? Does that module export the provider? Does your AppModule export that module?

sterile plover
#

Its a service from a Module that is imported inside AppModule, so i cannot re-export it from AppModule right ?

Nest cannot export a provider/module that is not a part of the currently processed module (AppModule). Please verify whether the exported FCMService is available in this particular context.
weary jasper
#

You can re-export the module rather than the service itself in AppModule

sterile plover
#

Wow , it seems i have to re-export the entire module

#

yeah, just found in the docs

#

man it works

#

still got

TypeError: Cannot read properties of undefined (reading 'discoveredClass')
    at /usr/src/app/node_modules/nest-commander/src/inquirer.service.js:42:140
    at /usr/src/app/node_modules/@golevelup/nestjs-discovery/lib/discovery.service.js:62:63
    at Array.filter (<anonymous>)
    at DiscoveryService.providers (/usr/src/app/node_modules/@golevelup/nestjs-discovery/lib/discovery.service.js:62:49)
    at async DiscoveryService.providerMethodsWithMetaAtKey (/usr/src/app/node_modules/@golevelup/nestjs-discovery/lib/discovery.service.js:131:27)
    at async InquirerService.findQuestionSet (/usr/src/app/node_modules/nest-commander/src/inquirer.service.js:42:27)
    at async InquirerService.prompt (/usr/src/app/node_modules/nest-commander/src/inquirer.service.js:34:30)
    at async CreateSpaceCommand.run (/usr/src/app/dist/command/create-space.command.js:41:19)
    at async Command.parseAsync (/usr/src/app/node_modules/nest-commander/node_modules/commander/lib/command.js:917:5)
    at async CommandRunnerService.run (/usr/src/app/node_modules/nest-commander/src/command-runner.service.js:129:9)
    at async Function.runApplication (/usr/src/app/node_modules/nest-commander/src/command.factory.js:29:9)
    at async Function.run (/usr/src/app/node_modules/nest-commander/src/command.factory.js:11:21)
    at async bootstrap (/usr/src/app/dist/cron.js:7:5)

tho, but its fine i can check this out by myself

#

thx bro 🙂 really appreciate all the effort u make to a complete stranger

#

my questionSet was not imported inside CommandModule

#

❤️ good night