This is an automated email from the ASF dual-hosted git repository. robin0716 pushed a commit to branch dev in repository https://gitbox.apache.org/repos/asf/incubator-answer.git
commit dc61ab7e830512f02c06d2418e35ace7fdff2406 Author: robin <[email protected]> AuthorDate: Wed Oct 9 12:16:54 2024 +0800 refactor(pluginKit): update plugin registration process --- ui/scripts/plugin.js | 12 +++++++++--- ui/src/utils/pluginKit/index.ts | 26 +++++++++++++++----------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/ui/scripts/plugin.js b/ui/scripts/plugin.js index a3f09df5..ede9f13f 100644 --- a/ui/scripts/plugin.js +++ b/ui/scripts/plugin.js @@ -19,6 +19,7 @@ const path = require('path'); const fs = require('fs'); +const yaml = require('js-yaml'); const pluginPath = path.join(__dirname, '../src/plugins'); const pluginFolders = fs.readdirSync(pluginPath); @@ -64,10 +65,15 @@ function addPluginToIndexTs(packageName) { const lines = indexTsContent.split('\n'); const ComponentName = pascalize(packageName); - const importLine = `export const load${ComponentName} = () => import('${packageName}').then(module => module.default);`; - if (!lines.includes(importLine)) { + const importLine = `const load${ComponentName} = () => import('${packageName}').then(module => module.default);`; + const info = yaml.load(fs.readFileSync(path.join(pluginPath, packageName, 'info.yaml'), 'utf8')); + const exportLine = `export const ${info.slug_name} = load${ComponentName}`; + + if (!lines.includes(exportLine)) { lines.push(importLine); + lines.push(exportLine); } + fs.writeFileSync(indexTsPath, lines.join('\n')); } @@ -75,7 +81,7 @@ const pluginLength = pluginFolders.filter((folder) => { const pluginFolder = path.join(pluginPath, folder); const stat = fs.statSync(pluginFolder); return stat.isDirectory() && folder !== 'builtin'; -}).length +}).length; if (pluginLength > 0) { resetIndexTs(); diff --git a/ui/src/utils/pluginKit/index.ts b/ui/src/utils/pluginKit/index.ts index 920b0a3c..a57b53ed 100644 --- a/ui/src/utils/pluginKit/index.ts +++ b/ui/src/utils/pluginKit/index.ts @@ -49,12 +49,11 @@ class Plugins { constructor() { this.registerBuiltin(); - this.registerPlugins() - .then(getPluginsStatus) - .then((plugins) => { - this.registeredPlugins = plugins; - this.activatePlugins(plugins); - }); + + getPluginsStatus().then((plugins) => { + this.registeredPlugins = plugins.filter((p) => p.enabled); + this.registerPlugins(); + }); } validate(plugin: Plugin) { @@ -95,13 +94,17 @@ class Plugins { } registerPlugins() { + const plugins = this.registeredPlugins.map((p) => { + const func = allPlugins[p.slug_name]; + + return func; + }); return new Promise((resolve) => { - this.loadPlugins().then((plugins) => { - plugins.forEach((plugin) => { - this.register(plugin); - }); - resolve(true); + plugins.forEach(async (p) => { + const plugin = await p(); + this.register(plugin); }); + resolve(true); }); } @@ -113,6 +116,7 @@ class Plugins { if (plugin.i18nConfig) { initI18nResource(plugin.i18nConfig); } + plugin.activated = true; this.plugins.push(plugin); }
