Skip to content

Commit c8f47f0

Browse files
committed
feat: sort top-level nodes of YAML files
1 parent 71963f8 commit c8f47f0

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

.pre-commit-hooks.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@
8787
entry: hooks/yaml/yaml-extension.sh
8888
language: script
8989
files: \.yml$
90+
- id: yaml-sort
91+
name: "yaml-sort: Style formatting"
92+
description: "Ensure top-level nodes are sorted in YAML files"
93+
entry: hooks/yaml/yaml-sort.sh
94+
language: script
95+
files: \.yaml$
9096
######################
9197
# Yalc related hook
9298
- id: yalc-check

hooks/yaml/yaml-sort.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
yaml_file="$1"
6+
7+
main() {
8+
if [[ ! -f "$yaml_file" ]]; then
9+
echo "Error: $yaml_file not found"
10+
exit 1
11+
fi
12+
if ! command -v yq &> /dev/null; then
13+
echo "Error: yq is required but not installed"
14+
echo "Install with: brew install yq"
15+
exit 1
16+
fi
17+
18+
temp_file=$(mktemp)
19+
20+
yq eval 'sort_keys(.)' "$yaml_file" > "$temp_file"
21+
22+
if [[ $? -eq 0 ]]; then
23+
mv "$temp_file" "$yaml_file"
24+
echo "Sorted top-level keys in $yaml_file."
25+
git add "$yaml_file"
26+
exit 0
27+
else
28+
echo "Error: Failed to sort YAML file"
29+
rm -f "$temp_file"
30+
exit 1
31+
fi
32+
}
33+
34+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
35+
main "$@"
36+
fi

0 commit comments

Comments
 (0)