Created
December 27, 2021 10:40
-
-
Save zirkelc/eb281b5b1958c2cd4d844c527b69c2c8 to your computer and use it in GitHub Desktop.
Run npm install automatically after git pull if package-lock.json has changed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
. "$(dirname "$0")/_/husky.sh" | |
IFS=$'\n' | |
# regex supports mono-repos with a package.json at root-level and at package-level | |
PACKAGE_LOCK_REGEX="(^packages\/.*\/package-lock\.json)|(^package-lock\.json)" | |
# extract all paths to package-lock.json files | |
PACKAGES=("$(git diff --name-only HEAD@{1} HEAD | grep -E "$PACKAGE_LOCK_REGEX")") | |
if [[ ${PACKAGES[@]} ]]; then | |
for package in $PACKAGES; do | |
echo "📦 $package was changed. Running npm install to update your dependencies..." | |
DIR=$(dirname package) | |
cd "$DIR" && npm install | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Test on macOS with
zsh
shell.