diff --git a/scripts/APIv3.js b/scripts/APIv3.js index 05a46ade03f..a3c9e0c620b 100644 --- a/scripts/APIv3.js +++ b/scripts/APIv3.js @@ -1,18 +1,18 @@ #!/usr/bin/env node -const fs = require("fs"); -const path = require("path"); +const fs = require('fs'); +const path = require('path'); let all = {}; let tfa = {}; let regions = {}; // Read all JSON files from the 'entries' directory -fs.readdirSync("entries").forEach((dir) => { - fs.readdirSync(path.join("entries", dir)).forEach((file) => { - if (file.endsWith(".json")) { +fs.readdirSync('entries').forEach((dir) => { + fs.readdirSync(path.join('entries', dir)).forEach((file) => { + if (file.endsWith('.json')) { let data = JSON.parse( - fs.readFileSync(path.join("entries", dir, file), "utf8"), + fs.readFileSync(path.join('entries', dir, file), 'utf8'), ); let key = Object.keys(data)[0]; all[key] = data[key]; @@ -21,72 +21,70 @@ fs.readdirSync("entries").forEach((dir) => { }); // Process all entries -Object.keys(all) - .sort() - .forEach((entryName) => { - let entry = all[entryName]; - - // Process tfa methods - if ("tfa" in entry) { - entry["tfa"].forEach((method) => { - if (!tfa[method]) tfa[method] = {}; - - tfa[method][entryName] = entry; - }); - } - - // Process regions - if ("regions" in entry) { - entry["regions"].forEach((region) => { - if (region[0] !== "-") { - if (!regions[region]) regions[region] = { count: 0 }; - - regions[region]["count"] += 1; - } - }); - } - - // Rename 'categories' to 'keywords' - if ("categories" in entry) { - entry["keywords"] = entry["categories"]; - delete entry["categories"]; - } - }); +Object.keys(all).sort().forEach((entryName) => { + let entry = all[entryName]; + + // Process tfa methods + if ('tfa' in entry) { + entry['tfa'].forEach((method) => { + if (!tfa[method]) tfa[method] = {}; + + tfa[method][entryName] = entry; + }); + } + + // Process regions + if ('regions' in entry) { + entry['regions'].forEach((region) => { + if (region[0] !== '-') { + if (!regions[region]) regions[region] = {count: 0}; + + regions[region]['count'] += 1; + } + }); + } + + // Rename 'categories' to 'keywords' + if ('categories' in entry) { + entry['keywords'] = entry['categories']; + delete entry['categories']; + } +}); // Write the all.json and tfa files -const outputDir = "api/v3"; -if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, { recursive: true }); +const outputDir = 'api/v3'; +if (!fs.existsSync(outputDir)) fs.mkdirSync(outputDir, {recursive: true}); const writeJsonFile = (filename, data) => fs.writeFileSync( path.join(outputDir, filename), - JSON.stringify(data, null, 2), + JSON.stringify(data), ); -writeJsonFile("all.json", { all }); +writeJsonFile('all.json', all); Object.keys(tfa).forEach((method) => writeJsonFile(`${method}.json`, tfa[method]), ); // Add the 'int' region -regions["int"] = { count: Object.keys(all).length, selection: true }; +regions['int'] = {count: Object.keys(all).length, selection: true}; // Write regions.json -const sortedRegions = Object.entries(regions) - .sort(([, a], [, b]) => b.count - a.count) - .reduce((acc, [k, v]) => { +const sortedRegions = Object.entries(regions). + sort(([, a], [, b]) => b.count - a.count). + reduce((acc, [k, v]) => { acc[k] = v; return acc; }, {}); -writeJsonFile("regions.json", sortedRegions); +writeJsonFile('regions.json', sortedRegions); // Write tfa.json -const tfaEntries = Object.entries(all) - .filter(([, entry]) => entry.tfa) - .sort(([a], [b]) => a.toLowerCase().localeCompare(b.toLowerCase())) - .reduce((acc, [k, v]) => { - acc[k] = v; +const tfaEntries = Object.keys(all). + filter((key) => all[key].tfa). + sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase())). + reduce((acc, key) => { + acc[key] = all[key]; return acc; }, {}); -writeJsonFile("tfa.json", tfaEntries); +writeJsonFile('tfa.json', tfaEntries);