Skip to content
This repository has been archived by the owner on Aug 28, 2020. It is now read-only.

Commit

Permalink
Require errors (#25)
Browse files Browse the repository at this point in the history
* Update Store.js

* temperary, going to remove this in a second

* fix Store.load to not need overriding by CommandStore.load

* remove commandStore.load in favor of new Store.load

* fix jsdocs

* make sure the issue is not a constructor

* lint

* docs fix
  • Loading branch information
bdistin committed Sep 20, 2017
1 parent 5ddb55a commit 386e441
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
26 changes: 7 additions & 19 deletions src/lib/structures/CommandStore.js
Expand Up @@ -109,18 +109,6 @@ class CommandStore extends Collection {
this.aliases.clear();
}

/**
* Loads a command file into Klasa so it can saved in this store.
* @param {string} dir The user directory or core directory where this file is saved.
* @param {string[]} file An array containing information about it's category structure.
* @returns {Command}
*/
load(dir, file) {
const cmd = this.set(new (require(join(dir, ...file)))(this.client, dir, file));
delete require.cache[join(dir, ...file)];
return cmd;
}

/**
* Loads all of our commands from both the user and core directories.
* @returns {number} The number of commands and aliases loaded.
Expand All @@ -139,12 +127,12 @@ class CommandStore extends Collection {
/* eslint-enable no-empty-function */

/**
* Walks our directory of commands for the user and core directories.
* @param {CommandStore} store The command store we're loading into.
* @param {string} dir The directory of commands we're using to load commands from.
* @param {string[]} subs Subfolders for recursion.
* @returns {void}
*/
* Walks our directory of commands for the user and core directories.
* @param {CommandStore} store The command store we're loading into.
* @param {string} dir The directory of commands we're using to load commands from.
* @param {string[]} subs Subfolders for recursion.
* @returns {void}
*/
static async walk(store, dir, subs = []) {
const files = await fs.readdir(join(dir, ...subs)).catch(() => { fs.ensureDir(dir).catch(err => store.client.emit('error', err)); });
if (!files) return true;
Expand All @@ -156,6 +144,6 @@ class CommandStore extends Collection {

}

Store.applyToClass(CommandStore, ['load', 'loadAll']);
Store.applyToClass(CommandStore, ['loadAll']);

module.exports = CommandStore;
15 changes: 11 additions & 4 deletions src/lib/structures/interfaces/Store.js
Expand Up @@ -25,12 +25,19 @@ class Store {
/**
* Loads a piece into Klasa so it can be saved in this store.
* @param {string} dir The user directory or core directory where this file is saved.
* @param {string} file A string showing where the file is located.
* @returns {Piece}
* @param {string|string[]} file A string or array of strings showing where the file is located.
* @returns {?Piece}
*/
load(dir, file) {
const piece = this.set(new (require(join(dir, file)))(this.client, dir, file));
delete require.cache[join(dir, file)];
const loc = Array.isArray(file) ? join(dir, ...file) : join(dir, file);
let piece = null;
try {
piece = this.set(new (require(loc))(this.client, dir, file));
} catch (err) {
const error = err.message.endsWith('not a constructor') ? new TypeError(`Non-Class Export: ${loc}`) : err;
this.client.emit('wtf', error);
}
delete require.cache[loc];
return piece;
}

Expand Down

0 comments on commit 386e441

Please sign in to comment.