Skip to content

Commit

Permalink
Resolve #8130
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlgo11 committed Jul 17, 2024
1 parent e59bf92 commit ef5e008
Showing 1 changed file with 50 additions and 52 deletions.
102 changes: 50 additions & 52 deletions scripts/APIv3.js
Original file line number Diff line number Diff line change
@@ -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];
Expand All @@ -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);

0 comments on commit ef5e008

Please sign in to comment.