仅当文件不存在时运行 nx 目标

发布于 2025-01-15 09:04:45 字数 1027 浏览 1 评论 0原文

我有一个具有 build 步骤的项目,但是,我需要确保在运行 build 之前文件 firebase.config.json 存在> 命令。

这样,我就有了两个 NPM 脚本:

// package.json
{
  ...,
  "nx": {
    "targets": {
      "prepare": {
        "outputs": ["firebase.config.json"]
      },
      "build": {
        "outputs": ["dist"],
        "dependsOn": [
          {
            "target": "prepare",
            "projects": "self"
          }
        ]
      }
    }
  },
  "scripts": {
    "prepare": "firebase apps:sdkconfig web $FIREBASE_APP_ID_SHOP --json | jq .result.sdkConfig > firebase.config.json",
    "build": "VITE_FIREBASE_CONFIG=$(cat ./firebase.config.json) vite build",
  },
  ...
}

因此,通过上面的内容,每次运行 nx build app 时,它都会首先运行 prepare 并构建 firebase.config。 json 文件。

但是,每次我对项目中的任何源文件进行更改时,即使 firebase.config.json 已经存在,prepare 也会重新运行。

如果 outputs 下声明的文件不存在,nx 是否可以仅运行目标?

I have a project which has a build step, however, I need to make sure that the file firebase.config.json exists before running the build command.

With that, I have two NPM scripts:

// package.json
{
  ...,
  "nx": {
    "targets": {
      "prepare": {
        "outputs": ["firebase.config.json"]
      },
      "build": {
        "outputs": ["dist"],
        "dependsOn": [
          {
            "target": "prepare",
            "projects": "self"
          }
        ]
      }
    }
  },
  "scripts": {
    "prepare": "firebase apps:sdkconfig web $FIREBASE_APP_ID_SHOP --json | jq .result.sdkConfig > firebase.config.json",
    "build": "VITE_FIREBASE_CONFIG=$(cat ./firebase.config.json) vite build",
  },
  ...
}

So with the above, every time I run nx build app it will first run prepare and build the firebase.config.json file.

However, every time I make a change to any of the source files inside my project, prepare re-runs even though the firebase.config.json is already present.

Is it possible for nx to only run a target if the file declared under outputs is not present?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一指流沙 2025-01-22 09:04:45

如果您处于 bash 环境中,您可以将准备脚本修改为以下内容(请注意,为了便于阅读,原始命令已用省略号缩短)。

// package.json
{
  "scripts":{
    "prepare": "CONFIG=firebase.config.json; [ -f \"$CONFIG\" ] || firebase apps:sdkconfig ... | jq ... > \"$CONFIG\""
  }
}

上面的准备脚本仍然会运行,但是如果配置文件已经存在,它不应该花费任何时间来复制配置文件。

CONFIG=firebase.config.json 只是将我们的文件放入 bash 环境变量中,这样我们就可以在多个地方使用它(有助于防止拼写错误)。如果 $CONFIG 包含与现有文件相对应的文件名,[ -f "$CONFIG" ] 将返回 true。如果返回 true,则会短路 || (OR) 命令。

如果您想进一步验证此技术,您可以使用命令 [ -f somefile.txt ] || 在终端测试此概念。 echo“文件不存在”。如果 somefile.txt 不存在,则 echo 将运行。如果文件确实存在,则echo不会运行。

稍微相关的旁注:虽然您显然可以在 package.json 配置中完成这一切,但如果您的 nx 工作区将增长以包含其他库或应用程序,我强烈建议将所有内容分开将工作区配置添加到默认的 nx 配置文件中:nx.jsonworkspace.json 以及每个项目的 project.json 文件为了可读性/可维护性。

祝你好运!

If you are in a bash environment you can modify your prepare script to be the following (note the original command has been shortened with ellipses for readability).

// package.json
{
  "scripts":{
    "prepare": "CONFIG=firebase.config.json; [ -f \"$CONFIG\" ] || firebase apps:sdkconfig ... | jq ... > \"$CONFIG\""
  }
}

The above prepare script will still run, but it should not spend any time reproducing the configuration file if it already exists.

CONFIG=firebase.config.json is just putting our file in a bash environment variable so we can use it in multiple places (helps prevent typos). [ -f "$CONFIG" ] will return true if $CONFIG holds a filename which corresponds to an existing file. If it returns true, it will short-circuit the || (OR) command.

If you want further verification of this technique, you can test this concept at the terminal with the command [ -f somefile.txt ] || echo "File does not exist". If somefile.txt does not exist, then the echo will run. If the file does exist, then the echo will not run.

A slightly-related side-note: while you clearly can do this all in the package.json configuration, if your nx workspace is going to grow to include other libraries or applications, I highly recommend splitting up all your workspace configuration into the default nx configuration files: nx.json, workspace.json, and the per-project project.json files for the sake of readability/maintainability.

Best of luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文