Skip to content

Instantly share code, notes, and snippets.

@svewag
Last active April 14, 2019 20:29
Show Gist options
  • Save svewag/4db7e1fb6b27ac5253453b4500a09829 to your computer and use it in GitHub Desktop.
Save svewag/4db7e1fb6b27ac5253453b4500a09829 to your computer and use it in GitHub Desktop.
reinstall node-modules after package-lock.json or .nvmrc has changed
#!/usr/bin/env bash
files=("package-lock.json" ".nvmrc")
for file in "${files[@]}"; do
# The HEAD@{1} is a special notation for the commit that HEAD used to be
# at prior to the original reset commit (1 change ago)
# See more: https://git-scm.com/docs/git-reflog
if [[ $(git diff HEAD@{1}..HEAD@{0} -- "${file}" | wc -l) -gt 0 ]]; then
echo
echo -e "======> \e[33;1m»${file}« has changed!\e[0m"
echo -e "======> \e[33;1mreinstalling (dev-)dependencies by running »npm ci«\e[0m"
echo
npm ci
break
fi
done
@svewag
Copy link
Author

svewag commented Apr 14, 2019

In order to reinstall the node-modules when the package-lock.json or the .nvmrc has changed after a git checkout, merge or rebase, you have to setup the following git hooks:

  • post-merge
  • post-rewrite
  • post-checkout

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment