简体   繁体   English

如何使用 Nx 运行单个 typescript 文件

[英]How to run a single typescript file with Nx

I have a Nx Node project that uses pre-processed summary stats files.我有一个使用预处理摘要统计文件的 Nx Node 项目。 These stats files require a lot of processing and rarely need to be generated.这些统计文件需要大量处理,很少需要生成。 However, once they are generated the primary Node app runs quickly.但是,一旦生成它们,主节点应用程序就会快速运行。

I want a pre-processing step that can be run manually to generate these stats assets.我想要一个可以手动运行的预处理步骤来生成这些统计资产。 I want to use Nx's typescript, prettier and linting support to write the code to generate these stats files.我想使用 Nx 的 typescript,更漂亮和 linting 支持来编写代码来生成这些统计文件。 But creating a whole Node Nx library for each processing step seems like a lot of unnecessary infrastructure.但是为每个处理步骤创建一个完整的 Node Nx 库似乎是很多不必要的基础设施。

There is an option to run custom commands: https://nx.dev/latest/node/executors/run-commands-builder .有一个运行自定义命令的选项: https://nx.dev/latest/node/executors/run-commands-builder I want to do this with a Typescript file but the documentation only shows shell commands or combining pre-existing Nx commands.我想使用 Typescript 文件执行此操作,但文档仅显示 shell 命令或组合预先存在的 Nx 命令。 AFAICT I can't just point this to a typescript file. AFAICT 我不能只将其指向 typescript 文件。

Question is:问题是:

  1. Can I run a custom command to trigger a single typescript file?我可以运行自定义命令来触发单个 typescript 文件吗?
  2. Is it more idiomatic to just create a whole Node library for each file that generates these stats?为每个生成这些统计信息的文件创建一个完整的 Node 库是否更习惯?

Thanks谢谢

You don't have to create a separate library for each.您不必为每个创建单独的库。 If they all fit within the category of "generate my preprocessed stats" then they can (maybe should) go together.如果它们都属于“生成我的预处理统计信息”的类别,那么它们可以(也许应该)go 一起。

As for triggering a single TypeScript file, TypeScript files are not meant to be triggered.至于触发单个 TypeScript 文件,TypeScript 文件并不意味着被触发。 Instead, we compile them into JS and run the JS files using Node.相反,我们将它们编译成 JS 并使用 Node.js 运行 JS 文件。 In general, this translates to the following commands:通常,这会转换为以下命令:

  1. Run tsc to compile your TypeScript files.运行tsc编译您的 TypeScript 文件。
  2. Run node <your-main-file>.js to execute your JS file(s).运行node <your-main-file>.js来执行你的 JS 文件。

Compilation汇编

Command命令 Description描述
tsc index.ts Compile a single file named index.ts in the current directory.在当前目录中编译一个名为index.ts的文件。
tsc *.ts Compile all TypeScript files in the current directory.编译当前目录下的所有TypeScript文件。
tsc --project tsconfig.stats-type1.json or tsc --project tsconfig.stats-type1.json
tsc --project tsconfig.stats-type2.json or tsc --project tsconfig.stats-type2.json
... ...
Compile according to the specified config file from the current directory.根据当前目录下的指定配置文件进行编译。

The last example shows how you could have several tsconfig.json files in your library, one for each stats type, in order to create different variations of your compilation according to your needs.最后一个示例显示了如何在库中拥有多个tsconfig.json文件,每个统计类型一个,以便根据需要创建不同的编译变体。

See tsc CLI options for more info.有关详细信息,请参阅tsc CLI 选项

Execution执行

After that, depending on where you told tsc to output your files to, pass the created file to node .之后,根据您告诉tsc到 output 文件的位置,将创建的文件传递给node

In case you want to run more than one file in a single command, there are a few ways to do that.如果您想在一个命令中运行多个文件,有几种方法可以做到这一点。 One would be creating a main file which imports and calls functions from all other files.一种是创建一个主文件,该文件从所有其他文件中导入和调用函数。

Good luck.祝你好运。

