Skip to content

Commit

Permalink
convert to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Sep 14, 2024
1 parent 472f0f0 commit 59779bf
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 79 deletions.
174 changes: 100 additions & 74 deletions activate.js
Original file line number Diff line number Diff line change
@@ -1,95 +1,121 @@
const fs = require('fs');
const path = require('path');
import fs from "fs";
import path from "path";
const fallbackLanguage = "en_us";

const baseDirectory = "bot";
const languages = require('./bot/languages.json');
import languages from "./bot/languages.json" with { type: "json" };

const languagesLocation = path.join(process.cwd(), "languages", baseDirectory);
const activeLanguagesLocation = path.join(process.cwd(), "languages", "active", baseDirectory);
const activeLanguagesLocation = path.join(
process.cwd(),
"languages",
"active",
baseDirectory
);

// relative file path is the path after the locale directory
function loadFile(locale, relativeFilePath) {

const filePath = path.join(languagesLocation, locale, relativeFilePath);

try {
let data;
if (!fs.existsSync(filePath))
data = "{}";
else
data = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(data);
const fallbackLanguageData = JSON.parse(fs.readFileSync(path.join(languagesLocation, fallbackLanguage, relativeFilePath), 'utf8'));
function checkFields(obj, path = '') {
Object.keys(obj).forEach(key => {
const value = obj[key];
const currentPath = path ? `${path}.${key}` : key;

if (typeof value === 'string') {
let pointer = fallbackLanguageData;
const structPath = currentPath.split(".");
for (let i = 0; i < structPath.length - 1; i++) {
pointer = pointer[structPath[i]];
}
pointer[structPath[structPath.length - 1]] = value;
} else if (typeof value === 'object' && value !== null) {
checkFields(value, currentPath);
}
});
const filePath = path.join(languagesLocation, locale, relativeFilePath);

try {
let data;
if (!fs.existsSync(filePath)) data = "{}";
else data = fs.readFileSync(filePath, "utf8");
const jsonData = JSON.parse(data);
const fallbackLanguageData = JSON.parse(
fs.readFileSync(
path.join(languagesLocation, fallbackLanguage, relativeFilePath),
"utf8"
)
);
function checkFields(obj, path = "") {
Object.keys(obj).forEach((key) => {
const value = obj[key];
const currentPath = path ? `${path}.${key}` : key;

if (typeof value === "string") {
let pointer = fallbackLanguageData;
const structPath = currentPath.split(".");
for (let i = 0; i < structPath.length - 1; i++) {
pointer = pointer[structPath[i]];
}
pointer[structPath[structPath.length - 1]] = value;
} else if (typeof value === "object" && value !== null) {
checkFields(value, currentPath);
}

checkFields(jsonData);

const absolutePathToActiveSubdirectory = path.join(activeLanguagesLocation, locale, relativeFilePath);

if (!fs.existsSync(path.dirname(absolutePathToActiveSubdirectory)))
fs.mkdirSync(path.dirname(absolutePathToActiveSubdirectory), { recursive: true });

fs.writeFileSync(absolutePathToActiveSubdirectory, JSON.stringify(fallbackLanguageData), { recursive: true });

} catch (err) {
console.error(`Error processing ${filePath}:`, err);
foundErrors = true;
});
}

checkFields(jsonData);

const absolutePathToActiveSubdirectory = path.join(
activeLanguagesLocation,
locale,
relativeFilePath
);

if (!fs.existsSync(path.dirname(absolutePathToActiveSubdirectory)))
fs.mkdirSync(path.dirname(absolutePathToActiveSubdirectory), {
recursive: true,
});

fs.writeFileSync(
absolutePathToActiveSubdirectory,
JSON.stringify(fallbackLanguageData),
{ recursive: true }
);
} catch (err) {
console.error(`Error processing ${filePath}:`, err);
foundErrors = true;
}
}

function loadSubdirectory(locale, relativePath) {
const absolutePath = path.join(languagesLocation, fallbackLanguage, relativePath);
const subdirectories = fs.readdirSync(absolutePath, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
const subfiles = fs.readdirSync(absolutePath, { withFileTypes: true })
.filter(dirent => !dirent.isDirectory())
.map(dirent => dirent.name);
for (let i = 0; i < subdirectories.length; i++) {
const subdirectoryPath = path.join(relativePath, subdirectories[i]);
loadSubdirectory(locale, subdirectoryPath);
}
for (let i = 0; i < subfiles.length; i++) {
const filePath = path.join(relativePath, subfiles[i]);
loadFile(locale, filePath);
}
const absolutePath = path.join(
languagesLocation,
fallbackLanguage,
relativePath
);
const subdirectories = fs
.readdirSync(absolutePath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
const subfiles = fs
.readdirSync(absolutePath, { withFileTypes: true })
.filter((dirent) => !dirent.isDirectory())
.map((dirent) => dirent.name);
for (let i = 0; i < subdirectories.length; i++) {
const subdirectoryPath = path.join(relativePath, subdirectories[i]);
loadSubdirectory(locale, subdirectoryPath);
}
for (let i = 0; i < subfiles.length; i++) {
const filePath = path.join(relativePath, subfiles[i]);
loadFile(locale, filePath);
}
}

function activate() {
Object.entries(languages).forEach(([key, value]) => {
const directory = path.join(process.cwd(), "languages", baseDirectory, value);
console.log(`Processing language code ${key}...`);
if (!fs.existsSync(directory)) {
console.log(`Skipping non-existent directory: ${directory} for language code ${key}`);
return;
}

if (!fs.existsSync(activeLanguagesLocation))
fs.mkdirSync(activeLanguagesLocation, { recursive: true });

console.log(`Copying ${directory}...`);
loadSubdirectory(value, "/");
Object.entries(languages).forEach(([key, value]) => {
const directory = path.join(
process.cwd(),
"languages",
baseDirectory,
value
);
console.log(`Processing language code ${key}...`);
if (!fs.existsSync(directory)) {
console.log(
`Skipping non-existent directory: ${directory} for language code ${key}`
);
return;
}

});
if (!fs.existsSync(activeLanguagesLocation))
fs.mkdirSync(activeLanguagesLocation, { recursive: true });

console.log(`Copying ${directory}...`);
loadSubdirectory(value, "/");
});
}

module.exports = activate;
export default activate;
6 changes: 1 addition & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
const activate = require("./activate");

module.exports = {
activate
};
export { default as activate } from "./activate.js";
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "languages",
"version": "1.0.0",
"description": "Languages for Quark",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/quark-bot-discord/languages.git"
},
"author": "Quark",
"license": "MIT",
"bugs": {
"url": "https://github.com/quark-bot-discord/languages/issues"
},
"homepage": "https://github.com/quark-bot-discord/languages#readme"
}

0 comments on commit 59779bf

Please sign in to comment.