Merge pull request #125 from JsSucks/external-modules

External modules
This commit is contained in:
Alexei Stukov 2018-02-13 18:33:15 +02:00 committed by GitHub
commit 0d660ac523
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 79 additions and 8 deletions

View File

@ -153,7 +153,7 @@ export default class {
mainPath
}
const content = await this.loadContent(paths, configs, readConfig.info, readConfig.main);
const content = await this.loadContent(paths, configs, readConfig.info, readConfig.main, readConfig.type);
if (reload) this.localContent[index] = content;
else this.localContent.push(content);
return content;

View File

@ -20,6 +20,7 @@ export default class {
this.stop = this.stop.bind(this);
}
get type() { return 'plugin' }
get configs() { return this.__pluginInternals.configs }
get info() { return this.__pluginInternals.info }
get icon() { return this.info.icon }

View File

@ -99,4 +99,10 @@ export default class PluginApi {
return plugin.exports;
}
import(m) {
const module = PluginManager.findPlugin(m);
if (module && module.__require) return module.__require;
return null;
}
}

View File

@ -15,6 +15,32 @@ import Vendor from './vendor';
import { ClientLogger as Logger } from 'common';
import { Events } from 'modules';
class Module {
constructor(pluginInternals) {
this.__pluginInternals = pluginInternals;
this.__require = window.require(this.paths.mainPath);
this.hasSettings = false;
}
get type() { return 'module' }
get configs() { return this.__pluginInternals.configs }
get info() { return this.__pluginInternals.info }
get icon() { return this.info.icon }
get paths() { return this.__pluginInternals.paths }
get main() { return this.__pluginInternals.main }
get defaultConfig() { return this.configs.defaultConfig }
get userConfig() { return this.configs.userConfig }
get id() { return this.info.id || this.info.name.replace(/[^a-zA-Z0-9-]/g, '-').replace(/--/g, '-') }
get name() { return this.info.name }
get authors() { return this.info.authors }
get version() { return this.info.version }
get pluginPath() { return this.paths.contentPath }
get dirName() { return this.paths.dirName }
get enabled() { return true }
get pluginConfig() { return this.userConfig.config || [] }
}
export default class extends ContentManager {
static get localPlugins() {
@ -33,15 +59,24 @@ export default class extends ContentManager {
return 'plugins';
}
static get loadAllPlugins() { return this.loadAllContent }
static async loadAllPlugins() {
const loadAll = await this.loadAllContent();
this.localPlugins.forEach(plugin => {
if (plugin.type === 'module') return;
if (plugin.enabled) plugin.start();
});
return loadAll;
}
static get refreshPlugins() { return this.refreshContent }
static get loadContent() { return this.loadPlugin }
static async loadPlugin(paths, configs, info, main) {
const plugin = window.require(paths.mainPath)(Plugin, new PluginApi(info), Vendor);
const instance = new plugin({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName } });
static async loadPlugin(paths, configs, info, main, type) {
if (instance.enabled) instance.start();
if (type === 'module') return new Module({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName, mainPath: paths.mainPath } });
const plugin = window.require(paths.mainPath)(Plugin, new PluginApi(info), Vendor);
const instance = new plugin({ configs, info, main, paths: { contentPath: paths.contentPath, dirName: paths.dirName, mainPath: paths.mainPath } });
return instance;
}

View File

@ -10,7 +10,7 @@
<template>
<Card :item="plugin">
<SettingSwitch slot="toggle" :checked="plugin.enabled" :change="() => plugin.enabled ? plugin.stop() : plugin.start()" />
<SettingSwitch v-if="plugin.type === 'plugin'" slot="toggle" :checked="plugin.enabled" :change="() => plugin.enabled ? plugin.stop() : plugin.start()" />
<ButtonGroup slot="controls">
<Button v-tooltip="'Settings'" v-if="plugin.hasSettings" :onClick="() => showSettings(plugin)">
<MiSettings size="18" />

View File

@ -5,6 +5,7 @@
"version": 1.0,
"description": "Example Plugin 2 Description"
},
"main": "index.js",
"main": "index.js",
"type": "plugin",
"defaultConfig": []
}

View File

@ -0,0 +1,10 @@
{
"info": {
"name": "Example Module",
"authors": [ "Jiiks" ],
"version": 1.0,
"description": "Module Example"
},
"main": "index.js",
"type": "module"
}

View File

@ -0,0 +1,14 @@
module.exports = class {
constructor() {
}
get foo() {
return 'Bar';
}
add(i1, i2) {
return i1 + i2;
}
}

View File

@ -8,6 +8,7 @@
"icon": "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48IURPQ1RZUEUgc3ZnIFBVQkxJQyAiLS8vVzNDLy9EVEQgU1ZHIDE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MzguNSw3My44LTkzLjgsMTM2LjItMTY0LjYsMTg1LjNjLTIyLjYsMTUuNy00Ni45LDMwLjEtNzIuNiw0My4xaDcyLjVjMzQ2LjIsMS45LDY3MS0xNzEuMiw2NzEtNTY3LjlWNzE2LjdDMTkzMy41LDMxMi4yLDE2MDguNywxMzUuMiwxMjYyLjUsMTM1LjJ6Ii8+PC9nPjwvc3ZnPg=="
},
"main": "index.js",
"type": "plugin",
"defaultConfig": [
{
"category_default_comment": "default category has no header and is always displayed first",

View File

@ -8,6 +8,9 @@ module.exports = (Plugin, Api, Vendor) => {
Events.subscribe('TEST_EVENT', this.eventTest);
Logger.log('onStart');
Logger.log(`Setting "default-0" value: ${this.getSetting('default-0')}`);
const exampleModule = new (Api.import('Example Module'));
Logger.log(`2+4=${exampleModule.add(2, 4)}`);
return true;
}