This repository has been archived on 2025-04-19. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
git-hooks/pre-commit.d/prettier-yml.py

39 lines
876 B
Python
Executable file

#!/usr/bin/env python
import os
import subprocess
import sys
from shutil import which
file_extensions = (".yml", ".yaml",)
prettier = os.getenv("PRETTIER_BIN", "./node_modules/.bin/prettier")
options = ["--write"]
if not which(prettier):
sys.exit("Prettier binary not found")
diff = subprocess.check_output(
["git", "diff", "--cached", "--name-only", "--diff-filter", "ACM"],
universal_newlines=True
)
yaml_files = [
item for item in diff.splitlines() if item.endswith(file_extensions)
]
if not yaml_files:
sys.exit(0)
try:
output = subprocess.check_output(
[prettier, *options, *yaml_files],
universal_newlines=True,
stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as e:
print("Formatting was unsuccessful:")
sys.exit(e.stdout)
subprocess.run(["git", "add", "-u", *yaml_files])
sys.exit(0)