Skip to content

Commit

Permalink
validation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
thatkev committed Apr 20, 2024
1 parent 0756857 commit e9ab08c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 50 deletions.
105 changes: 57 additions & 48 deletions .github/scripts/validate-slash_commands.js
Original file line number Diff line number Diff line change
@@ -1,69 +1,78 @@
const fs = require('fs');
const path = require('path');

const directory = process.argv[2]; // Take directory as an input argument
const baseDirectory = './langs'; // Adjust if your base directory differs
const conversionFile = './languages-conversion.json';

let foundErrors = false;

try {
const files = fs.readdirSync(directory);
const conversionData = fs.readFileSync(conversionFile, 'utf8');
const languageMap = JSON.parse(conversionData);

if (files.length === 0) {
console.log("No JSON files found in the directory.");
process.exit(0);
}

console.log(`Found ${files.length} files in the directory.`);
Object.entries(languageMap).forEach(([key, value]) => {
const directory = path.join(baseDirectory, value, 'bot', 'slash_commands');
if (!fs.existsSync(directory)) {
console.log(`Skipping non-existent directory: ${directory} for language code ${key}`);
return;
}

files.forEach(file => {
if (!file.endsWith('.json')) {
console.log(`Skipping non-JSON file: ${file}`);
const files = fs.readdirSync(directory);
if (files.length === 0) {
console.log(`No JSON files found in ${directory}.`);
return;
}

const filePath = path.join(directory, file);
try {
const data = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(data);
files.forEach(file => {
if (!file.endsWith('.json')) {
console.log(`Skipping non-JSON file: ${file}`);
return;
}

console.log(`Processing ${file}...`);
console.log(`Validating ${value}/${file}...`)
const filePath = path.join(directory, file);
try {
const data = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(data);

function checkFields(obj, path = '') {
Object.keys(obj).forEach(key => {
const value = obj[key];
const currentPath = path ? `${path}.${key}` : key;

if (typeof value === 'string') {
if (path.endsWith('.description') && value.length > 100) {
console.error(`${file}: Description exceeds 100 characters at '${currentPath}'`);
foundErrors = true;
}
if (path.endsWith('.name') && value.includes(' ')) {
console.error(`${file}: Name contains space at '${currentPath}'`);
foundErrors = true;
function checkFields(obj, path = '') {
Object.keys(obj).forEach(key => {
const value = obj[key];
const currentPath = path ? `${path}.${key}` : key;

if (typeof value === 'string') {
if (currentPath.endsWith('.description') && value.length > 100) {
console.error(`Validation error: ${directory}/${file}: Description exceeds 100 characters at '${currentPath}'`);
foundErrors = true;
}
if (currentPath.endsWith('.name') && value.includes(' ')) {
console.error(`Validation error: ${directory}/${file}: Name contains space at '${currentPath}'`);
foundErrors = true;
}
} else if (typeof value === 'object' && value !== null) {
checkFields(value, currentPath);
}
} else if (typeof value === 'object' && value !== null) {
checkFields(value, currentPath);
}
});
}
});
}

checkFields(jsonData);
checkFields(jsonData);

} catch (err) {
console.error(`Error processing ${file}:`, err);
foundErrors = true;
}
} catch (err) {
console.error(`Error processing ${file}:`, err);
foundErrors = true;
}
});
});
} catch (err) {
console.error("Error reading the directory:", err);
process.exit(1);
}

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

} catch (err) {
console.error("Error reading the conversion file or directory:", err);
process.exit(1);
} else {
console.log("All files validated successfully.");
process.exit(0);
}
2 changes: 1 addition & 1 deletion langs/en_gb/bot/slash_commands/ban.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

"commandOptions": {
"userOption": {
"name": "user",
"name": "user abc",
"description": "The user to ban"
},

Expand Down
5 changes: 5 additions & 0 deletions languages-conversion.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"en-US": "en_us",
"en-GB": "en_gb",
"tr": "tr"
}
1 change: 0 additions & 1 deletion languages.json

This file was deleted.

0 comments on commit e9ab08c

Please sign in to comment.