Skip to content

Commit

Permalink
refactor (BREAKING): Remove _ from name of all dataImport functions a…
Browse files Browse the repository at this point in the history
…s they should not be private
  • Loading branch information
3urobeat committed Jan 12, 2025
1 parent c448b11 commit 92ff0dd
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 652 deletions.
4 changes: 2 additions & 2 deletions src/commands/core/system.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2021-07-09 16:26:00
* Author: 3urobeat
*
* Last Modified: 2025-01-08 19:27:11
* Last Modified: 2025-01-12 17:25:28
* Modified By: 3urobeat
*
* Copyright (c) 2021 - 2025 3urobeat <https://github.com/3urobeat>
Expand Down Expand Up @@ -190,7 +190,7 @@ module.exports.reload = {
commandHandler.controller.pluginSystem.reloadPlugins();

// Reload data
await commandHandler.data._importFromDisk();
await commandHandler.data.importFromDisk();

// Send response message
respondModule(context, { prefix: "/me", ...resInfo }, await commandHandler.data.getLang("reloadcmdreloaded", null, resInfo.userID)); // Pass new resInfo object which contains prefix and everything the original resInfo obj contained
Expand Down
4 changes: 2 additions & 2 deletions src/controller/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2021-07-09 16:26:00
* Author: 3urobeat
*
* Last Modified: 2025-01-12 16:58:54
* Last Modified: 2025-01-12 17:25:28
* Modified By: 3urobeat
*
* Copyright (c) 2021 - 2025 3urobeat <https://github.com/3urobeat>
Expand Down Expand Up @@ -214,7 +214,7 @@ Controller.prototype._start = async function() {
this.data = new DataManager(this); // All functions provided by the DataManager, as well as all imported file data will be accessible here

await this.data._loadDataManagerFiles();
await this.data._importFromDisk();
await this.data.importFromDisk();


/* ------------ Print startup messages to log and set terminal title: ------------ */
Expand Down
4 changes: 2 additions & 2 deletions src/controller/manage.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2024-12-28 12:56:44
* Author: 3urobeat
*
* Last Modified: 2025-01-03 16:31:28
* Last Modified: 2025-01-12 17:26:11
* Modified By: 3urobeat
*
* Copyright (c) 2024 - 2025 3urobeat <https://github.com/3urobeat>
Expand Down Expand Up @@ -113,7 +113,7 @@ Controller.prototype.respreadProxies = async function() {
logger("info", "Reloading proxies from disk, respreading them and relogging affected accounts...", false, false, logger.animation("loading"));

// Reload proxies from disk real quick
this.data.proxies = await this.data._importProxiesFromDisk();
this.data.proxies = await this.data.importProxiesFromDisk();

// Update status of all proxies once
await this.data.checkAllProxies(15000);
Expand Down
554 changes: 7 additions & 547 deletions src/data/fileStructure.json

Large diffs are not rendered by default.

70 changes: 35 additions & 35 deletions src/dataManager/dataImport.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2021-07-09 16:26:00
* Author: 3urobeat
*
* Last Modified: 2025-01-09 21:41:21
* Last Modified: 2025-01-12 17:29:13
* Modified By: 3urobeat
*
* Copyright (c) 2021 - 2025 3urobeat <https://github.com/3urobeat>
Expand All @@ -22,10 +22,10 @@ const DataManager = require("./dataManager.js");


/**
* Internal: Loads cache.json from disk, updates cachefile property in DataManager and handles potential errors
* Loads cache.json from disk, updates cachefile property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importCacheFromDisk = function() {
DataManager.prototype.importCacheFromDisk = function() {
return new Promise((resolve) => {
try {
delete require.cache[require.resolve(srcdir + "/data/cache.json")]; // Delete cache to enable reloading data
Expand Down Expand Up @@ -65,10 +65,10 @@ DataManager.prototype._importCacheFromDisk = function() {


/**
* Internal: Loads data.json from disk, updates datafile property in DataManager and handles potential errors
* Loads data.json from disk, updates datafile property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importDataFromDisk = function() {
DataManager.prototype.importDataFromDisk = function() {
return new Promise((resolve) => {
try {
delete require.cache[require.resolve(srcdir + "/data/data.json")]; // Delete cache to enable reloading data
Expand Down Expand Up @@ -105,10 +105,10 @@ DataManager.prototype._importDataFromDisk = function() {


/**
* Internal: Loads config.json from disk, updates config property in DataManager and handles potential errors
* Loads config.json from disk, updates config property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importConfigFromDisk = function() {
DataManager.prototype.importConfigFromDisk = function() {
return new Promise((resolve) => {
try {
delete require.cache[require.resolve(srcdir + "/../config.json")]; // Delete cache to enable reloading data
Expand Down Expand Up @@ -163,10 +163,10 @@ DataManager.prototype._importConfigFromDisk = function() {


/**
* Internal: Loads advancedconfig.json from disk, updates advancedconfig property in DataManager and handles potential errors
* Loads advancedconfig.json from disk, updates advancedconfig property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importAdvancedConfigFromDisk = function() {
DataManager.prototype.importAdvancedConfigFromDisk = function() {
return new Promise((resolve) => {
try {
delete require.cache[require.resolve(srcdir + "/../advancedconfig.json")]; // Delete cache to enable reloading data
Expand Down Expand Up @@ -203,10 +203,10 @@ DataManager.prototype._importAdvancedConfigFromDisk = function() {


/**
* Internal: Loads accounts.txt/logininfo.json from disk, updates logininfo property in DataManager and handles potential errors
* Loads accounts.txt/logininfo.json from disk, updates logininfo property in DataManager and handles potential errors
* @returns {Promise.<object[]>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importLogininfoFromDisk = function() {
DataManager.prototype.importLogininfoFromDisk = function() {
return new Promise((resolve) => {
const logininfo = [];

Expand All @@ -231,7 +231,7 @@ DataManager.prototype._importLogininfoFromDisk = function() {
});
});

logger("debug", `DataManager _importLogininfoFromDisk(): Found ${logininfo.length} accounts in accounts.txt, not checking for logininfo.json...`);
logger("debug", `DataManager importLogininfoFromDisk(): Found ${logininfo.length} accounts in accounts.txt, not checking for logininfo.json...`);

this.controller._dataUpdateEvent("logininfo", this.logininfo, logininfo);
this.logininfo = logininfo;
Expand Down Expand Up @@ -280,10 +280,10 @@ DataManager.prototype._importLogininfoFromDisk = function() {


/**
* Internal: Loads proxies.txt from disk, updates proxies property in DataManager and handles potential errors
* Loads proxies.txt from disk, updates proxies property in DataManager and handles potential errors
* @returns {Promise.<object[]>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importProxiesFromDisk = function() {
DataManager.prototype.importProxiesFromDisk = function() {
return new Promise((resolve) => {
let proxies = []; // When the file is just created there can't be proxies in it (this bot doesn't support magic)

Expand Down Expand Up @@ -316,7 +316,7 @@ DataManager.prototype._importProxiesFromDisk = function() {
// Escape each delimiter if necessary and construct regex to split proxy below once with 1. delimiter, then once with 2. delimiter on the remaining string, and so on...
proxySplitRegex = new RegExp(proxySplitDelimiters.map((e) => `(${e.replace(/[/\-\\^$*+?.()|[\]{}]/g, "\\$&")})(.*)`).join(""), "g");
} else {
logger("debug", "DataManager _importProxiesFromDisk(): No proxyFormat provided in advancedconfig, skipping proxy format conversion...");
logger("debug", "DataManager importProxiesFromDisk(): No proxyFormat provided in advancedconfig, skipping proxy format conversion...");
}


Expand Down Expand Up @@ -354,7 +354,7 @@ DataManager.prototype._importProxiesFromDisk = function() {
lastOnlineCheck: 0
};

logger("debug", `DataManager _importProxiesFromDisk(): Converted proxy '${e}' using format '${this.advancedconfig.proxyFormat}' to '${proxies[i].proxy}'`);
logger("debug", `DataManager importProxiesFromDisk(): Converted proxy '${e}' using format '${this.advancedconfig.proxyFormat}' to '${proxies[i].proxy}'`);

} else { // Can be used as is

Expand All @@ -380,10 +380,10 @@ DataManager.prototype._importProxiesFromDisk = function() {


/**
* Internal: Loads quotes.txt from disk, updates quotes property in DataManager and handles potential errors
* Loads quotes.txt from disk, updates quotes property in DataManager and handles potential errors
* @returns {Promise.<string[]>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importQuotesFromDisk = function() {
DataManager.prototype.importQuotesFromDisk = function() {
return new Promise((resolve) => {
let quotes = [];

Expand Down Expand Up @@ -421,10 +421,10 @@ DataManager.prototype._importQuotesFromDisk = function() {


/**
* Internal: Loads languages from disk, updates languages property in DataManager and handles potential errors
* Loads languages from disk, updates languages property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importLanguagesFromDisk = function() {
DataManager.prototype.importLanguagesFromDisk = function() {
return new Promise((resolve) => {
try {
const obj = {};
Expand All @@ -438,7 +438,7 @@ DataManager.prototype._importLanguagesFromDisk = function() {

// Iterate through all files in lang dir and load them
fs.readdir("./src/data/lang", (err, files) => {
logger("debug", `DataManager _importLanguagesFromDisk(): Found these languages in the lang folder: '${files.join(", ")}'`);
logger("debug", `DataManager importLanguagesFromDisk(): Found these languages in the lang folder: '${files.join(", ")}'`);

files.forEach((e) => {
let thisFile;
Expand Down Expand Up @@ -486,10 +486,10 @@ DataManager.prototype._importLanguagesFromDisk = function() {


/**
* Internal: Loads customlang.json from disk, updates languages property in DataManager and handles potential errors
* Loads customlang.json from disk, updates languages property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importCustomLangFromDisk = function() {
DataManager.prototype.importCustomLangFromDisk = function() {
return new Promise((resolve) => {
// Check before trying to import if the user even created the file
if (fs.existsSync(srcdir + "/../customlang.json")) {
Expand Down Expand Up @@ -545,34 +545,34 @@ DataManager.prototype._importCustomLangFromDisk = function() {
}
});
} else {
logger("debug", "DataManager _importCustomLangFromDisk(): No customlang.json file found");
logger("debug", "DataManager importCustomLangFromDisk(): No customlang.json file found");
resolve(this.lang); // Resolve with default lang object
}
});
};


/**
* Internal: Loads all config & data files from disk and handles potential errors
* Loads all config & data files from disk and handles potential errors
* @returns {Promise.<void>} Resolves promise when all files have been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importFromDisk = async function () {
DataManager.prototype.importFromDisk = async function () {

// Call all functions from above after another. This must be done async to avoid a check failing that depends on something from a previous function. We sadly cannot use Promise.all() because of this.
logger("info", "Importing data files and settings...", false, true, logger.animation("loading"));

await this._importCacheFromDisk();
await this._importDataFromDisk();
await this._importConfigFromDisk();
await this._importAdvancedConfigFromDisk();
await this.importCacheFromDisk();
await this.importDataFromDisk();
await this.importConfigFromDisk();
await this.importAdvancedConfigFromDisk();

this.controller._loggerOptionsUpdateAfterConfigLoad(this.advancedconfig); // Call optionsUpdateAfterConfigLoad() to set previously inaccessible options

await this._importLogininfoFromDisk();
await this._importProxiesFromDisk();
await this._importQuotesFromDisk();
await this._importLanguagesFromDisk();
await this._importCustomLangFromDisk();
await this.importLogininfoFromDisk();
await this.importProxiesFromDisk();
await this.importQuotesFromDisk();
await this.importLanguagesFromDisk();
await this.importCustomLangFromDisk();

this.lastCommentDB = new nedb({ filename: srcdir + "/data/lastcomment.db", autoload: true }); // Autoload
this.ratingHistoryDB = new nedb({ filename: srcdir + "/data/ratingHistory.db", autoload: true });
Expand Down
42 changes: 21 additions & 21 deletions src/dataManager/dataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Created Date: 2023-03-21 22:34:51
* Author: 3urobeat
*
* Last Modified: 2025-01-12 17:03:39
* Last Modified: 2025-01-12 17:28:30
* Modified By: 3urobeat
*
* Copyright (c) 2023 - 2025 3urobeat <https://github.com/3urobeat>
Expand Down Expand Up @@ -196,64 +196,64 @@ DataManager.prototype.writeProxiesToDisk = function() {};
DataManager.prototype.writeQuotesToDisk = function() {};

/**
* Internal: Loads cache.json from disk, updates cachefile property in DataManager and handles potential errors
* Loads cache.json from disk, updates cachefile property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importCacheFromDisk = function() {};
DataManager.prototype.importCacheFromDisk = function() {};

/**
* Internal: Loads data.json from disk, updates datafile property in DataManager and handles potential errors
* Loads data.json from disk, updates datafile property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importDataFromDisk = function() {};
DataManager.prototype.importDataFromDisk = function() {};

/**
* Internal: Loads config.json from disk, updates config property in DataManager and handles potential errors
* Loads config.json from disk, updates config property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importConfigFromDisk = function() {};
DataManager.prototype.importConfigFromDisk = function() {};

/**
* Internal: Loads advancedconfig.json from disk, updates advancedconfig property in DataManager and handles potential errors
* Loads advancedconfig.json from disk, updates advancedconfig property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importAdvancedConfigFromDisk = function() {};
DataManager.prototype.importAdvancedConfigFromDisk = function() {};

/**
* Internal: Loads accounts.txt/logininfo.json from disk, updates logininfo property in DataManager and handles potential errors
* Loads accounts.txt/logininfo.json from disk, updates logininfo property in DataManager and handles potential errors
* @returns {Promise.<object[]>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importLogininfoFromDisk = function() {};
DataManager.prototype.importLogininfoFromDisk = function() {};

/**
* Internal: Loads proxies.txt from disk, updates proxies property in DataManager and handles potential errors
* Loads proxies.txt from disk, updates proxies property in DataManager and handles potential errors
* @returns {Promise.<object[]>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importProxiesFromDisk = function() {};
DataManager.prototype.importProxiesFromDisk = function() {};

/**
* Internal: Loads quotes.txt from disk, updates quotes property in DataManager and handles potential errors
* Loads quotes.txt from disk, updates quotes property in DataManager and handles potential errors
* @returns {Promise.<string[]>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importQuotesFromDisk = function() {};
DataManager.prototype.importQuotesFromDisk = function() {};

/**
* Internal: Loads languages from disk, updates languages property in DataManager and handles potential errors
* Loads languages from disk, updates languages property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importLanguagesFromDisk = function() {};
DataManager.prototype.importLanguagesFromDisk = function() {};

/**
* Internal: Loads customlang.json from disk, updates languages property in DataManager and handles potential errors
* Loads customlang.json from disk, updates languages property in DataManager and handles potential errors
* @returns {Promise.<object>} Resolves promise with file content when file has been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importCustomLangFromDisk = function() {};
DataManager.prototype.importCustomLangFromDisk = function() {};

/**
* Internal: Loads all config & data files from disk and handles potential errors
* Loads all config & data files from disk and handles potential errors
* @returns {Promise.<void>} Resolves promise when all files have been loaded successfully. The function will log an error and terminate the application should a fatal error occur.
*/
DataManager.prototype._importFromDisk = async function () {};
DataManager.prototype.importFromDisk = async function () {};

/**
* Verifies the data integrity of every source code file in the project by comparing its checksum.
Expand Down
Loading

0 comments on commit 92ff0dd

Please sign in to comment.