所以我尝试使用nest-commander来构建播种机,我在示例和文档中是如何做到的,我必须创建src/commands文件夹,然后创建seed.command.ts文件
import { Command, CommandRunner } from 'nest-commander';
@Command({
name: 'my-exec',
arguments: '<task>',
options: { isDefault: true },
})
export class BasicCommand extends CommandRunner {
async run(inputs: string[], options: Record<string, any>): Promise<void> {
console.log('here will be my seeder');
}
}
这是我的 main.ts 文件
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { Logger } from '@nestjs/common';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { CommandFactory } from 'nest-commander';
const port = process.env.PORT || 3000;
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await CommandFactory.run(AppModule, ['warn', 'error']);
app.enableCors();
const options = new DocumentBuilder()
.addBearerAuth()
.setTitle('CHS')
.setDescription('CHS API')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('api', app, document);
await app.listen(port);
Logger.log(`CHS is running on port: ${port} `, 'Bootstrap');
}
bootstrap();
在 app.module.ts 文件中,我在提供中添加了 BasicCommand:[ BasicCommand ]
然后我尝试使用以下命令运行此命令播种器文件:crun my-exec 但收到此错误:2023-09-21T11:36:54.000556816Z:未知命令 my-exec
请有人解释一下我做错了什么......
您是否在 AppModule 中设置了提供程序?
尝试检查这个...
在您的应用程序模块中:
@Module({
imports: [
//your imports
],
providers: [
other providers...,
...NestCommands
],
})
export class AppModule {}
NestCommands 在哪里:
export const NestCommands = [
BasicCommand,
other commands...
];