[Bash] Run `npm install` when package.json changes in githook

Git hooks

In root of project:

mkdir -p hooks
cd hooks
touch post-merge

post-merge:

#! /bin/bash

# all stdio in this script after this line will be sent to the log file
# note that we're appending so that the log file is not overwritten every time this script is called
exec >> log/hooks-out.log 2>&1

if git diff-tree --name-only --no-commit-id ORIG_HEAD HEAD | grep --quiet 'package.json'; then
  echo "$(date): Running npm install because package.json changed"
  # redirecting stdout to dev/null trashes quiets the stdout, but it's stderr will go to our log file
  npm install > /dev/null
else
  echo "$(date): No changes in package.json found"
fi

Give premission:

chmod +x hooks/post-merge

Link to .git:

ln -fv hooks/post-merge .git/hooks/

Later if co-worker upgrade some packages, when we pull the changes, it will trigger `npm install

原文地址:https://www.cnblogs.com/Answer1215/p/14398455.html