Based on this answer consider adding the following command in your npm pre-hook:
git diff-index --quiet HEAD --
This git
command exits with a zero (0
) exit code when there are no uncommitted git changes, otherwise it exits with a non-zero code (1
) when there are changes to commit.
Note: Run that git
command via Powershell then check its exit code by running $LastExitCode
(Or echo $?
on *Nix)
Consider the following contrived scripts
section in package.json:
package.json
"scripts": {
"prefoo": "git diff-index --quiet HEAD --",
"foo": "echo \"Hello World\""
},
Running npm run foo
will either:
- Run the
foo
script when there are no uncommitted git changes, i.e. it will print "Hello World".
- Or fail to run the
foo
script when uncommitted git changes exist.