Skip to content

Instantly share code, notes, and snippets.

@zirkelc
Created December 27, 2021 10:40
Show Gist options
  • Save zirkelc/eb281b5b1958c2cd4d844c527b69c2c8 to your computer and use it in GitHub Desktop.
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
#!/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
@zirkelc
Copy link
Author

zirkelc commented Dec 27, 2021

Test on macOS with zsh shell.

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