【问题标题】:How to run the Command from nest-commander in CLI?如何在 CLI 中从 nest-commander 运行命令?
【发布时间】:2023-01-18 04:33:25
【问题描述】:
我有一个嵌套JS应用程序并添加了一个CLI 命令根据他们的官方文档使用nest-commander:
https://docs.nestjs.com/recipes/nest-commander
我的目录如下所示:
+ src
+ command
+ test.command.js
- app.controller.spec.ts
- app.controller.ts
- app.module.ts
- app.service.ts
- main.ts
+ test
要启动网络应用程序,我通常执行
yarn start:dev
但是如何启动nest-commander提供的命令呢?似乎官方文档甚至 github 存储库的文档都没有说明任何相关信息。
我需要类似的东西
yarn run test-command
或类似的东西。如何制作?
【问题讨论】:
标签:
node.js
nestjs
nest-commander
【解决方案1】:
As is written in the docs通常你应该构建应用程序(yarn build应该调用nest build它应该输出dist)然后你应该能够调用node dist/path/to/main-cli其中main-cli是你使用@的文件987654327@方法。
您可以设置一个 package.json 脚本,例如 "test-command": "nest build && node dist/path/to/main-cli",这样您就可以运行 yarn test-command,它会为您运行所有步骤。
以上所有内容还假设您在 src 之外也没有编译的 ts 文件。如果是这样,那么您的dist路径将是dist/src/path/to/main-cli,所以请记住这一点。
最后一个选项是使用ts-node 并调用ts-node src/path/to/main-cli,在运行时在内存中编译 TS。