Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pre commit hooks #198

Merged
merged 5 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
hooks:
- id: end-of-file-fixer
- id: check-json
- id: check-yaml
- repo: local
hooks:
- id: format-data
name: Format JSON data files
entry: bash run_on_data_changed.sh
language: system
types: [json]
verbose: true
30 changes: 30 additions & 0 deletions format_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import json
from argparse import ArgumentParser
from pathlib import Path


def main(file: Path):
with open(file) as f:
data = json.load(f)

# sort cards by 'title' value
data['cards'] = sorted(data['cards'], key=lambda x: x['title'].lower())

# sort tags inside each card by value
for card in data['cards']:
card['tags'] = sorted(card['tags'], key=lambda x: x.lower())

# sort keys in the cards by reverse key order
data['cards'] = [{k: v for k, v in sorted(card.items(), reverse=True)} for card in data['cards']]

# save data back to the file
with open(file, 'w') as f:
json.dump(data, f, indent=2, sort_keys=False, ensure_ascii=False)


if __name__ == '__main__':
parser = ArgumentParser(description="Sort data.")
parser.add_argument('-f', '--file', type=Path, required=True,
help='Path to the file containing the data')
args = parser.parse_args()
main(args.file)
12 changes: 12 additions & 0 deletions run_on_data_changed.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash
# find all staged json files inside assets/data/ and its subdirectories
files=$(git diff --name-only --cached | grep -E 'assets/data/.*\.json$')

if [ -n "$files" ]; then
# loop through each file and run sort_data.py
for file in $files; do
echo "Formatting $file"
python3 format_data.py -f $file
git add $file
done
fi