I created a command line application using nest CommandRunner class,and also created a module named JobModule and provided the command file as provider in it.
import { CommandFactory } from 'nest-commander';
import { AppModule } from './app.module';
import { JobModule } from './job.module';
async function bootstrap() {
console.log('BAR');
await CommandFactory.run(JobModule);
}
bootstrap();
this is my jobmodule:
import { Module } from '@nestjs/common';
import { PrismaModule } from './infrastructure/database/prisma.module';
import { LoggerModule } from './common/utils/logger/logger.module';
import { OrderModule } from './modules/order/order.module';
import { VoucherModule } from './modules/voucher/voucher.module';
import { TbsVoucherCommand } from './infrastructure/jobs/tbs.voucher';
@Module({
imports: [PrismaModule, OrderModule, VoucherModule],
providers: [TbsVoucherCommand],
})
export class JobModule {}
and this is my class which extends command runner:
@Command({ name: 'tbs-voucher', description: 'Process TBS Voucher' })
export class TbsVoucherCommand extends CommandRunner {
constructor(
private readonly logger: LoggerService,
private readonly prismaService: PrismaService,
private readonly orderService: OrderService,
) {
super();
}
async run(passedParams: string[], options?: Record<string, any>) {
console.log('Foo');
return this.processTbsVoucher();
}
}
in this case only “BAR” gets logged, but when i pass AppModule which is the my main module,’BAR’ also gets logged.
import { CommandFactory } from 'nest-commander';
import { AppModule } from './app.module';
import { JobModule } from './job.module';
async function bootstrap() {
console.log('BAR');
await CommandFactory.run(AppModule);
}
bootstrap();
i also run this file after npm run build
, with this command : node .distjob tbs-voucher
can someone explain why nest works like that? could it be because of resolving dependencies in the modules i imported inside of the job module?