Skip to content

Commit

Permalink
fix: make readme generation more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
maehr committed Dec 19, 2023
1 parent 7ccca62 commit 0cf2597
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions utils/sortJSON.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,35 @@ fs.readFile(filepath, 'utf-8', (err, data) => {
try {
const jsonData = JSON.parse(data);

// sort the JSON array by the "title" field
// Ensure all entries have the required structure
jsonData.forEach((entry) => {
const defaultStructure = {
title: '',
description: '',
url: '',
region: [],
language: [],
type: [],
period: []
};

Object.keys(defaultStructure).forEach((key) => {
if (!entry.hasOwnProperty(key)) {
entry[key] = defaultStructure[key];
}
});
});

// Sort the JSON array by the "title" field
jsonData.sort((a, b) => a.title.localeCompare(b.title));

// write the sorted JSON data back to the file
// Write the sorted and amended JSON data back to the file
fs.writeFile(filepath, JSON.stringify(jsonData, null, 2), (err) => {
if (err) {
console.error(`Error writing file: ${err.message}`);
return;
}
console.log(`File "${filepath}" sorted successfully.`);
console.log(`File "${filepath}" sorted and structured successfully.`);
});
} catch (error) {
console.error(`Error parsing JSON data: ${error.message}`);
Expand Down

0 comments on commit 0cf2597

Please sign in to comment.