Skip to content

Commit cc13f13

Browse files
committed
Update Python scripts to use Prettier for JSON formatting instead of manual formatting
1 parent 4ae676f commit cc13f13

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

scripts/normalize_book_sets.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import os
1515
import argparse
1616
import re
17+
import subprocess
1718
from pathlib import Path
1819

1920

@@ -273,7 +274,7 @@ def normalize_book_sets(
273274
original_count = len(all_obj.get("problems", []))
274275
original_problems = set(all_obj.get("problems", []))
275276
problems_with_both_set = set(problems_with_both)
276-
277+
277278
# Remove premium problems from original_problems for comparison
278279
original_non_premium = original_problems - premium_set
279280
removed_premium = sorted(original_problems & premium_set)
@@ -382,8 +383,21 @@ def normalize_book_sets(
382383
print("[DRY RUN] Would write changes to file (use --write to apply)")
383384
else:
384385
try:
386+
# Write JSON file (prettier will format it)
385387
with open(book_sets_file, "w", encoding="utf-8") as f:
386-
json.dump(data, f, indent=2, ensure_ascii=False)
388+
json.dump(data, f, ensure_ascii=False)
389+
390+
# Format with prettier
391+
try:
392+
subprocess.run(
393+
["npx", "prettier", "--write", book_sets_file],
394+
check=True,
395+
capture_output=True,
396+
)
397+
except subprocess.CalledProcessError as e:
398+
print(f"Warning: Prettier formatting failed: {e}")
399+
except FileNotFoundError:
400+
print("Warning: npx/prettier not found, skipping formatting")
387401

388402
print("\n" + "=" * 70)
389403
print(f"✓ Successfully updated '{book_sets_file}'")

scripts/normalize_json.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import json
1313
import argparse
14+
import subprocess
1415
from collections import OrderedDict
1516

1617

@@ -106,6 +107,16 @@ def sort_json_by_numeric_keys(input_file, output_file):
106107
with open(output_file, "w", encoding="utf-8") as f:
107108
f.write("\n".join(lines) + "\n")
108109

110+
# Format with prettier
111+
try:
112+
subprocess.run(
113+
["npx", "prettier", "--write", output_file],
114+
check=True,
115+
capture_output=True,
116+
)
117+
except (subprocess.CalledProcessError, FileNotFoundError):
118+
pass # Silently fail if prettier is not available
119+
109120
print(
110121
f"Successfully sorted '{input_file}' and saved the result to '{output_file}'."
111122
)

scripts/sort_book_sets_by_difficulty.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import os
1818
import sys
1919
import argparse
20+
import subprocess
2021
from pathlib import Path
2122

2223
# Paths
@@ -185,7 +186,20 @@ def main():
185186
if updated_count > 0:
186187
# Save updated book-sets.json
187188
with open(BOOK_SETS_PATH, "w", encoding="utf-8") as f:
188-
json.dump(book_sets, f, indent=2)
189+
json.dump(book_sets, f, ensure_ascii=False)
190+
191+
# Format with prettier
192+
try:
193+
subprocess.run(
194+
["npx", "prettier", "--write", str(BOOK_SETS_PATH)],
195+
check=True,
196+
capture_output=True,
197+
)
198+
except subprocess.CalledProcessError as e:
199+
print(f"Warning: Prettier formatting failed: {e}")
200+
except FileNotFoundError:
201+
print("Warning: npx/prettier not found, skipping formatting")
202+
189203
print(f"✓ Updated {updated_count} book set(s) in {BOOK_SETS_PATH}")
190204
else:
191205
print("No changes needed. All sets are already sorted.")

scripts/update_problems_from_csv.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,19 @@ def save_json(data):
157157
sorted_data = OrderedDict(sorted_items)
158158

159159
with open(JSON_PATH, "w", encoding="utf-8") as f:
160-
json.dump(sorted_data, f, indent=2)
160+
json.dump(sorted_data, f, ensure_ascii=False)
161+
162+
# Format with prettier
163+
import subprocess
164+
try:
165+
subprocess.run(
166+
["npx", "prettier", "--write", str(JSON_PATH)],
167+
check=True,
168+
capture_output=True,
169+
)
170+
except (subprocess.CalledProcessError, FileNotFoundError):
171+
pass # Silently fail if prettier is not available
172+
161173
return True
162174
except Exception as e:
163175
print(f"Error saving JSON: {e}")

0 commit comments

Comments
 (0)