1

How to configure npm pre-hook which should check for uncommitted git changes and allow main script to run only if there are no uncommitted git changes.
I'm not using husky and it would be great if it can be done without using it.

System: Windows
Shell: Powershell Core

0

1 Answer 1

1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.