-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathauto-format.ps1
More file actions
50 lines (44 loc) · 1.44 KB
/
auto-format.ps1
File metadata and controls
50 lines (44 loc) · 1.44 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
# PostToolUse hook - Auto-format files after editing
# Place in: .claude\hooks\auto-format.ps1
# Register in: .claude\settings.json (with -ExecutionPolicy Bypass)
$inputJson = [Console]::In.ReadToEnd() | ConvertFrom-Json
$tool = $inputJson.tool_name
# Only run after Write or Edit operations
if ($tool -eq "Write" -or $tool -eq "Edit") {
$file = $inputJson.tool_input.file_path
if (-not $file) {
$file = $inputJson.tool_input.path
}
if (-not $file -or $file -eq "null") {
exit 0
}
# Get file extension
$ext = [System.IO.Path]::GetExtension($file).TrimStart('.')
switch ($ext) {
{ $_ -in @("js", "jsx", "ts", "tsx", "json", "css", "scss", "md", "html", "vue") } {
# Format with Prettier if available
if (Test-Path "node_modules\.bin\prettier.cmd") {
& npx prettier --write $file 2>$null
}
}
"py" {
# Format with Black if available
if (Get-Command black -ErrorAction SilentlyContinue) {
& black $file 2>$null
}
}
"go" {
# Format with gofmt
if (Get-Command gofmt -ErrorAction SilentlyContinue) {
& gofmt -w $file 2>$null
}
}
"rs" {
# Format with rustfmt
if (Get-Command rustfmt -ErrorAction SilentlyContinue) {
& rustfmt $file 2>$null
}
}
}
}
exit 0