You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by ro...@apache.org on 2024/10/11 03:53:23 UTC

(incubator-answer) branch dev updated (500c774f -> ad477136)

This is an automated email from the ASF dual-hosted git repository.

robin0716 pushed a change to branch dev
in repository https://gitbox.apache.org/repos/asf/incubator-answer.git


    from 500c774f fix: Some contents of en_us.yaml have newline errors
     new 62d2d2a8 refactor(pluginKit): improve plugin registration process
     new 9479f1f8 chore(ui): Dynamically import plugins
     new dc61ab7e refactor(pluginKit): update plugin registration process
     new 84a49321 chore(ui): update pre-install script in package.json
     new 95b9af55 refactor(pluginKit): update addPluginToIndexTs function to include pluginFolder parameter
     new 59080727 refactor(ui): update pre-install script in package.json
     new df507a9d refactor(pluginKit): remove unused loadPlugins function
     new ad477136 refactor(ui): update pluginKit refresh method

The 8 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 ui/package.json                                |  2 +-
 ui/scripts/importPlugins.js                    | 68 ++++++++++++++++++++++++++
 ui/scripts/{plugin.js => loadPlugins.js}       | 47 +++++-------------
 ui/scripts/preinstall.js                       |  2 +-
 ui/src/pages/Admin/Plugins/Installed/index.tsx |  2 +-
 ui/src/utils/pluginKit/index.ts                | 36 +++++++++-----
 6 files changed, 107 insertions(+), 50 deletions(-)
 create mode 100644 ui/scripts/importPlugins.js
 rename ui/scripts/{plugin.js => loadPlugins.js} (67%)


(incubator-answer) 04/08: chore(ui): update pre-install script in package.json

Posted by ro...@apache.org.
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 84a49321f19da0bab26820a72d4c7d71a2cc6387
Author: robin <ro...@rnode.me>
AuthorDate: Wed Oct 9 14:21:51 2024 +0800

    chore(ui): update pre-install script in package.json
---
 ui/package.json | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/ui/package.json b/ui/package.json
index 2344125d..51cd29d1 100644
--- a/ui/package.json
+++ b/ui/package.json
@@ -8,7 +8,7 @@
     "build": "node ./scripts/env.js && react-app-rewired build",
     "lint": "eslint . --cache --fix --ext .ts,.tsx",
     "prettier": "prettier --write 'src/**/*.{js,jsx,ts,tsx,json,css,scss,md}' --config ./.prettierrc.json",
-    "pre-install": "node ./scripts/preinstall.js && pnpm install",
+    "pre-install": "pnpm install && node ./scripts/preinstall.js",
     "prepare": "pnpm build:packages",
     "pre-commit": "lint-staged",
     "build:packages": "pnpm -r --filter=./src/plugins/* run build",


(incubator-answer) 01/08: refactor(pluginKit): improve plugin registration process

Posted by ro...@apache.org.
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 62d2d2a8b356043ca1eb5ff8c4d6d46c9c76c7c4
Author: robin <ro...@rnode.me>
AuthorDate: Tue Oct 8 16:50:14 2024 +0800

    refactor(pluginKit): improve plugin registration process
---
 ui/src/utils/pluginKit/index.ts | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)

