本文介绍了在命令行运行NestJS脚本中使用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何使用 npm npx ts-node [script.ts] 从命令行运行脚本,就像.

I know how to run a script from command line, using npm or npx ts-node [script.ts] just as stated here.

我的问题有所不同,现在我可以运行脚本了,我可以使用项目模块内部的服务吗?假设我具有这种结构,通常在项目内部由其他模块调用该结构:

My question is different, now that I can run scripts, can I use services that are inside modules in my project? Let's say that I have this structure that it is normally called inside the project by other modules:

foo/foo.module.ts

foo/foo.module.ts

import { HttpModule, Module } from '@nestjs/common';

@Module({
  providers: [FooService],
  imports: [HttpModule],
  exports: [FooService]
})
export class FooModule { }

foo/foo.service.ts

foo/foo.service.ts

import { HttpService, Injectable } from '@nestjs/common';

@Injectable()
export class FooService {
    constructor(
        private readonly httpService: HttpService,
    ) {}

    bar() {
        console.log('do stuff');
    }
}

如何在文件/src/script.ts 中调用 bar(),然后调用 npx ts-node script.ts 保留所有进口商品?谢谢.

how can I call bar() inside the file /src/script.ts and then call npx ts-node script.ts keeping all the imports? Thank you.

推荐答案

假设您有一个这样的应用程序模块:

Let say you have an application module like this:

import { Module } from '@nestjs/common';
import { UsersModule } from './users/users.module';

@Module({
  imports: [
    UsersModule,
  ],
})
export class ApplicationModule {}

以及UsersModule以这种方式使用的UserService:

And an UserService used by UsersModule in this way:

import { Module } from '@nestjs/common';

@Module({
  providers: [UsersService],
  exports: [UsersService],
})
export class UsersModule {}

您想创建一个命令来直接从命令行创建新用户.

And you want to create a command to create a new user directly from command line.

您可以创建一个名为 console.ts 的文件,并放置以下内容:

You can create a file named console.ts and put the following content:

import { NestFactory } from '@nestjs/core';
import { ApplicationModule } from './application.module';
import { UsersService } from './users/users.service';

async function bootstrap() {
  const application = await NestFactory.createApplicationContext(
    ApplicationModule,
  );

  const command = process.argv[2];

  switch (command) {
    case 'create-administrator-user':
      const usersService = application.get(UsersService);
      await usersService.create({
        username: 'administrator',
        password: 'password',
      });
      break;
    default:
      console.log('Command not found');
      process.exit(1);
  }

  await application.close();
  process.exit(0);
}

bootstrap();

现在,在您的 package.json 中,您可以创建以下脚本:

And now in your package.json you can create the following script:

"execute": "ts-node ./src/console.ts"

现在,您可以像下面的示例一样在NestJS上下文中调用自定义命令:

Now you have the ability to call a custom command in a NestJS context like in the following example:

// Using Yarn
yarn execute create-administrator-user

// Using NPM
npm run execute create-administrator-user

这篇关于在命令行运行NestJS脚本中使用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:45
查看更多