-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathformat
More file actions
executable file
·54 lines (48 loc) · 2.24 KB
/
format
File metadata and controls
executable file
·54 lines (48 loc) · 2.24 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
#!/usr/bin/env bash
# scripts/format — auto-format shell scripts (and pre-commit if configured).
#
# Usage: scripts/format
# or: make format
#
# Priority:
# 1. pre-commit run --all-files (if pre-commit and config are present)
# 2. shfmt -w (write) on scripts/* and bin/*
# If none of the above tools are present, the script exits 0 with a notice.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$REPO_ROOT" || exit 1
# ── helpers ──────────────────────────────────────────────────────────────────
info() { printf '\033[1;34m==> %s\033[0m\n' "$*"; }
ok() { printf '\033[1;32m ✓ %s\033[0m\n' "$*"; }
warn() { printf '\033[1;33mwarn: %s\033[0m\n' "$*"; }
# ── collect shell scripts ─────────────────────────────────────────────────────
shell_scripts=()
for dir in scripts bin; do
if [ -d "$dir" ]; then
while IFS= read -r -d '' f; do
shell_scripts+=("$f")
done < <(find "$dir" -type f ! -name "*.*" -print0 2>/dev/null)
fi
done
# ── 1. pre-commit ─────────────────────────────────────────────────────────────
if command -v pre-commit >/dev/null 2>&1 && [ -f ".pre-commit-config.yaml" ]; then
info "Running pre-commit run --all-files…"
if pre-commit run --all-files; then
ok "pre-commit passed"
else
exit 1
fi
exit 0
fi
# ── 2. shfmt (write mode) ─────────────────────────────────────────────────────
if command -v shfmt >/dev/null 2>&1; then
if [ "${#shell_scripts[@]}" -gt 0 ]; then
info "Running shfmt -w on shell scripts…"
shfmt -w "${shell_scripts[@]}"
ok "shfmt: formatting applied"
else
warn "No shell scripts found to format with shfmt."
fi
else
warn "shfmt not found — skipping. See https://github.com/mvdan/sh for install instructions."
fi