Skip to content

Instantly share code, notes, and snippets.

@ztrayner
Created April 14, 2023 22:01
Show Gist options
  • Save ztrayner/855d3bf0c770afd5b68334823fe086d4 to your computer and use it in GitHub Desktop.
Save ztrayner/855d3bf0c770afd5b68334823fe086d4 to your computer and use it in GitHub Desktop.
.husky/pre-push for prettier to only write modified files that are being pushed
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
echo "Running Prettier on changed files..."
# Get a list of changed files since the last commit
changed_files=$(git diff --name-only --diff-filter=d HEAD @{push} "*.js" "*.jsx" "*.ts" "*.tsx" "*.json" "*.css" "*.scss" "*.md")
# Run Prettier on changed files
if [ -n "$changed_files" ]; then
echo "$changed_files" | xargs ./node_modules/.bin/prettier --write
echo "Prettier has finished formatting the changed files."
# Check for uncommitted changes due to Prettier
if ! git diff --quiet; then
echo "Prettier made changes to the following files:"
git diff --name-only
echo "Please review and commit the changes before pushing."
exit 1
fi
else
echo "No changed files detected. Skipping Prettier."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment