mirror of
https://github.com/WebBreacher/WhatsMyName.git
synced 2025-06-18 14:25:31 -04:00
Create sort-format-json.yml
This commit is contained in:
parent
4ec90c4d12
commit
09931fe30a
85
.github/workflows/sort-format-json.yml
vendored
Normal file
85
.github/workflows/sort-format-json.yml
vendored
Normal file
@ -0,0 +1,85 @@
|
||||
name: Sort and Format wmn-data.json
|
||||
|
||||
on:
|
||||
push:
|
||||
paths:
|
||||
- 'wmn-data.json'
|
||||
- 'wmn-data-schema.json'
|
||||
|
||||
jobs:
|
||||
sort-json:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Set up Node.js
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: '20'
|
||||
|
||||
- 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 data = JSON.parse(fs.readFileSync(dataPath, 'utf8'));
|
||||
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 = Array.isArray(siteSchema?.required) ? siteSchema.required : [];
|
||||
|
||||
// ---------- 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));
|
||||
}
|
||||
|
||||
fs.writeFileSync(dataPath, JSON.stringify(data, null, 2));
|
||||
console.log('wmn-data.json sorted and reordered according to schema');
|
||||
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
|
||||
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 push
|
Loading…
Reference in New Issue
Block a user