Skip to content

Instantly share code, notes, and snippets.

@taurus227
Forked from betorobson/post-checkout
Last active July 26, 2022 20:49
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save taurus227/28960de89e6c43bb3d492125368f1224 to your computer and use it in GitHub Desktop.
Save taurus227/28960de89e6c43bb3d492125368f1224 to your computer and use it in GitHub Desktop.
git hook to run `npm install` after git pull, merge, rebase or checkout if package.json was changed
# The name for this gist (starts with exclamation mark, because the name of the gist is the name of the first file in ASCIIbetical order)

Git hooks for NPM Projects

1. Copy theses files into your hooks folder

$ mkdir hooks && curl -L -o hooks/post-checkout -O https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224/raw/post-checkout && curl -L -o hooks/post-merge -O https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224/raw/post-merge && curl -L -o hooks/post-rewrite -O https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224/raw/post-rewrite

2. Add an installation script into your package.json

...
  "scripts": {                                                       
    "install": "cp ./hooks/* .git/hooks/ && chmod -R a+x .git/hooks/"
  },                                                                 
...

3. Commit and push to remote repository

At this point it will work for the whole team as soon as they all run npm install

Thanks to these guys:

#!/usr/bin/env bash
# from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
changed_files="$(git diff-tree -r --name-only --no-commit-id $1 $2)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && echo "$0: Running $2..." && eval "$2"
}
check_run package.json "npm prune && npm install"
#!/usr/bin/env bash
# from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && echo "$0: Running $2..." && eval "$2"
}
check_run package.json "npm prune && npm install"
#!/bin/bash
# from https://gist.github.com/taurus227/28960de89e6c43bb3d492125368f1224
if [[ "$1" == "rebase" ]]
then
$(dirname "$0")/post-merge
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment