#!/bin/sh # Format JavaScript/JSX files using Prettier # # Usage: # ./scripts/prettier.sh - Check formatting (read-only) # ./scripts/prettier.sh --fix - Auto-fix formatting issues # ./scripts/prettier.sh --check - Check only (no formatting, for CI) set -e FIX_MODE=false CHECK_MODE=false # Parse command line arguments while [ $# -gt 0 ]; do case $1 in --fix) FIX_MODE=true shift ;; --check) CHECK_MODE=true shift ;; -h|--help) echo "Usage: $0 [--fix|--check]" echo " --fix Auto-fix formatting issues" echo " --check Check only (no formatting, for CI)" echo " -h, --help Show this help message" exit 0 ;; *) echo "Unknown option: $1" echo "Use --help for usage information" exit 1 ;; esac done echo "Running Prettier on JavaScript/JSX files..." # Show which files will be checked echo "JavaScript/JSX files to check:" js_files=$(find . -type f \( -name '*.js' -o -name '*.jsx' -o -name '*.mjs' \) -not -path './.git/*' -not -path '*/node_modules/*' 2>/dev/null | sort) if [ -n "$js_files" ]; then echo "$js_files" | sed 's/^/ /' echo "" echo "Found $(echo "$js_files" | grep -c .) JavaScript/JSX file(s) to check" else echo " (no JavaScript/JSX files found)" fi echo "" # prettier exits 2 on an unmatched pattern, so bail before invoking it. if [ -z "$js_files" ]; then echo "Nothing to check." exit 0 fi # Let prettier expand the glob itself. Passing the find output above would break # on the JSX paths containing spaces and non-ASCII. js_glob='**/*.{js,jsx,mjs}' if [ "$CHECK_MODE" = true ]; then echo "Checking formatting (no changes)..." bunx prettier --check "$js_glob" elif [ "$FIX_MODE" = true ]; then echo "Formatting files..." bunx prettier --write "$js_glob" else echo "Checking formatting (read-only)..." bunx prettier --check "$js_glob" fi echo "Prettier formatting complete!"