Răsfoiți Sursa

refactor PluginService (add PluginUtils)

Yuki Takei 9 ani în urmă
părinte
comite
1c2787c637

+ 1 - 1
lib/plugins/plugin-loader-v2.model.js

@@ -2,7 +2,7 @@ const debug = require('debug')('crowi:plugins:PluginLoaderV2');
 
 class PluginLoaderV2 {
 
-  loadForServer(name, crowi, app) {
+  load(name, crowi, app) {
     const meta = require(name);
 
     const entries = meta.serverEntries

+ 56 - 0
lib/plugins/plugin-utils.js

@@ -0,0 +1,56 @@
+const path = require('path');
+const fs = require('hexo-fs');
+
+class PluginUtils {
+  /**
+   * return following structure for client:
+   *
+   * [
+   *   'crowi-plugin-X': {
+   *     meta: require('crowi-plugin-X'),
+   *     entries: [
+   *       require('crowi-plugin-X/lib/client-entry')
+   *     ]
+   *   },
+   * ]
+   *
+   * usage:
+   *  1. define at webpack.DefinePlugin
+   *  2. retrieve from resource/js/plugin.js
+   *
+   * @return
+   * @memberOf PluginService
+   */
+  generatePluginDefinitions() {
+    // TODO impl
+  }
+
+  /**
+   * list plugin module names that starts with 'crowi-plugin-'
+   * borrowing from: https://github.com/hexojs/hexo/blob/d1db459c92a4765620343b95789361cbbc6414c5/lib/hexo/load_plugins.js#L17
+   *
+   * @returns
+   *
+   * @memberOf PluginService
+   */
+  listPluginNames(rootDir) {
+    var packagePath = path.join(rootDir, 'package.json');
+
+    // Make sure package.json exists
+    return fs.exists(packagePath).then(function(exist) {
+      if (!exist) return [];
+
+      // Read package.json and find dependencies
+      return fs.readFile(packagePath).then(function(content) {
+        var json = JSON.parse(content);
+        var deps = json.dependencies || {};
+        return Object.keys(deps);
+      });
+    }).filter(function(name) {
+      // Ignore plugins whose name is not started with "crowi-"
+      return /^crowi-plugin-/.test(name);
+    });
+  }
+}
+
+module.exports = PluginUtils

+ 4 - 36
lib/plugins/plugin.service.js

@@ -1,6 +1,5 @@
 const debug = require('debug')('crowi:plugins:PluginService');
-const path = require('path');
-const fs = require('hexo-fs');
+const PluginUtils = require('./plugin-utils');
 const PluginLoaderV2 = require('./plugin-loader-v2.model');
 
 class PluginService {
@@ -8,44 +7,13 @@ class PluginService {
   constructor(crowi, app) {
     this.crowi = crowi;
     this.app = app;
+    this.pluginUtils = new PluginUtils();
     this.pluginLoaderV2 = new PluginLoaderV2();
   }
 
-  /**
-   * list plugin module names that starts with 'crowi-plugin-'
-   * borrowing from: https://github.com/hexojs/hexo/blob/d1db459c92a4765620343b95789361cbbc6414c5/lib/hexo/load_plugins.js#L17
-   *
-   * @returns
-   *
-   * @memberOf PluginService
-   */
-  listPluginNames() {
-    var packagePath = path.join(this.crowi.rootDir, 'package.json');
-    var pluginDir = this.crowi.pluginDir;
-
-    // Make sure package.json exists
-    return fs.exists(packagePath).then(function(exist) {
-      if (!exist) return [];
-
-      // Read package.json and find dependencies
-      return fs.readFile(packagePath).then(function(content) {
-        var json = JSON.parse(content);
-        var deps = json.dependencies || {};
-        return Object.keys(deps);
-      });
-    }).filter(function(name) {
-      // Ignore plugins whose name is not started with "crowi-"
-      if (!/^crowi-plugin-/.test(name)) return false;
-
-      // Make sure the plugin exists
-      var pluginPath = path.join(pluginDir, name);
-      return fs.exists(pluginPath);
-    });
-  }
-
   loadPlugins() {
     let self = this;
-    this.listPluginNames()
+    this.pluginUtils.listPluginNames(this.crowi.rootDir)
       .map(function(name) {
         self.loadPlugin(name);
       });
@@ -62,7 +30,7 @@ class PluginService {
 
     // v2
     if (2 === meta.pluginSchemaVersion) {
-      this.pluginLoaderV2.loadForServer(name, this.crowi, this.app);
+      this.pluginLoaderV2.load(name, this.crowi, this.app);
       return;
     }
   }