From 006b7bed7a7bb2ae8058947be1606186108d5f33 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Tue, 16 Jan 2018 09:05:33 +0200 Subject: [PATCH 1/6] Add watch script --- client/package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/package.json b/client/package.json index 40fd33fb..0b287ad5 100644 --- a/client/package.json +++ b/client/package.json @@ -25,6 +25,7 @@ "jquery": "^3.2.1" }, "scripts": { - "build": "webpack --progress --colors" + "build": "webpack --progress --colors", + "watch": "webpack --progress --colors --watch" } } From 7c126174481b09b05eec8a8981a37766b91d7010 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 17 Jan 2018 09:41:31 +0200 Subject: [PATCH 2/6] Correct Object.assign --- client/src/modules/pluginmanager.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/modules/pluginmanager.js b/client/src/modules/pluginmanager.js index bbddc650..8b826ebc 100644 --- a/client/src/modules/pluginmanager.js +++ b/client/src/modules/pluginmanager.js @@ -81,7 +81,7 @@ class PluginManager extends Module { let userConfig = readConfig.defaultConfig; try { const readUserConfig = await FileUtils.readJsonFromFile(userConfigPath); - userConfig = Object.assign(userConfig, readUserConfig); + userConfig = Object.assign({}, userConfig, readUserConfig); } catch (err) {/*We don't care if this fails it either means that user config doesn't exist or there's something wrong with it so we revert to default config*/} const configs = { From 3ee59845def7ccdcc0b6d703e8f58954647d38a1 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 17 Jan 2018 09:42:40 +0200 Subject: [PATCH 3/6] Plugin name as param for tests --- client/src/modules/pluginmanager.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/client/src/modules/pluginmanager.js b/client/src/modules/pluginmanager.js index 8b826ebc..d00163d8 100644 --- a/client/src/modules/pluginmanager.js +++ b/client/src/modules/pluginmanager.js @@ -133,9 +133,7 @@ class PluginManager extends Module { const _instance = new PluginManager(); -async function pluginManager() { - - const pluginName = 'Example'; +async function pluginManager(pluginName) { try { //Load test plugin From e3367768eb7c1c7fb9e6d502e251f2aa02f4fc3d Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 17 Jan 2018 10:06:40 +0200 Subject: [PATCH 4/6] Plugin reloading --- client/src/modules/pluginmanager.js | 30 ++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/client/src/modules/pluginmanager.js b/client/src/modules/pluginmanager.js index d00163d8..2b42c50e 100644 --- a/client/src/modules/pluginmanager.js +++ b/client/src/modules/pluginmanager.js @@ -29,6 +29,7 @@ class Plugin { get authors() { return this.info.authors } get version() { return this.info.version } get pluginPath() { return this.paths.pluginPath } + get dirName() { return this.paths.dirName } get enabled() { return this.userConfig.enabled } start() { @@ -62,6 +63,7 @@ class PluginManager extends Module { async loadPlugin(pluginPath) { const { plugins } = this.state; + const dirName = pluginPath; try { const pluginsPath = this.pluginsPath(); @@ -74,8 +76,6 @@ class PluginManager extends Module { const readConfig = await this.readConfig(pluginPath); const mainPath = path.join(pluginPath, readConfig.main); - - //TODO Read plugin user config and call onStart if enabled const userConfigPath = path.join(pluginPath, 'user.config.json'); let userConfig = readConfig.defaultConfig; @@ -90,7 +90,7 @@ class PluginManager extends Module { }; const plugin = window.require(mainPath)(Plugin, {}, {}); - const instance = new plugin({configs, info: readConfig.info, main: readConfig.main, paths: { pluginPath }}); + const instance = new plugin({configs, info: readConfig.info, main: readConfig.main, paths: { pluginPath, dirName }}); if (instance.enabled) instance.start(); @@ -104,9 +104,19 @@ class PluginManager extends Module { } } - async reloadPlugin(pluginPath) { - //TODO Cleanup loaded plugin - return await this.loadPlugin(pluginPath); + async reloadPlugin(plugin) { + let _plugin = this.getPluginByName(plugin); + if (!_plugin) _plugin = this.plugins.find(plugin => plugin.pluginPath === plugin); + if (!_plugin) throw { 'message': 'Attempted to reload a plugin that is not loaded?' }; + window.rp = _plugin; + if (!_plugin.stop()) throw { 'message': 'Plugin failed to stop!' }; + const index = this.plugins.findIndex(plugin => plugin === _plugin); + const { pluginPath, dirName } = _plugin; + delete window.require.cache[window.require.resolve(pluginPath)]; + + this.plugins.splice(index, 1); + + return this.loadPlugin(dirName); } getPluginByName(name) { return this.plugins.find(plugin => plugin.name === name); } @@ -144,7 +154,13 @@ async function pluginManager(pluginName) { return true; } catch (err) { console.log(`Failed to load plugin! ${err.message}`); - throw err; + } + + try { + //Reload test plugin + const reloadedPlugin = await _instance.reloadPlugin('Example Plugin'); + } catch (err) { + console.log(`Failed to reload plugin! ${err.message}`); } } From 289533b75594216adc39b9f84188a6b9d16111b5 Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 17 Jan 2018 10:10:29 +0200 Subject: [PATCH 5/6] Add dirName check and remove debug --- client/src/modules/pluginmanager.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/client/src/modules/pluginmanager.js b/client/src/modules/pluginmanager.js index 2b42c50e..c9d00be1 100644 --- a/client/src/modules/pluginmanager.js +++ b/client/src/modules/pluginmanager.js @@ -106,9 +106,8 @@ class PluginManager extends Module { async reloadPlugin(plugin) { let _plugin = this.getPluginByName(plugin); - if (!_plugin) _plugin = this.plugins.find(plugin => plugin.pluginPath === plugin); + if (!_plugin) _plugin = this.plugins.find(plugin => plugin.pluginPath === plugin || plugin.dirName === plugin); if (!_plugin) throw { 'message': 'Attempted to reload a plugin that is not loaded?' }; - window.rp = _plugin; if (!_plugin.stop()) throw { 'message': 'Plugin failed to stop!' }; const index = this.plugins.findIndex(plugin => plugin === _plugin); const { pluginPath, dirName } = _plugin; From 509f44bcb14a5668dc547e72f5ae6834f070df5c Mon Sep 17 00:00:00 2001 From: Jiiks Date: Wed, 17 Jan 2018 10:17:01 +0200 Subject: [PATCH 6/6] add some plugin getters --- client/src/modules/pluginmanager.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/client/src/modules/pluginmanager.js b/client/src/modules/pluginmanager.js index c9d00be1..432aeee3 100644 --- a/client/src/modules/pluginmanager.js +++ b/client/src/modules/pluginmanager.js @@ -105,11 +105,10 @@ class PluginManager extends Module { } async reloadPlugin(plugin) { - let _plugin = this.getPluginByName(plugin); - if (!_plugin) _plugin = this.plugins.find(plugin => plugin.pluginPath === plugin || plugin.dirName === plugin); + const _plugin = this.findPlugin(plugin); if (!_plugin) throw { 'message': 'Attempted to reload a plugin that is not loaded?' }; if (!_plugin.stop()) throw { 'message': 'Plugin failed to stop!' }; - const index = this.plugins.findIndex(plugin => plugin === _plugin); + const index = this.getPluginIndex(_plugin); const { pluginPath, dirName } = _plugin; delete window.require.cache[window.require.resolve(pluginPath)]; @@ -118,8 +117,22 @@ class PluginManager extends Module { return this.loadPlugin(dirName); } - getPluginByName(name) { return this.plugins.find(plugin => plugin.name === name); } - getPluginById(id) { return this.plugins.find(plugin => plugin.id === id); } + //TODO make this nicer + findPlugin(wild) { + let plugin = this.getPluginByName(wild); + if (plugin) return plugin; + plugin = this.getPluginById(wild); + if (plugin) return plugin; + plugin = this.getPluginByPath(wild); + if (plugin) return plugin; + return this.getPluginByDirName(wild); + } + + getPluginIndex(plugin) { return this.plugins.findIndex(p => p === plugin) } + getPluginByName(name) { return this.plugins.find(p => p.name === name) } + getPluginById(id) { return this.plugins.find(p => p.id === id) } + getPluginByPath(path) { return this.plugins.find(p => p.pluginPath === path) } + getPluginByDirName(dirName) { return this.plugins.find(p => p.dirName === dirName) } stopPlugin(name) { const plugin = this.getPluginByName(name);