Yuki Takei 9 лет назад
Родитель
Сommit
957a244cd8
4 измененных файлов с 97 добавлено и 23 удалено
  1. 3 23
      lib/plugins/index.js
  2. 22 0
      lib/plugins/plugin-loader-v2.model.js
  3. 71 0
      lib/plugins/plugin.service.js
  4. 1 0
      package.json

+ 3 - 23
lib/plugins/index.js

@@ -1,28 +1,8 @@
-const plugins = {
-  // 'crowi-plugin-X': {
-  //   meta: require('crowi-plugin-X'),
-  //   entries: [
-  //     require('crowi-plugin-X/lib/server-entry')
-  //   ]
-  // },
-}
+const PluginService = require('./plugin.service');
 
 module.exports = function(crowi, app) {
   var debug = require('debug')('crowi:plugins');
 
-  for (var pluginName of Object.keys(plugins)) {
-    var meta = plugins[pluginName].meta;
-    var entries = plugins[pluginName].entries;
-
-    // v1 is deprecated
-
-    // v2
-    if (2 === meta.pluginSchemaVersion) {
-      debug(`import plugin entries for '${pluginName}'`);
-
-      entries.forEach((entry) => {
-        entry(crowi, app);
-      });
-    }
-  }
+  const pluginService = new PluginService(crowi, app);
+  pluginService.loadPlugins();
 }

+ 22 - 0
lib/plugins/plugin-loader-v2.model.js

@@ -0,0 +1,22 @@
+const debug = require('debug')('crowi:plugins:PluginLoaderV2');
+
+class PluginLoaderV2 {
+
+  loadForServer(name, crowi, app) {
+    const meta = require(name);
+
+    const entries = meta.serverEntries
+      .map(function(entryPath) {
+        return require(entryPath);
+      });
+
+    debug(`load plugin '${name}'`);
+
+    entries.forEach((entry) => {
+      entry(crowi, app);
+    });
+  }
+
+}
+
+module.exports = PluginLoaderV2;

+ 71 - 0
lib/plugins/plugin.service.js

@@ -0,0 +1,71 @@
+const debug = require('debug')('crowi:plugins:PluginService');
+const path = require('path');
+const fs = require('hexo-fs');
+const PluginLoaderV2 = require('./plugin-loader-v2.model');
+
+class PluginService {
+
+  constructor(crowi, app) {
+    this.crowi = crowi;
+    this.app = app;
+    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()
+      .map(function(name) {
+        self.loadPlugin(name);
+      });
+  }
+
+  loadPlugin(name) {
+    const meta = require(name);
+
+    // v1 is deprecated
+    if (1 === meta.pluginSchemaVersion) {
+      debug('pluginSchemaVersion 1 is deprecated');
+      return;
+    }
+
+    // v2
+    if (2 === meta.pluginSchemaVersion) {
+      this.pluginLoaderV2.loadForServer(name, this.crowi, this.app);
+      return;
+    }
+  }
+}
+
+module.exports = PluginService;

+ 1 - 0
package.json

@@ -67,6 +67,7 @@
     "gulp-spawn-mocha": "~2.2.1",
     "gulp-uglify": "~1.4.2",
     "gulp-watch": "~4.3.5",
+    "hexo-fs": "^0.1.6",
     "highlight.js": "~9.9.0",
     "i18next": "~4.1.0",
     "i18next-express-middleware": "~1.0.2",