-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathfmt.sh
More file actions
executable file
·113 lines (98 loc) · 2.75 KB
/
Copy pathfmt.sh
File metadata and controls
executable file
·113 lines (98 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env bash
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
cd "$PROJECT_ROOT"
PRETTIER_PATTERNS=(
"src/**/*.{ts,tsx,js,jsx,json}"
"tests/**/*.{ts,tsx,js,jsx,json}"
"docs/**/*.{md,mdx}"
"*.{json,md}"
)
run_prettier() {
local mode="$1"
local prettier_cmd
if command -v prettier &>/dev/null; then
prettier_cmd=(prettier)
elif command -v bun &>/dev/null; then
prettier_cmd=(bun x prettier)
else
echo "Error: prettier not found. Install Prettier or Bun to run formatting."
exit 1
fi
if [ "$mode" = "--check" ]; then
echo "Checking TypeScript/JSON/Markdown formatting..."
"${prettier_cmd[@]}" --check "${PRETTIER_PATTERNS[@]}"
else
echo "Formatting TypeScript/JSON/Markdown files..."
"${prettier_cmd[@]}" --write "${PRETTIER_PATTERNS[@]}"
fi
}
format_nix() {
if ! command -v nix &>/dev/null; then
echo "Error: nix command not found. Install Nix to format flake.nix."
exit 1
fi
local flake_path="$PROJECT_ROOT/flake.nix"
if [ ! -f "$flake_path" ]; then
echo "flake.nix not found at $flake_path; skipping Nix formatting."
return
fi
echo "Formatting Nix flake..."
nix fmt -- flake.nix
}
check_nix_format() (
if ! command -v nix &>/dev/null; then
echo "Error: nix command not found. Install Nix to check flake.nix formatting."
exit 1
fi
local flake_path="$PROJECT_ROOT/flake.nix"
if [ ! -f "$flake_path" ]; then
echo "flake.nix not found at $flake_path; skipping Nix format check."
exit 0
fi
local tmp_dir
tmp_dir="$(mktemp -d "${TMPDIR:-/tmp}/fmt-nix-check.XXXXXX")"
trap 'rm -rf "$tmp_dir"' EXIT
cp "$flake_path" "$tmp_dir/flake.nix"
(
cd "$tmp_dir"
nix fmt -- flake.nix
)
if ! cmp -s "$flake_path" "$tmp_dir/flake.nix"; then
echo "flake.nix is not formatted correctly. Run ./scripts/fmt.sh --nix or make fmt-nix."
diff -u "$flake_path" "$tmp_dir/flake.nix" || true
exit 1
fi
)
format_shell() {
if ! command -v shfmt &>/dev/null; then
echo "shfmt not found. Installing via brew..."
if command -v brew &>/dev/null; then
brew install shfmt
else
echo "Error: brew not found. Please install shfmt manually:"
echo " macOS: brew install shfmt"
echo " Linux: apt-get install shfmt or snap install shfmt"
echo " Go: go install mvdan.cc/sh/v3/cmd/shfmt@latest"
exit 1
fi
fi
echo "Formatting shell scripts..."
shfmt -i 2 -ci -bn -w scripts
}
if [ "$1" = "--check" ]; then
run_prettier --check
elif [ "$1" = "--shell" ]; then
format_shell
elif [ "$1" = "--nix" ]; then
format_nix
elif [ "$1" = "--nix-check" ]; then
check_nix_format
elif [ "$1" = "--all" ]; then
run_prettier
format_nix
format_shell
else
run_prettier
fi