| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | set -e |
| 3 | |
| 4 | # Script to run formatters for both Go and webui files |
| 5 | # Usage: bin/run-formatters.sh [check] |
| 6 | # If 'check' is provided, only checks formatting without making changes |
| 7 | |
| 8 | CHECK_MODE=false |
| 9 | if [ "$1" = "check" ]; then |
| 10 | CHECK_MODE=true |
| 11 | echo "Running in check mode (formatting will not be modified)" |
| 12 | else |
| 13 | echo "Running in fix mode (formatting will be modified)" |
| 14 | fi |
| 15 | |
| Philip Zeyliger | 80ff717 | 2025-07-28 15:26:02 -0700 | [diff] [blame^] | 16 | # Run both formatters in parallel |
| 17 | echo "Running Go and webui formatters in parallel..." |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 18 | |
| Philip Zeyliger | 80ff717 | 2025-07-28 15:26:02 -0700 | [diff] [blame^] | 19 | # Run Go formatting in background |
| 20 | ( |
| 21 | echo "Checking Go formatting..." |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 22 | if [ "$CHECK_MODE" = true ]; then |
| Philip Zeyliger | 80ff717 | 2025-07-28 15:26:02 -0700 | [diff] [blame^] | 23 | # In check mode, we want to display the files that need formatting and exit with error if any |
| 24 | FILES_TO_FORMAT=$(gofumpt -l .) |
| 25 | if [ -n "$FILES_TO_FORMAT" ]; then |
| 26 | echo "The following Go files need formatting:" |
| 27 | echo "$FILES_TO_FORMAT" |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 28 | exit 1 |
| Philip Zeyliger | 80ff717 | 2025-07-28 15:26:02 -0700 | [diff] [blame^] | 29 | else |
| 30 | echo "Go formatting check passed" |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 31 | fi |
| 32 | else |
| Philip Zeyliger | 80ff717 | 2025-07-28 15:26:02 -0700 | [diff] [blame^] | 33 | # In fix mode, we apply the formatting |
| 34 | echo "Fixing Go formatting with gofumpt" |
| 35 | gofumpt -w . |
| 36 | echo "Go formatting complete" |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 37 | fi |
| Philip Zeyliger | 80ff717 | 2025-07-28 15:26:02 -0700 | [diff] [blame^] | 38 | ) & |
| 39 | GO_PID=$! |
| 40 | |
| 41 | # Run webui formatting in background |
| 42 | ( |
| 43 | echo "Checking webui formatting..." |
| 44 | if [ -d "./webui" ]; then |
| 45 | cd webui |
| 46 | if [ "$CHECK_MODE" = true ]; then |
| 47 | echo "Checking webui formatting with Prettier" |
| 48 | if ! npx prettier@3.5.3 --check .; then |
| 49 | echo "Webui files need formatting" |
| 50 | exit 1 |
| 51 | fi |
| 52 | else |
| 53 | echo "Fixing webui formatting with Prettier" |
| 54 | npx prettier@3.5.3 --write . |
| 55 | echo "Webui formatting complete" |
| 56 | fi |
| 57 | else |
| 58 | echo "No webui directory found, skipping Prettier check" |
| 59 | fi |
| 60 | ) & |
| 61 | WEBUI_PID=$! |
| 62 | |
| 63 | # Wait for both processes and capture exit codes |
| 64 | wait $GO_PID |
| 65 | GO_EXIT=$? |
| 66 | wait $WEBUI_PID |
| 67 | WEBUI_EXIT=$? |
| 68 | |
| 69 | # Exit with error if either formatter failed |
| 70 | if [ $GO_EXIT -ne 0 ] || [ $WEBUI_EXIT -ne 0 ]; then |
| 71 | exit 1 |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 72 | fi |
| 73 | |
| 74 | if [ "$CHECK_MODE" = true ]; then |
| 75 | echo "All formatting checks passed" |
| 76 | else |
| 77 | echo "All formatting has been fixed" |
| 78 | fi |