Skip to content
This repository was archived by the owner on Feb 9, 2020. It is now read-only.

Commit

Permalink
Merge pull request #4 from HittmanA/master
Browse files Browse the repository at this point in the history
Add some idiot-proofing.
  • Loading branch information
hcortezr authored Dec 9, 2017
2 parents 771ad90 + 3f438e1 commit bca0858
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/pocketnode/plugin/Plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ class Plugin {
this.arguments = [];
}

constructor(name, description, permission, aliases, server){
constructor(name, description, permission, aliases, server, version, api, author){
this.initVars();
this.name = name;
this.description = description;
this.permission = permission;
this.aliases = aliases || [];
this.server = server;
this.version = version;
this.api = api;
this.author = author;
}

getName(){
Expand Down
45 changes: 36 additions & 9 deletions src/pocketnode/plugin/PluginLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,49 @@ class PluginLoader {
let pluginList = [];
let plugins = [];
let increment = 0;
FileSystem.readdirSync(directory).forEach(file => {
pluginList[increment] = file;
logger.info("Loaded " + file);
increment++;
FileSystem.readdirSync(directory).forEach(dirContent => {
if(FileSystem.lstatSync(directory + dirContent).isDirectory()){
if(FileSystem.readdirSync(directory + dirContent + "/")[0] != null) {
pluginList[increment] = dirContent;
logger.info("Loaded " + dirContent);
increment++;
}
}
})
for(var i = 0; i < pluginList.length; i++) {
var location = directory + pluginList[i] + "/";
plugins[i] = require(location + pluginList[i]);
var manifest = JSON.parse(FileSystem.readFileSync(location + "manifest.json").toString());
plugins[i] = new plugins[i](manifest.name, manifest.description, manifest.permission, manifest.aliases, this.server);
this.server.getCommandMap().registerCommand(plugins[i]);
if(plugins[i].onEnable != null) {
plugins[i].onEnable();
if(FileSystem.existsSync(location + "manifest.json")) {
var manifest = JSON.parse(FileSystem.readFileSync(location + "manifest.json").toString());
manifest = this.checkManifest(manifest);
plugins[i] = new plugins[i](manifest.name, manifest.description, manifest.permission, manifest.aliases, this.server, manifest.version, manifest.api, manifest.author);
this.server.getCommandMap().registerCommand(plugins[i]);
if(plugins[i].onEnable != null) {
plugins[i].onEnable();
}
} else {
throw new Error("No manifest found!");
}
}
}

checkManifest(manifest){
if(manifest.name != null && manifest.name !== "") {
if(manifest.description == null) {
manifest.description = "No description";
}
if(manifest.permission == null) {
manifest.permission = "";
}
if(manifest.aliases == null) {
manifest.aliases = [];
}
return manifest;
} else {
throw new Error("No valid manifest name.");
}
}

}

module.exports = PluginLoader;

0 comments on commit bca0858

Please sign in to comment.