aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/serverLib/ImportsManager.js
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/serverLib/ImportsManager.js')
-rw-r--r--server/src/serverLib/ImportsManager.js87
1 files changed, 46 insertions, 41 deletions
diff --git a/server/src/serverLib/ImportsManager.js b/server/src/serverLib/ImportsManager.js
index 8db7b68..ac1fc4c 100644
--- a/server/src/serverLib/ImportsManager.js
+++ b/server/src/serverLib/ImportsManager.js
@@ -1,69 +1,77 @@
+import {
+ resolve,
+ basename as _basename,
+ relative,
+} from 'path';
+import RecursiveRead from 'readdir-recursive';
+
/**
* Import managment base, used to load commands/protocol and configuration objects
- *
- * Version: v2.0.0
- * Developer: Marzavec ( https://github.com/marzavec )
- * License: WTFPL ( http://www.wtfpl.net/txt/copying/ )
- *
+ * @property {String} base - Base path that all imports are required in from
+ * @author Marzavec ( https://github.com/marzavec )
+ * @version v2.0.0
+ * @license WTFPL ( http://www.wtfpl.net/txt/copying/ )
*/
-
-const read = require('readdir-recursive');
-const path = require('path');
-
class ImportsManager {
/**
- * Create a `ImportsManager` instance for (re)loading classes and config
- *
+ * Create an `ImportsManager` instance for (re)loading classes and config
* @param {String} basePath executing directory name; default __dirname
*/
- constructor (basePath) {
+ constructor(basePath) {
+ /**
+ * Stored reference to the base directory path
+ * @type {String}
+ */
this.basePath = basePath;
+
+ /**
+ * Data holder for imported modules
+ * @type {Object}
+ */
this.imports = {};
}
/**
* Pull base path that all imports are required in from
- *
+ * @public
* @type {String} readonly
*/
- get base () {
+ get base() {
return this.basePath;
}
/**
* Gather all js files from target directory, then verify and load
- *
- * @param {String} dirName The name of the dir to load, relative to the basePath.
- *
+ * @param {String} dirName The name of the dir to load, relative to the basePath
+ * @private
* @return {String} Load errors or empty if none
*/
- loadDir (dirName) {
- const dir = path.resolve(this.basePath, dirName);
+ loadDir(dirName) {
+ const dir = resolve(this.basePath, dirName);
let errorText = '';
try {
- read.fileSync(dir).forEach(file => {
- const basename = path.basename(file);
+ RecursiveRead.fileSync(dir).forEach((file) => {
+ const basename = _basename(file);
if (basename.startsWith('_') || !basename.endsWith('.js')) return;
let imported;
try {
imported = require(file);
+
+ if (!this.imports[dirName]) {
+ this.imports[dirName] = {};
+ }
+
+ this.imports[dirName][file] = imported;
} catch (e) {
- let err = `Unable to load modules from ${dirName} (${path.relative(dir, file)})\n${e}`;
+ const err = `Unable to load modules from ${dirName} (${relative(dir, file)})\n${e}`;
errorText += err;
console.error(err);
- return errorText;
- }
-
- if (!this.imports[dirName]) {
- this.imports[dirName] = {};
}
-
- this.imports[dirName][file] = imported;
});
} catch (e) {
- let err = `Unable to load modules from ${dirName}\n${e}`;
+ const err = `Unable to load modules from ${dirName}\n${e}`;
errorText += err;
console.error(err);
return errorText;
@@ -75,15 +83,13 @@ class ImportsManager {
/**
* Unlink references to each loaded module, pray to google that gc knows it's job,
* then reinitialize this class to start the reload
- *
- * @param {Array} dirName The name of the dir to load, relative to the _base path.
- *
+ * @public
* @return {String} Load errors or empty if none
*/
- reloadDirCache () {
+ reloadDirCache() {
let errorText = '';
- Object.keys(this.imports).forEach(dir => {
+ Object.keys(this.imports).forEach((dir) => {
Object.keys(this.imports[dir]).forEach((mod) => {
delete require.cache[require.resolve(mod)];
});
@@ -97,20 +103,19 @@ class ImportsManager {
/**
* Pull reference to imported modules that were imported from dirName, or
* load required directory if not found
- *
* @param {String} dirName The name of the dir to load, relative to the _base path.
- *
+ * @public
* @return {Object} Object containing command module paths and structs
*/
- getImport (dirName) {
- let imported = this.imports[dirName];
+ getImport(dirName) {
+ const imported = this.imports[dirName];
if (!imported) {
this.loadDir(dirName);
}
- return Object.assign({}, this.imports[dirName]);
+ return { ...this.imports[dirName] };
}
}
-module.exports = ImportsManager;
+export default ImportsManager;