mirror of
https://github.com/WebBreacher/WhatsMyName.git
synced 2025-06-18 14:25:31 -04:00
Refactor: Rewrite in Python with headers sorting and scheme formatting
This commit is contained in:
parent
84a119aeb0
commit
1f407de617
83
.github/workflows/sort-format-json.yml
vendored
83
.github/workflows/sort-format-json.yml
vendored
@ -1,4 +1,4 @@
|
|||||||
name: Sort and Format wmn-data.json
|
name: Sort and Format JSON Files
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
@ -7,88 +7,25 @@ on:
|
|||||||
- 'wmn-data-schema.json'
|
- 'wmn-data-schema.json'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
sort-json:
|
sort-and-format-json:
|
||||||
|
name: Sort and Format JSON Files
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Set up Node.js
|
- name: Set up Python
|
||||||
uses: actions/setup-node@v3
|
uses: actions/setup-python@v5
|
||||||
with:
|
with:
|
||||||
node-version: '20'
|
python-version: '3.11'
|
||||||
|
|
||||||
- name: Create sorting script
|
- name: Run Python sorting script
|
||||||
run: |
|
run: python scripts/sort_wmn_data.py
|
||||||
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: Commit if changed
|
- name: Commit if changed
|
||||||
run: |
|
run: |
|
||||||
git config --global user.name "github-actions"
|
git config --global user.name "github-actions"
|
||||||
git config --global user.email "github-actions@github.com"
|
git config --global user.email "github-actions@github.com"
|
||||||
git add wmn-data.json
|
git add wmn-data.json wmn-data-schema.json
|
||||||
git diff --cached --quiet || git commit -m "chore: auto-sort wmn-data.json by schema"
|
git diff --cached --quiet || git commit -m "chore: auto-sort and format JSON files"
|
||||||
git push
|
git push
|
||||||
|
71
scripts/sort_format_json.py
Normal file
71
scripts/sort_format_json.py
Normal file
@ -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.")
|
Loading…
Reference in New Issue
Block a user