I've came across to a similar use case as yours: I've a NX app that needs some sort of scripts to run (not always, so I can't put them in some utility in the app).我遇到了与您类似的用例:我有一个 NX 应用程序,它需要某种脚本才能运行(并非总是如此,所以我不能将它们放在应用程序的某个实用程序中)。 Those scripts don't need to be transpiled to js, but I want to use all the utils and libs in my nx monorepo.这些脚本不需要转译为 js,但我想使用我的 nx monorepo 中的所有 utils 和 libs。

My solution was to create a script folder inside my app and exclude it from the build process, then on the workspace.json (or project.json) I added this target:我的解决方案是在我的应用程序中创建一个脚本文件夹并将其从构建过程中排除,然后在 workspace.json(或 project.json)上我添加了这个目标:

{
  "run-script": {
    "executor": "@nrwl/workspace:run-commands",
    "configurations": {
      "my-script": {
        "commands": [
          "ts-node apps/path/to/my/script.ts"
        ]
      }
    }
  }
}

and I can run it using the command:我可以使用以下命令运行它:

nx run my-project:run-script:my-script

Executing the compiled version of the script avoids a lot of pain if you have specialized tsconfig files and dependent libraries used by the script.如果您有专门的 tsconfig 文件和脚本使用的依赖库,则执行脚本的编译版本可以避免很多麻烦。 Executing the following:执行以下操作:

nx g @nrwl/workspace:run-commands run-script --project my-project --command 'node dist/apps/my-project/main.js'

Creates a command that uses the javascript compilation of the script bypassing all of the typescript configuration.创建一个使用脚本的 javascript 编译绕过所有 typescript 配置的命令。 It produces the following in apps/my-project/project.json它在 apps/my-project/project.json 中产生以下内容

    "run-script": {
      "executor": "@nrwl/workspace:run-commands",
      "outputs": [],
      "options": {
        "command": "node dist/apps/my-project/main.js"
      }
    }

Which can then be executed by:然后可以通过以下方式执行:

nx run my-project:run-script

Thanks to alessiopremoli and Dharman for pointing me in the right direction.感谢 alessiopremoli 和 Dharman 为我指明了正确的方向。

After six months I'm back to this issue and both my old answer and the other added in the meantime don't work for my (new) case: I have to run a script with specific imports from the nx monorepo.六个月后,我回到了这个问题,我的旧答案和同时添加的另一个答案都不适用于我的(新)案例:我必须运行一个脚本,其中包含来自 nx monorepo 的特定导入。 After digging through the updates I've come up with a new solution.在挖掘更新后,我想出了一个新的解决方案。

The main idea is to store your app / lib related scripts in a sub-folder of the project folder to which they're related and then add to the workspace.json or project.json a specific build command for this folder, something like:主要思想是将您的 app / lib 相关脚本存储在与它们相关的项目文件夹的子文件夹中,然后添加到workspace.jsonproject.json此文件夹的特定构建命令,例如:

{
    "_build_scripts": {
        "executor": "@nrwl/node:build",
        "outputs": [
            "{options.outputPath}"
        ],
        "options": {
            "outputPath": "dist/libs/my-project-path",
            "tsConfig": "libs/my-project-path/tsconfig.lib.json",
            "packageJson": "libs/my-project-path/package.json",
            "main": "libs/my-project-path/scripts/myscript.ts",
            "assets": ["if needed"]
        }
    }
}

then you can set up your run script command, always in the workspace.json or project.json :然后你可以设置你的运行脚本命令,总是在workspace.jsonproject.json中:

{
  "run-script": {
    "executor": "@nrwl/workspace:run-commands",
    "configurations": {
      "my-script": {
        "commands": [
          "./node_modules/.bin/nx run my-project:_build_script",
          "node dist/my-project-path/main.js"
        ],
        "parallel": false
      }
    }
  }
}

Two things to keep in mind: parallel set to false is mandatory, you need the build step before executing the script;要记住两件事: parallel设置为 false 是强制性的,您需要在执行脚本之前执行构建步骤; in my case, before the node main.js I had to cd in the directory since fs was reading some files resolving paths incompatible with the monorepo root.就我而言,在node main.js之前,我必须在目录中 cd,因为fs正在读取一些解析与 monorepo 根目录不兼容的路径的文件。

In the end you can run:最后你可以运行:

npx nx run my-project:run-script:my-script

I had a similar situation where I wanted to run a nx node app once to perform a task and output the result.我有类似的情况,我想运行一次nx 节点应用程序来执行任务,结果是 output。 I appended this to my app's targets:我将此附加到我的应用程序的目标中:

// project.json

{
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "sourceRoot": "apps/app-name/src",
  "projectType": "application",
  "targets": {
    ...
    "run": {
      "executor": "@nrwl/workspace:run-commands",
      "options": {
        "commands": [
          "nx run app-name:build:production",
          "node dist/apps/app-name/main.js"
        ]
      }
    }
  },
  "tags": []
}

Then to run the app once: nx run app-name:run然后运行应用程序一次: nx run app-name:run

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM