我的主要 npm 脚本是“npm run build”。
我想要做的是编写另一个脚本,每当“npm run build”失败时,该脚本就会像异常处理中的 catch 一样执行。
您可以在 npm 脚本中使用OR 运算符( )。||两者都支持它sh,cmd.exe因此它可以跨平台工作。
请考虑package.jsonscripts部分中的以下示例:
"scripts": {
"build": "<your build script here> || npm run fallback",
"fallback": "echo \"Oops! Running another script....\""
},
Run Code Online (Sandbox Code Playgroud)
解释:
<your build script here>中的部分build。fallback仅当操作符左侧的构建命令||以非零退出状态退出(即失败)时,该脚本才会运行。npm run fallback右侧的部分||永远不会被调用。"echo \"Oops! Running another script....\""中fallback的 应该替换为失败时要执行的命令build。上面的示例使用了另一个脚本,其名称fallback主要用于解释目的,但是您可以只有一个build脚本,例如:
"scripts": {
"build": "<your build script here> || echo \"Oops! Running another script...\""
},
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2973 次 |
| 最近记录: |