Skip to content

Commit

Permalink
Merge pull request #32 from quark-bot-discord/development
Browse files Browse the repository at this point in the history
Translations + Convert to ESM
  • Loading branch information
thatkev authored Sep 15, 2024
2 parents dbcc830 + e9bca56 commit 34ab070
Show file tree
Hide file tree
Showing 24 changed files with 801 additions and 453 deletions.
132 changes: 75 additions & 57 deletions .github/scripts/compare-languages.js
Original file line number Diff line number Diff line change
@@ -1,74 +1,92 @@
const fs = require('fs');
const path = require('path');
import { readFileSync, readdirSync, statSync } from "fs";
import { join, relative } from "path";

function readJson(filePath) {
const rawData = fs.readFileSync(filePath, 'utf8');
return JSON.parse(rawData);
const rawData = readFileSync(filePath, "utf8");
return JSON.parse(rawData);
}

function getAllJsonFiles(directory) {
let jsonFiles = [];
const files = fs.readdirSync(directory);

files.forEach(file => {
const fullPath = path.join(directory, file);
if (fs.statSync(fullPath).isDirectory()) {
jsonFiles = jsonFiles.concat(getAllJsonFiles(fullPath));
} else if (file.endsWith('.json')) {
jsonFiles.push(fullPath);
}
});

return jsonFiles;
let jsonFiles = [];
const files = readdirSync(directory);

files.forEach((file) => {
const fullPath = join(directory, file);
if (statSync(fullPath).isDirectory()) {
jsonFiles = jsonFiles.concat(getAllJsonFiles(fullPath));
} else if (file.endsWith(".json")) {
jsonFiles.push(fullPath);
}
});

return jsonFiles;
}

function compareJsonFiles(enUsDir, otherLangDir) {
const enUsFiles = getAllJsonFiles(enUsDir);
const otherLangFiles = getAllJsonFiles(otherLangDir);

const enUsFilesRelative = enUsFiles.map(file => path.relative(enUsDir, file));
const otherLangFilesRelative = otherLangFiles.map(file => path.relative(otherLangDir, file));

const missingFiles = enUsFilesRelative.filter(file => !otherLangFilesRelative.includes(file));

const missingKeys = {};

enUsFilesRelative.forEach(file => {
if (otherLangFilesRelative.includes(file)) {
const enUsJson = readJson(path.join(enUsDir, file));
const otherLangJson = readJson(path.join(otherLangDir, file));

const missingKeysInFile = Object.keys(enUsJson).filter(key => !otherLangJson.hasOwnProperty(key));
if (missingKeysInFile.length > 0) {
missingKeys[file] = missingKeysInFile;
}
}
});

return { missingFiles, missingKeys };
const enUsFiles = getAllJsonFiles(enUsDir);
const otherLangFiles = getAllJsonFiles(otherLangDir);

const enUsFilesRelative = enUsFiles.map((file) => relative(enUsDir, file));
const otherLangFilesRelative = otherLangFiles.map((file) =>
relative(otherLangDir, file)
);

const missingFiles = enUsFilesRelative.filter(
(file) => !otherLangFilesRelative.includes(file)
);

const missingKeys = {};

enUsFilesRelative.forEach((file) => {
if (otherLangFilesRelative.includes(file)) {
const enUsJson = readJson(join(enUsDir, file));
const otherLangJson = readJson(join(otherLangDir, file));

const missingKeysInFile = Object.keys(enUsJson).filter(
(key) => !otherLangJson.hasOwnProperty(key)
);
if (missingKeysInFile.length > 0) {
missingKeys[file] = missingKeysInFile;
}
}
});

return { missingFiles, missingKeys };
}

function compareAllLanguages(baseDir) {
const enUsDir = path.join(baseDir, 'en_us');
const languages = fs.readdirSync(baseDir).filter(dir => fs.statSync(path.join(baseDir, dir)).isDirectory() && dir !== 'en_us');

const report = {};

languages.forEach(lang => {
const langDir = path.join(baseDir, lang);
const { missingFiles, missingKeys } = compareJsonFiles(enUsDir, langDir);
report[lang] = { missingFiles, missingKeys };
});

return report;
const enUsDir = join(baseDir, "en_us");
const languages = readdirSync(baseDir).filter(
(dir) => statSync(join(baseDir, dir)).isDirectory() && dir !== "en_us"
);

const report = {};

languages.forEach((lang) => {
const langDir = join(baseDir, lang);
const { missingFiles, missingKeys } = compareJsonFiles(enUsDir, langDir);
report[lang] = { missingFiles, missingKeys };
});

return report;
}

const baseDirectory = './bot/';
const baseDirectory = "./bot/";
const comparisonReport = compareAllLanguages(baseDirectory);

for (const [lang, report] of Object.entries(comparisonReport)) {
console.log(`Language: ${lang}`);
console.log(` Missing files: ${report.missingFiles.length > 0 ? report.missingFiles.join(', ') : 'None'}`);
console.log(` Missing keys: ${Object.keys(report.missingKeys).length > 0 ? JSON.stringify(report.missingKeys, null, 2) : 'None'}`);
console.log();
console.log(`Language: ${lang}`);
console.log(
` Missing files: ${
report.missingFiles.length > 0 ? report.missingFiles.join(", ") : "None"
}`
);
console.log(
` Missing keys: ${
Object.keys(report.missingKeys).length > 0
? JSON.stringify(report.missingKeys, null, 2)
: "None"
}`
);
console.log();
}
60 changes: 31 additions & 29 deletions .github/scripts/validate-json.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
const fs = require('fs');
const path = require('path');
import { readdirSync, statSync, readFileSync } from "fs";
import { join } from "path";

const baseDirectory = './bot';
const baseDirectory = "./bot";

let foundErrors = false;

function validateJSONFiles(directory) {
try {
const files = fs.readdirSync(directory);
try {
const files = readdirSync(directory);

files.forEach(file => {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
files.forEach((file) => {
const filePath = join(directory, file);
const stat = statSync(filePath);

if (stat.isDirectory()) {
validateJSONFiles(filePath);
} else if (file.endsWith('.json')) {
console.log(`Validating ${filePath}...`);
try {
const data = fs.readFileSync(filePath, 'utf8');
JSON.parse(data);
} catch (err) {
console.error(`Error processing ${filePath}: ${err.message}`);
foundErrors = true;
}
}
});
} catch (err) {
console.error(`Error reading directory ${directory}: ${err.message}`);
process.exit(1);
}
if (stat.isDirectory()) {
validateJSONFiles(filePath);
} else if (file.endsWith(".json")) {
console.log(`Validating ${filePath}...`);
try {
const data = readFileSync(filePath, "utf8");
JSON.parse(data);
} catch (err) {
console.error(`Error processing ${filePath}: ${err.message}`);
foundErrors = true;
}
}
});
} catch (err) {
console.error(`Error reading directory ${directory}: ${err.message}`);
process.exit(1);
}
}

validateJSONFiles(baseDirectory);

if (foundErrors) {
console.error("Validation errors found in some JSON files. Please check the logs above.");
process.exit(1);
console.error(
"Validation errors found in some JSON files. Please check the logs above."
);
process.exit(1);
} else {
console.log("All JSON files validated successfully.");
process.exit(0);
console.log("All JSON files validated successfully.");
process.exit(0);
}
Loading

0 comments on commit 34ab070

Please sign in to comment.