Parcourir la source

define 'plugin definition objects' in PluginUtils

Yuki Takei il y a 9 ans
Parent
commit
92ab856f2f

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

@@ -1,22 +0,0 @@
-const debug = require('debug')('crowi:plugins:PluginLoaderV2');
-
-class PluginLoaderV2 {
-
-  load(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;

+ 20 - 8
lib/plugins/plugin-utils.js

@@ -2,11 +2,13 @@ const path = require('path');
 const fs = require('hexo-fs');
 
 class PluginUtils {
+
   /**
-   * return following structure for client:
+   * return a array of definition objects that has following structure:
    *
    * [
-   *   'crowi-plugin-X': {
+   *   {
+   *     name: 'crowi-plugin-X',
    *     meta: require('crowi-plugin-X'),
    *     entries: [
    *       require('crowi-plugin-X/lib/client-entry')
@@ -14,15 +16,25 @@ class PluginUtils {
    *   },
    * ]
    *
-   * usage:
-   *  1. define at webpack.DefinePlugin
-   *  2. retrieve from resource/js/plugin.js
-   *
+   * @param {string} rootDir Crowi Project root dir
    * @return
    * @memberOf PluginService
    */
-  generatePluginDefinitions() {
-    // TODO impl
+  generatePluginDefinitions(rootDir, isForClient = false) {
+    return this.listPluginNames(rootDir)
+      .map((name) => {
+        const meta = require(name);
+        const entries = (isForClient) ? meta.clientEntries : meta.serverEntries;
+
+        return {
+          name: name,
+          meta: meta,
+          entries: entries.map((entryPath) => {
+            return require(entryPath);
+          })
+        }
+
+      });
   }
 
   /**

+ 10 - 9
lib/plugins/plugin.service.js

@@ -1,6 +1,5 @@
 const debug = require('debug')('crowi:plugins:PluginService');
 const PluginUtils = require('./plugin-utils');
-const PluginLoaderV2 = require('./plugin-loader-v2.model');
 
 class PluginService {
 
@@ -8,19 +7,18 @@ class PluginService {
     this.crowi = crowi;
     this.app = app;
     this.pluginUtils = new PluginUtils();
-    this.pluginLoaderV2 = new PluginLoaderV2();
   }
 
   loadPlugins() {
     let self = this;
-    this.pluginUtils.listPluginNames(this.crowi.rootDir)
-      .map(function(name) {
-        self.loadPlugin(name);
+    this.pluginUtils.generatePluginDefinitions(this.crowi.rootDir)
+      .map((definition) => {
+        self.loadPlugin(definition);
       });
   }
 
-  loadPlugin(name) {
-    const meta = require(name);
+  loadPlugin(definition) {
+    const meta = definition.meta;
 
     // v1 is deprecated
     if (1 === meta.pluginSchemaVersion) {
@@ -30,8 +28,11 @@ class PluginService {
 
     // v2
     if (2 === meta.pluginSchemaVersion) {
-      this.pluginLoaderV2.load(name, this.crowi, this.app);
-      return;
+      debug(`load plugin '${definition.name}'`);
+
+      definition.entries.forEach((entry) => {
+        entry(this.crowi, this.app);
+      });
     }
   }
 }