如何使用NX生成OpenAPI JSON?

发布于 2025-02-12 02:26:10 字数 877 浏览 0 评论 0原文

我有 nestjs 在monorepo和 nx 是我们的构建系统。

我正在寻找一种在构建时间编译和运行TS文件的方法。 TS文件具有生成OpenApi.json文件的逻辑。 TS文件从项目中的main.js和其他文件导入函数。

代码:

  
  const options = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  const outputPath = path.resolve(process.cwd(), 'swagger.json');
  writeFileSync(outputPath, JSON.stringify(document), { encoding: 'utf8'});

  await app.close();

问题:由于NX生成了一个文件main.js。如何编译和运行我的TS文件?

PS如果我将其放入main.js的bootstrap方法中,则相同的代码可以正常工作,但这是我不想要的。

I am having NestJS projects in a Monorepo and NX is our build system.

I am looking for a way to compile and run a TS file at build time. The TS file has a logic to generate openapi.json file. TS file imports functions from main.js and other files in the project.

Code:

  
  const options = new DocumentBuilder()
    .setTitle('Cats example')
    .setDescription('The cats API description')
    .setVersion('1.0')
    .addTag('cats')
    .build();
  const document = SwaggerModule.createDocument(app, options);
  const outputPath = path.resolve(process.cwd(), 'swagger.json');
  writeFileSync(outputPath, JSON.stringify(document), { encoding: 'utf8'});

  await app.close();

Issue: Since the NX generates a single file main.js. How I can compile and run my TS file?

P.S the same code works if I put it in bootstrap method of main.js but that is something I don't want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文