【发布时间】:2021-02-23 10:42:10
【问题描述】:
如何配置 npm pre-hook,它应该检查未提交的 git 更改并仅在没有未提交的 git 更改时才允许主脚本运行。
我没用哈士奇,如果不用哈士奇也能搞定就好了。
系统:Windows
外壳:Powershell Core
【问题讨论】:
标签: node.js git npm npm-scripts
如何配置 npm pre-hook,它应该检查未提交的 git 更改并仅在没有未提交的 git 更改时才允许主脚本运行。
我没用哈士奇,如果不用哈士奇也能搞定就好了。
系统:Windows
外壳:Powershell Core
【问题讨论】:
标签: node.js git npm npm-scripts
基于这个answer,考虑在你的 npm pre-hook 中添加以下命令:
git diff-index --quiet HEAD --
当没有未提交的 git 更改时,此 git 命令以零 (0) 退出代码退出,否则当有要提交的更改时,它以非零代码 (1) 退出。
注意:通过 Powershell 运行 git 命令,然后通过运行 $LastExitCode(或 *Nix 上的 echo $?)检查其退出代码/sub>
请考虑 package.json 中以下人为设计的 scripts 部分:
package.json
"scripts": {
"prefoo": "git diff-index --quiet HEAD --",
"foo": "echo \"Hello World\""
},
运行npm run foo 将:
foo 脚本,即它将打印 “Hello World”。foo 脚本。【讨论】: