WhatsMyName icon indicating copy to clipboard operation
WhatsMyName copied to clipboard

Auto-sort and format wmn-data.json

Open 3xp0rt opened this issue 9 months ago • 6 comments

This improvement is designed to simplify the process of extending wmn-data.json for contributors by eliminating the need to manually sort entries. In addition to streamlining contributions, it ensures a consistent dataset format and automatically sorts previously unordered entries.

3xp0rt avatar May 24 '25 10:05 3xp0rt

This is a really cool idea and will save a bunch of work.

I'm wondering if you can provide more details about how it works and what it does please.

WebBreacher avatar May 25 '25 13:05 WebBreacher

This GitHub Actions script is designed to automate the process of sorting and formatting a JSON file named wmn-data.json whenever it or its related schema file wmn-data-schema.json is modified in a Git push. It begins by listening for any push events that affect either of the two mentioned files. When such an event occurs, it triggers a job that runs on the latest version of Ubuntu. The job first checks out the repository’s code, then sets up a Node.js environment using version 20. After that, it creates a JavaScript file on the fly (sort-wmn-data.js). This script reads in both the JSON data and its schema, and uses helper functions to sort arrays alphabetically and reorder object keys based on a preferred order defined in the schema. Specifically, it ensures that the authors and categories arrays are alphabetically sorted. For the sites array, it not only sorts the objects alphabetically by the name field but also rearranges each site's properties so that required keys (as specified by the schema) appear first in a defined order, followed by any extra keys. Once the sorting logic is complete, it writes the updated JSON back to the original file and prints a confirmation message. Next, the workflow globally installs Prettier, a code formatter, and uses it to format the JSON file to ensure consistent style. Finally, it checks if the file has changed as a result of the sorting and formatting. If so, it commits those changes to the repository with a predefined commit message and pushes the update. This ensures the wmn-data.json file remains consistently ordered and clean every time it's edited.

3xp0rt avatar May 25 '25 14:05 3xp0rt

Thanks for the details. I'd like to ask for a few modifications before merging into the project:

  1. Check to see if there was a change in the file before saving/commiting:
const updated = JSON.stringify(data, null, 2);
if (original !== updated) {
  fs.writeFileSync(dataPath, updated);
}

  1. Custom ordering of keys. To make it so that the most important pieces are at the top of each entry, I suggest we sort in a specific order. You could create a list like the one below and use it to sort the elements in the entry. Why? Having uri_check next to uri_pretty (if it exists) is visually helpful when reviewing content.

"preferredOrder": ["name", "uri_check", "uri_pretty", "e_code", "e_string", "m_code", "m_string", cat"]

Please let me know your thoughts. And thanks for the submission.

WebBreacher avatar Jun 02 '25 12:06 WebBreacher

I’ve implemented your first suggestion. As for the second point, key ordering is handled by the schema, so there’s no need for a preferredOrder field. This will work as expected because uri_pretty was added to the schema in this commit.

3xp0rt avatar Jun 02 '25 13:06 3xp0rt

@WebBreacher, I’ve resolved the key ordering issue, everything’s working as expected now.

3xp0rt avatar Jun 02 '25 19:06 3xp0rt

To streamline future support, the workflow was refactored to use Python code. Headers sorting and schema formatting were also added.

3xp0rt avatar Jun 02 '25 20:06 3xp0rt

OK. Let's do this. I've reviewed the code and changes to the schema as much as I can. Let's give this a try. I'm looking forward to the improvements!

WebBreacher avatar Jun 09 '25 17:06 WebBreacher