【问题标题】:NPM script should fail if there are uncommitted git changes如果有未提交的 git 更改,NPM 脚本应该会失败
【发布时间】:2021-02-23 10:42:10
【问题描述】:

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

系统:Windows
外壳:Powershell Core

【问题讨论】:

    标签: node.js git npm npm-scripts


    【解决方案1】:

    基于这个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 将:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-15
      • 2012-09-16
      • 1970-01-01
      • 2019-07-12
      • 2020-12-02
      • 2011-09-16
      • 2014-10-30
      • 2021-12-18
      相关资源
      最近更新 更多