-
Notifications
You must be signed in to change notification settings - Fork 353
/
plugins.js
98 lines (90 loc) · 3.01 KB
/
plugins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
var fs = require('fs'),
path = require('path');
function getDirectories(srcpath) {
return fs.readdirSync(srcpath).filter(function(file) {
return fs.statSync(path.join(srcpath, file)).isDirectory();
});
}
var plugin_folders;
var plugin_directory;
var exec_dir;
try { //try loading plugins from a non standalone install first
plugin_directory = "./plugins/";
plugin_folders = getDirectories(plugin_directory);
} catch(e){//load paths for an Electrify install
exec_dir = path.dirname(process.execPath) + "/resources/default_app/"; //need this to change node prefix for npm installs
plugin_directory = path.dirname(process.execPath) + "/resources/default_app/plugins/";
plugin_folders = getDirectories(plugin_directory);
}
exports.init = function(hooks){
preload_plugins(hooks);
};
function createNpmDependenciesArray (packageFilePath) {
var p = require(packageFilePath);
if (!p.dependencies) return [];
var deps = [];
for (var mod in p.dependencies) {
deps.push(mod + "@" + p.dependencies[mod]);
}
return deps;
}
function preload_plugins(hooks){
var deps = [];
var npm = require("npm");
for (var i = 0; i < plugin_folders.length; i++) {
try{
require(plugin_directory + plugin_folders[i]);
} catch(e) {
deps = deps.concat(createNpmDependenciesArray(plugin_directory + plugin_folders[i] + "/package.json"));
}
}
if(deps.length > 0) {
npm.load({
loaded: false
}, function (err) {
// catch errors
if (plugin_directory != "./plugins/"){ //install plugin modules for Electrify builds
npm.prefix = exec_dir;
console.log(npm.prefix);
}
npm.commands.install(deps, function (er, data) {
if(er){
console.log(er);
}
console.log("Plugin preload complete");
load_plugins(hooks)
});
if (err) {
console.log("preload_plugins: " + err);
}
});
} else {
load_plugins(hooks)
}
}
function load_plugins(hooks){
var dbot = require("./discord_bot.js");
var commandCount = 0;
for (var i = 0; i < plugin_folders.length; i++) {
var plugin;
try{
plugin = require(plugin_directory + plugin_folders[i])
} catch (err){
console.log("Improper setup of the '" + plugin_folders[i] +"' plugin. : " + err);
}
if (plugin){
if(plugin.init){
plugin.init(hooks)
}
if("commands" in plugin){
for (var j = 0; j < plugin.commands.length; j++) {
if (plugin.commands[j] in plugin){
dbot.addCommand(plugin.commands[j], plugin[plugin.commands[j]])
commandCount++;
}
}
}
}
}
console.log("Loaded " + dbot.commandCount() + " chat commands")
}