Skip to content

Instantly share code, notes, and snippets.

@boarnoah
Last active August 31, 2023 18:49
Show Gist options
  • Save boarnoah/97431eff16efe9593d6e40236c5a5a65 to your computer and use it in GitHub Desktop.
Save boarnoah/97431eff16efe9593d6e40236c5a5a65 to your computer and use it in GitHub Desktop.
Pre Commit hook for Prettier, dotnet format & terraform format. Relatively fast, only runs lints relevant to commit= content.
# Powershell scripts can be used in .git/hooks very easily ex:
# #!/usr/bin/env sh
# pwsh -noprofile -F "C:\Code\pre-commit.ps1"
$files = (git diff --cached --name-only --diff-filter=ACM);
if ( $files -like '*.tf' ) {
$tfFmtResults = terraform fmt -recursive
if ($LASTEXITCODE -ne 0){
Write-Warning "Terraform FMT failed"
Write-Warning $fmtResults
exit 1
}
}
if ( $files -like '*.cs' ) {
$solution = Get-ChildItem -path *.sln
if ($null -eq $solution) {
Write-Warning "*.cs files in repo but no solution file found"
exit 1
}
dotnet format $solution --no-restore --verify-no-changes
if ($LASTEXITCODE -ne 0) {
Write-Warning "CS FMT failed"
Write-Warning $dotnetFmtResults
exit 1
}
}
if ( $files -match '.(js|ts|vue|tsx)' ) {
$prettierResult = npx prettier@3 --list-different '**/*.{js,ts,vue,tsx}' '!*.d.ts'
if ($LASTEXITCODE -ne 0) {
Write-Host "JS FMT failed"
Write-Host $prettierResult
exit 1
}
}
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment