From 1f407de617db398073ac0f161a2fc270064e75a7 Mon Sep 17 00:00:00 2001 From: 3xp0rt <61662492+3xp0rt@users.noreply.github.com> Date: Mon, 2 Jun 2025 23:38:27 +0300 Subject: [PATCH] Refactor: Rewrite in Python with headers sorting and scheme formatting --- .github/workflows/sort-format-json.yml | 83 ++++---------------------- scripts/sort_format_json.py | 71 ++++++++++++++++++++++ 2 files changed, 81 insertions(+), 73 deletions(-) create mode 100644 scripts/sort_format_json.py diff --git a/.github/workflows/sort-format-json.yml b/.github/workflows/sort-format-json.yml index cfb13e2..1d616fd 100644 --- a/.github/workflows/sort-format-json.yml +++ b/.github/workflows/sort-format-json.yml @@ -1,4 +1,4 @@ -name: Sort and Format wmn-data.json +name: Sort and Format JSON Files on: push: @@ -7,88 +7,25 @@ on: - 'wmn-data-schema.json' jobs: - sort-json: + sort-and-format-json: + name: Sort and Format JSON Files runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - name: Set up Node.js - uses: actions/setup-node@v3 + - name: Set up Python + uses: actions/setup-python@v5 with: - node-version: '20' + python-version: '3.11' - - name: Create sorting script - run: | - cat << 'EOF' > sort-wmn-data.js - const fs = require('fs'); - - const dataPath = 'wmn-data.json'; - const schemaPath = 'wmn-data-schema.json'; - - const original = fs.readFileSync(dataPath, 'utf8'); - const data = JSON.parse(original); - const schema = JSON.parse(fs.readFileSync(schemaPath, 'utf8')); - - // ---------- Sorting Helpers ---------- - function sortArrayAlphabetically(arr) { - return arr.sort((a, b) => a.localeCompare(b)); - } - - function reorderObjectKeys(obj, keyOrder) { - const reordered = {}; - keyOrder.forEach(key => { - if (key in obj) reordered[key] = obj[key]; - }); - Object.keys(obj).forEach(key => { - if (!keyOrder.includes(key)) reordered[key] = obj[key]; - }); - return reordered; - } - - // ---------- Extract Schema Key Order ---------- - const siteSchema = schema.properties?.sites?.items; - const keyOrder = siteSchema?.properties ? Object.keys(siteSchema.properties) : []; - - // ---------- Sort authors and categories ---------- - if (Array.isArray(data.authors)) { - data.authors = sortArrayAlphabetically(data.authors); - } - - if (Array.isArray(data.categories)) { - data.categories = sortArrayAlphabetically(data.categories); - } - - // ---------- Sort and Reorder Sites ---------- - if (Array.isArray(data.sites)) { - data.sites.sort((a, b) => a.name.localeCompare(b.name)); - data.sites = data.sites.map(site => reorderObjectKeys(site, keyOrder)); - } - - // Convert to JSON with 2-space indentation - const updated = JSON.stringify(data, null, 2); - - // Only write if content has changed - if (original !== updated) { - fs.writeFileSync(dataPath, updated); - console.log('wmn-data.json updated and reordered.'); - } else { - console.log('wmn-data.json is already sorted and formatted.'); - } - EOF - - - name: Install Prettier - run: npm install --global prettier - - - name: Run sorting script and format - run: | - node sort-wmn-data.js - prettier --write wmn-data.json + - name: Run Python sorting script + run: python scripts/sort_wmn_data.py - name: Commit if changed run: | git config --global user.name "github-actions" git config --global user.email "github-actions@github.com" - git add wmn-data.json - git diff --cached --quiet || git commit -m "chore: auto-sort wmn-data.json by schema" + git add wmn-data.json wmn-data-schema.json + git diff --cached --quiet || git commit -m "chore: auto-sort and format JSON files" git push diff --git a/scripts/sort_format_json.py b/scripts/sort_format_json.py new file mode 100644 index 0000000..ead1dcb --- /dev/null +++ b/scripts/sort_format_json.py @@ -0,0 +1,71 @@ +import json + +data_path = 'wmn-data.json' +schema_path = 'wmn-data-schema.json' + +def sort_array_alphabetically(arr): + return sorted(arr, key=str.lower) + +def reorder_object_keys(obj, key_order): + reordered = {k: obj[k] for k in key_order if k in obj} + for k in obj: + if k not in key_order: + reordered[k] = obj[k] + return reordered + +def sort_headers(site): + headers = site.get("headers") + if isinstance(headers, dict): + site["headers"] = dict(sorted(headers.items(), key=lambda item: item[0].lower())) + +def load_and_format_json(path): + with open(path, 'r', encoding='utf-8') as f: + raw_content = f.read() + data = json.loads(raw_content) + formatted = json.dumps(data, indent=2, ensure_ascii=False) + return data, raw_content, formatted + +data, data_raw, data_formatted = load_and_format_json(data_path) +schema, schema_raw, schema_formatted = load_and_format_json(schema_path) + +changed = False + +# Sort authors and categories +if isinstance(data.get('authors'), list): + data['authors'] = sort_array_alphabetically(data['authors']) + +if isinstance(data.get('categories'), list): + data['categories'] = sort_array_alphabetically(data['categories']) + +# Sort and reorder sites +site_schema = schema.get('properties', {}).get('sites', {}).get('items', {}) +key_order = list(site_schema.get('properties', {}).keys()) + +if isinstance(data.get('sites'), list): + data['sites'].sort(key=lambda site: site.get('name', '').lower()) + for site in data['sites']: + sort_headers(site) + data['sites'] = [reorder_object_keys(site, key_order) for site in data['sites']] + +updated_data_formatted = json.dumps(data, indent=2, ensure_ascii=False) + +# Write wmn-data.json if changed +if data_raw.strip() != updated_data_formatted.strip(): + with open(data_path, 'w', encoding='utf-8') as f: + f.write(updated_data_formatted) + print("Updated and sorted wmn-data.json.") + changed = True +else: + print("wmn-data.json already formatted.") + +# Write formatted wmn-data-schema.json if changed +if schema_raw.strip() != schema_formatted.strip(): + with open(schema_path, 'w', encoding='utf-8') as f: + f.write(schema_formatted) + print("Formatted wmn-data-schema.json.") + changed = True +else: + print("wmn-data-schema.json already formatted.") + +if not changed: + print("No changes made.")