diff --git a/ui/src/utils/pluginKit/index.ts b/ui/src/utils/pluginKit/index.ts
index 12cd0247..920b0a3c 100644
--- a/ui/src/utils/pluginKit/index.ts
+++ b/ui/src/utils/pluginKit/index.ts
@@ -49,12 +49,12 @@ class Plugins {
 
   constructor() {
     this.registerBuiltin();
-    this.registerPlugins();
-
-    getPluginsStatus().then((plugins) => {
-      this.registeredPlugins = plugins;
-      this.activatePlugins(plugins);
-    });
+    this.registerPlugins()
+      .then(getPluginsStatus)
+      .then((plugins) => {
+        this.registeredPlugins = plugins;
+        this.activatePlugins(plugins);
+      });
   }
 
   validate(plugin: Plugin) {
@@ -75,6 +75,15 @@ class Plugins {
     return true;
   }
 
+  async loadPlugins() {
+    return Promise.all(
+      Object.keys(allPlugins).map(async (key) => {
+        const plugin = await allPlugins[key]();
+        return plugin;
+      }),
+    );
+  }
+
   registerBuiltin() {
     Object.keys(builtin).forEach((key) => {
       const plugin = builtin[key];
@@ -86,9 +95,13 @@ class Plugins {
   }
 
   registerPlugins() {
-    Object.keys(allPlugins).forEach((key) => {
-      const plugin = allPlugins[key];
-      this.register(plugin);
+    return new Promise((resolve) => {
+      this.loadPlugins().then((plugins) => {
+        plugins.forEach((plugin) => {
+          this.register(plugin);
+        });
+        resolve(true);
+      });
     });
   }
 


(incubator-answer) 08/08: refactor(ui): update pluginKit refresh method

Posted by ro...@apache.org.
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 ad4771364140679dde67670602db6d735544b879
Author: robin <ro...@rnode.me>
AuthorDate: Fri Oct 11 11:21:29 2024 +0800

    refactor(ui): update pluginKit refresh method
---
 ui/src/pages/Admin/Plugins/Installed/index.tsx |  2 +-
 ui/src/utils/pluginKit/index.ts                | 16 +++++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)

diff --git a/ui/src/pages/Admin/Plugins/Installed/index.tsx b/ui/src/pages/Admin/Plugins/Installed/index.tsx
index 82e0618b..868b827a 100644
--- a/ui/src/pages/Admin/Plugins/Installed/index.tsx
+++ b/ui/src/pages/Admin/Plugins/Installed/index.tsx
@@ -66,7 +66,7 @@ const Users: FC = () => {
       plugin_slug_name: plugin.slug_name,
     }).then(() => {
       updatePlugins();
-      PluginKit.changePluginActiveStatus(plugin.slug_name, !plugin.enabled);
+      PluginKit.refresh();
       if (plugin.have_config) {
         emitPluginChange('refreshConfigurablePlugins');
       }
diff --git a/ui/src/utils/pluginKit/index.ts b/ui/src/utils/pluginKit/index.ts
index 8e19d881..f53fdb4c 100644
--- a/ui/src/utils/pluginKit/index.ts
+++ b/ui/src/utils/pluginKit/index.ts
@@ -48,6 +48,10 @@ class Plugins {
   registeredPlugins: Type.ActivatedPlugin[] = [];
 
   constructor() {
+    this.init();
+  }
+
+  init() {
     this.registerBuiltin();
 
     getPluginsStatus().then((plugins) => {
@@ -56,6 +60,11 @@ class Plugins {
     });
   }
 
+  refresh() {
+    this.plugins = [];
+    this.init();
+  }
+
   validate(plugin: Plugin) {
     if (!plugin) {
       return false;
@@ -123,13 +132,6 @@ class Plugins {
     });
   }
 
-  changePluginActiveStatus(slug_name: string, active: boolean) {
-    const plugin = this.getPlugin(slug_name);
-    if (plugin) {
-      plugin.activated = active;
-    }
-  }
-
   getPlugin(slug_name: string) {
     return this.plugins.find((p) => p.info.slug_name === slug_name);
   }


(incubator-answer) 05/08: refactor(pluginKit): update addPluginToIndexTs function to include pluginFolder parameter

Posted by ro...@apache.org.
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 95b9af557f1d40eebf58d5433b44c2e57d39640d
Author: robin <ro...@rnode.me>
AuthorDate: Thu Oct 10 10:37:36 2024 +0800

    refactor(pluginKit): update addPluginToIndexTs function to include pluginFolder parameter
---
 ui/scripts/plugin.js | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/ui/scripts/plugin.js b/ui/scripts/plugin.js
index ede9f13f..7ad3d7c5 100644
--- a/ui/scripts/plugin.js
+++ b/ui/scripts/plugin.js
@@ -59,14 +59,14 @@ function addPluginToPackageJson(packageName) {
   );
 }
 
-function addPluginToIndexTs(packageName) {
+function addPluginToIndexTs(packageName, pluginFolder) {
   const indexTsPath = path.join(pluginPath, 'index.ts');
   const indexTsContent = fs.readFileSync(indexTsPath, 'utf-8');
   const lines = indexTsContent.split('\n');
   const ComponentName = pascalize(packageName);
 
   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 info = yaml.load(fs.readFileSync(path.join(pluginFolder, 'info.yaml'), 'utf8'));
   const exportLine = `export const ${info.slug_name} = load${ComponentName}`;
 
   if (!lines.includes(exportLine)) {
@@ -101,6 +101,6 @@ pluginFolders.forEach((folder) => {
     const packageName = packageJson.name;
 
     addPluginToPackageJson(packageName);
-    addPluginToIndexTs(packageName);
+    addPluginToIndexTs(packageName, pluginFolder);
   }
 });


(incubator-answer) 07/08: refactor(pluginKit): remove unused loadPlugins function

Posted by ro...@apache.org.
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 df507a9de5a772d8bf432a9a0c9887a8a249873f
Author: robin <ro...@rnode.me>
AuthorDate: Fri Oct 11 10:38:37 2024 +0800

    refactor(pluginKit): remove unused loadPlugins function
---
 ui/src/utils/pluginKit/index.ts | 9 ---------
 1 file changed, 9 deletions(-)

diff --git a/ui/src/utils/pluginKit/index.ts b/ui/src/utils/pluginKit/index.ts
index a57b53ed..8e19d881 100644
--- a/ui/src/utils/pluginKit/index.ts
+++ b/ui/src/utils/pluginKit/index.ts
@@ -74,15 +74,6 @@ class Plugins {
     return true;
   }
 
-  async loadPlugins() {
-    return Promise.all(
-      Object.keys(allPlugins).map(async (key) => {
-        const plugin = await allPlugins[key]();
-        return plugin;
-      }),
-    );
-  }
-
   registerBuiltin() {
     Object.keys(builtin).forEach((key) => {
       const plugin = builtin[key];


(incubator-answer) 02/08: chore(ui): Dynamically import plugins

Posted by ro...@apache.org.
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 9479f1f80172cee4e61a376c96a8269fc2cc6c83
Author: robin <ro...@rnode.me>
AuthorDate: Tue Oct 8 16:52:36 2024 +0800

    chore(ui): Dynamically import plugins
---
 ui/scripts/plugin.js | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/ui/scripts/plugin.js b/ui/scripts/plugin.js
index d554df8f..a3f09df5 100644
--- a/ui/scripts/plugin.js
+++ b/ui/scripts/plugin.js
@@ -63,7 +63,8 @@ function addPluginToIndexTs(packageName) {
   const indexTsContent = fs.readFileSync(indexTsPath, 'utf-8');
   const lines = indexTsContent.split('\n');
   const ComponentName = pascalize(packageName);
-  const importLine = `export { default as ${ComponentName} } from '${packageName}';`;
+
+  const importLine = `export const load${ComponentName} = () => import('${packageName}').then(module => module.default);`;
   if (!lines.includes(importLine)) {
     lines.push(importLine);
   }


(incubator-answer) 06/08: refactor(ui): update pre-install script in package.json

Posted by ro...@apache.org.
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 59080727cf4ff074953f0cafc96ed73f4de2c789
Author: robin <ro...@rnode.me>
AuthorDate: Thu Oct 10 17:47:36 2024 +0800

    refactor(ui): update pre-install script in package.json
---
 ui/package.json                          |  2 +-
 ui/scripts/importPlugins.js              | 68 ++++++++++++++++++++++++++++++++
 ui/scripts/{plugin.js => loadPlugins.js} | 30 +-------------
 ui/scripts/preinstall.js                 |  2 +-
 4 files changed, 71 insertions(+), 31 deletions(-)

diff --git a/ui/package.json b/ui/package.json
index 51cd29d1..3d9c2614 100644
--- a/ui/package.json
+++ b/ui/package.json
@@ -8,7 +8,7 @@
     "build": "node ./scripts/env.js && react-app-rewired build",
     "lint": "eslint . --cache --fix --ext .ts,.tsx",
     "prettier": "prettier --write 'src/**/*.{js,jsx,ts,tsx,json,css,scss,md}' --config ./.prettierrc.json",
-    "pre-install": "pnpm install && node ./scripts/preinstall.js",
+    "pre-install": "node ./scripts/importPlugins.js && pnpm install && node ./scripts/preinstall.js ",
     "prepare": "pnpm build:packages",
     "pre-commit": "lint-staged",
     "build:packages": "pnpm -r --filter=./src/plugins/* run build",
diff --git a/ui/scripts/importPlugins.js b/ui/scripts/importPlugins.js
new file mode 100644
index 00000000..0862a015
--- /dev/null
+++ b/ui/scripts/importPlugins.js
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+const path = require('path');
+const fs = require('fs');
+
+const pluginPath = path.join(__dirname, '../src/plugins');
+const pluginFolders = fs.readdirSync(pluginPath);
+
+function resetPackageJson() {
+    const packageJsonPath = path.join(__dirname, '..', 'package.json');
+    const packageJsonContent = require(packageJsonPath);
+    const dependencies = packageJsonContent.dependencies;
+    for (const key in dependencies) {
+        if (dependencies[key].startsWith('workspace')) {
+            delete dependencies[key];
+        }
+    }
+    fs.writeFileSync(
+        packageJsonPath,
+        JSON.stringify(packageJsonContent, null, 2),
+    );
+}
+
+function addPluginToPackageJson(packageName) {
+    const packageJsonPath = path.join(__dirname, '..', 'package.json');
+    const packageJsonContent = require(packageJsonPath);
+    packageJsonContent.dependencies[packageName] = 'workspace:*';
+
+    fs.writeFileSync(
+        packageJsonPath,
+        JSON.stringify(packageJsonContent, null, 2),
+    );
+}
+
+
+resetPackageJson();
+
+pluginFolders.forEach((folder) => {
+    const pluginFolder = path.join(pluginPath, folder);
+    const stat = fs.statSync(pluginFolder);
+
+    if (stat.isDirectory() && folder !== 'builtin') {
+        if (!fs.existsSync(path.join(pluginFolder, 'index.ts'))) {
+            return;
+        }
+        const packageJson = require(path.join(pluginFolder, 'package.json'));
+        const packageName = packageJson.name;
+
+        addPluginToPackageJson(packageName);
+    }
+});
\ No newline at end of file
diff --git a/ui/scripts/plugin.js b/ui/scripts/loadPlugins.js
similarity index 76%
rename from ui/scripts/plugin.js
rename to ui/scripts/loadPlugins.js
index 7ad3d7c5..77b5fe5d 100644
--- a/ui/scripts/plugin.js
+++ b/ui/scripts/loadPlugins.js
@@ -28,37 +28,11 @@ function pascalize(str) {
   return str.split(/[_-]/).map((part) => part.charAt(0).toUpperCase() + part.slice(1)).join('');
 }
 
-function resetPackageJson() {
-  const packageJsonPath = path.join(__dirname, '..', 'package.json');
-  const packageJsonContent = require(packageJsonPath);
-  const dependencies = packageJsonContent.dependencies;
-  for (const key in dependencies) {
-    if (dependencies[key].startsWith('workspace')) {
-      delete dependencies[key];
-    }
-  }
-  fs.writeFileSync(
-    packageJsonPath,
-    JSON.stringify(packageJsonContent, null, 2),
-  );
-}
-
 function resetIndexTs() {
   const indexTsPath = path.join(pluginPath, 'index.ts');
   fs.writeFileSync(indexTsPath, '');
 }
 
-function addPluginToPackageJson(packageName) {
-  const packageJsonPath = path.join(__dirname, '..', 'package.json');
-  const packageJsonContent = require(packageJsonPath);
-  packageJsonContent.dependencies[packageName] = 'workspace:*';
-
-  fs.writeFileSync(
-    packageJsonPath,
-    JSON.stringify(packageJsonContent, null, 2),
-  );
-}
-
 function addPluginToIndexTs(packageName, pluginFolder) {
   const indexTsPath = path.join(pluginPath, 'index.ts');
   const indexTsContent = fs.readFileSync(indexTsPath, 'utf-8');
@@ -87,8 +61,6 @@ if (pluginLength > 0) {
   resetIndexTs();
 }
 
-resetPackageJson();
-
 pluginFolders.forEach((folder) => {
   const pluginFolder = path.join(pluginPath, folder);
   const stat = fs.statSync(pluginFolder);
@@ -100,7 +72,7 @@ pluginFolders.forEach((folder) => {
     const packageJson = require(path.join(pluginFolder, 'package.json'));
     const packageName = packageJson.name;
 
-    addPluginToPackageJson(packageName);
     addPluginToIndexTs(packageName, pluginFolder);
   }
 });
+
diff --git a/ui/scripts/preinstall.js b/ui/scripts/preinstall.js
index e3608091..94de3f32 100644
--- a/ui/scripts/preinstall.js
+++ b/ui/scripts/preinstall.js
@@ -19,7 +19,7 @@
 
 // There is a bug when using npm to install: the execution of preinstall is after install, so when this prompt is displayed, the dependent packages have already been installed.
 
-require('./plugin');
+require('./loadPlugins');
 
 if (!/pnpm/.test(process.env.npm_execpath)) {
   console.warn(


(incubator-answer) 03/08: refactor(pluginKit): update plugin registration process

Posted by ro...@apache.org.
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 <ro...@rnode.me>
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);
   }