blob: 585993111fdce7058aa688303f38949c3cf8c035 [file] [log] [blame]
Philip Zeyliger8b00db12025-04-25 18:41:38 +00001#!/bin/bash
2set -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
8CHECK_MODE=false
9if [ "$1" = "check" ]; then
10 CHECK_MODE=true
11 echo "Running in check mode (formatting will not be modified)"
12else
13 echo "Running in fix mode (formatting will be modified)"
14fi
15
Philip Zeyliger80ff7172025-07-28 15:26:02 -070016# Run both formatters in parallel
17echo "Running Go and webui formatters in parallel..."
Philip Zeyliger8b00db12025-04-25 18:41:38 +000018
Philip Zeyliger80ff7172025-07-28 15:26:02 -070019# Run Go formatting in background
20(
21 echo "Checking Go formatting..."
Philip Zeyliger8b00db12025-04-25 18:41:38 +000022 if [ "$CHECK_MODE" = true ]; then
Philip Zeyliger80ff7172025-07-28 15:26:02 -070023 # 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 Zeyliger8b00db12025-04-25 18:41:38 +000028 exit 1
Philip Zeyliger80ff7172025-07-28 15:26:02 -070029 else
30 echo "Go formatting check passed"
Philip Zeyliger8b00db12025-04-25 18:41:38 +000031 fi
32 else
Philip Zeyliger80ff7172025-07-28 15:26:02 -070033 # In fix mode, we apply the formatting
34 echo "Fixing Go formatting with gofumpt"
35 gofumpt -w .
36 echo "Go formatting complete"
Philip Zeyliger8b00db12025-04-25 18:41:38 +000037 fi
Philip Zeyliger80ff7172025-07-28 15:26:02 -070038) &
39GO_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) &
61WEBUI_PID=$!
62
63# Wait for both processes and capture exit codes
64wait $GO_PID
65GO_EXIT=$?
66wait $WEBUI_PID
67WEBUI_EXIT=$?
68
69# Exit with error if either formatter failed
70if [ $GO_EXIT -ne 0 ] || [ $WEBUI_EXIT -ne 0 ]; then
71 exit 1
Philip Zeyliger8b00db12025-04-25 18:41:38 +000072fi
73
74if [ "$CHECK_MODE" = true ]; then
75 echo "All formatting checks passed"
76else
77 echo "All formatting has been fixed"
78fi