仅当文件不存在时运行 nx 目标
我有一个具有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您处于 bash 环境中,您可以将准备脚本修改为以下内容(请注意,为了便于阅读,原始命令已用省略号缩短)。
上面的准备脚本仍然会运行,但是如果配置文件已经存在,它不应该花费任何时间来复制配置文件。
CONFIG=firebase.config.json 只是将我们的文件放入 bash 环境变量中,这样我们就可以在多个地方使用它(有助于防止拼写错误)。如果
$CONFIG
包含与现有文件相对应的文件名,[ -f "$CONFIG" ]
将返回 true。如果返回 true,则会短路||
(OR) 命令。如果您想进一步验证此技术,您可以使用命令
[ -f somefile.txt ] || 在终端测试此概念。 echo“文件不存在”
。如果somefile.txt
不存在,则echo
将运行。如果文件确实存在,则echo
将不会运行。稍微相关的旁注:虽然您显然可以在
package.json
配置中完成这一切,但如果您的 nx 工作区将增长以包含其他库或应用程序,我强烈建议将所有内容分开将工作区配置添加到默认的 nx 配置文件中:nx.json
、workspace.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).
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"
. Ifsomefile.txt
does not exist, then theecho
will run. If the file does exist, then theecho
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-projectproject.json
files for the sake of readability/maintainability.Best of luck!