Load plugins by directory name not full path

This commit is contained in:
Jiiks 2018-01-16 06:38:28 +02:00
parent a1261cab0a
commit 39fbae166f
2 changed files with 24 additions and 6 deletions

View File

@ -27260,15 +27260,24 @@ class PluginManager extends Module {
this.setState({ this.setState({
plugins: [] plugins: []
}); });
tests(); // tests();
} }
get plugins() { get plugins() {
return this.state.plugins; return this.state.plugins;
} }
async pluginsPath() {
//TODO Get this from config module
const config = await BDIpc.send('getConfig');
return config.paths.find(path => 'plugins' in path).plugins;
}
async loadPlugin(pluginPath) { async loadPlugin(pluginPath) {
const { plugins } = this.state; const { plugins } = this.state;
const pluginsPath = await this.pluginsPath();
pluginPath = path.join(pluginsPath, pluginPath);
try { try {

View File

@ -47,10 +47,18 @@ class PluginManager extends Module {
return this.state.plugins; return this.state.plugins;
} }
async pluginsPath() {
//TODO Get this from config module
const config = await BDIpc.send('getConfig');
return config.paths.find(path => 'plugins' in path).plugins;
}
async loadPlugin(pluginPath) { async loadPlugin(pluginPath) {
const { plugins } = this.state; const { plugins } = this.state;
try { try {
const pluginsPath = await this.pluginsPath();
pluginPath = path.join(pluginsPath, pluginPath);
const loaded = plugins.find(plugin => plugin.pluginPath === pluginPath); const loaded = plugins.find(plugin => plugin.pluginPath === pluginPath);
if (loaded) { if (loaded) {
@ -66,10 +74,12 @@ class PluginManager extends Module {
plugins.push(Object.assign({ plugins.push(Object.assign({
pluginPath, pluginPath,
instance instance
},readConfig)); }, readConfig));
this.setState(plugins); this.setState(plugins);
//TODO Read plugin user config and call onStart if enabled
return instance; return instance;
} catch (err) { } catch (err) {
throw err; throw err;
@ -108,13 +118,12 @@ const _instance = new PluginManager();
async function tests() { async function tests() {
const pluginName = 'Example'; const pluginName = 'Example';
const config = await BDIpc.send('getConfig');
const pluginPath = config.paths.find(path => 'plugins' in path).plugins;
try { try {
//Load test plugin //Load test plugin
const plugin = await _instance.loadPlugin(path.join(pluginPath, pluginName)); const plugin = await _instance.loadPlugin(pluginName);
//Attempt to load the same plugin again //Attempt to load the same plugin again
const plugin2 = await _instance.loadPlugin(path.join(pluginPath, pluginName)); const plugin2 = await _instance.loadPlugin(pluginName);
} catch (err) { } catch (err) {
console.log(`Failed to load plugin! ${err.message}`); console.log(`Failed to load plugin! ${err.message}`);
} }