如果有未提交的 git 更改,NPM 脚本应该会失败

NPM script should fail if there are uncommitted git changes

如何配置 npm pre-hook,它应该检查未提交的 git 更改并允许主脚本仅在没有未提交的 git 更改时才 运行。
我没有用哈士奇,如果不用它也能搞定就好了。

系统:Windows
Shell:Powershell 核心

基于此 answer 考虑在您的 npm pre-hook 中添加以下命令:

git diff-index --quiet HEAD --

git 命令在没有未提交的 git 更改时以零 (0) 退出代码退出,否则以非零代码退出 (1) 当有更改要提交时。

注意: 运行 通过 Powershell 命令 git 然后通过 运行ning 检查其退出代码 $LastExitCode(或 *Nix 上的 echo $?

考虑 package.json 中以下人为设计的 scripts 部分:

package.json

"scripts": {
  "prefoo": "git diff-index --quiet HEAD --",
  "foo": "echo \"Hello World\""
},

运行宁 npm run foo 将:

  • 运行 foo 脚本,当没有未提交的 git 更改时,即它会打印 "Hello World".
  • 或者在存在未提交的 git 更改时无法 运行 foo 脚本。