Selaa lähdekoodia

rewrite PluginUtils.listPluginNames synchronous ver

Yuki Takei 9 vuotta sitten
vanhempi
sitoutus
0349524f1a
2 muutettua tiedostoa jossa 41 lisäystä ja 41 poistoa
  1. 30 37
      lib/plugins/plugin-utils.js
  2. 11 4
      lib/plugins/plugin.service.js

+ 30 - 37
lib/plugins/plugin-utils.js

@@ -1,40 +1,34 @@
 const path = require('path');
-const fs = require('hexo-fs');
+const fs = require('graceful-fs');
 
 class PluginUtils {
 
   /**
-   * return a array of definition objects that has following structure:
+   * return a definition objects that has following structure:
    *
-   * [
-   *   {
-   *     name: 'crowi-plugin-X',
-   *     meta: require('crowi-plugin-X'),
-   *     entries: [
-   *       require('crowi-plugin-X/lib/client-entry')
-   *     ]
-   *   },
-   * ]
+   * {
+   *   name: 'crowi-plugin-X',
+   *   meta: require('crowi-plugin-X'),
+   *   entries: [
+   *     require('crowi-plugin-X/lib/client-entry')
+   *   ]
+   * }
    *
-   * @param {string} rootDir Crowi Project root dir
+   * @param {string} pluginName
    * @return
    * @memberOf PluginService
    */
-  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);
-          })
-        }
-
-      });
+  generatePluginDefinition(pluginName, isForClient = false) {
+    const meta = require(pluginName);
+    const entries = (isForClient) ? meta.clientEntries : meta.serverEntries;
+
+    return {
+      name: pluginName,
+      meta: meta,
+      entries: entries.map((entryPath) => {
+        return require(entryPath);
+      })
+    }
   }
 
   /**
@@ -49,16 +43,15 @@ class PluginUtils {
     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) {
+    if (!fs.existsSync(packagePath)) {
+      return [];
+    }
+
+    // Read package.json and find dependencies
+    const content = fs.readFileSync(packagePath);
+    const json = JSON.parse(content);
+    const deps = json.dependencies || {};
+    return Object.keys(deps).filter((name) => {
       // Ignore plugins whose name is not started with "crowi-"
       return /^crowi-plugin-/.test(name);
     });

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

@@ -9,11 +9,18 @@ class PluginService {
     this.pluginUtils = new PluginUtils();
   }
 
+  /**
+   * load plugins
+   *
+   * @memberOf PluginService
+   */
   loadPlugins() {
-    let self = this;
-    this.pluginUtils.generatePluginDefinitions(this.crowi.rootDir)
-      .map((definition) => {
-        self.loadPlugin(definition);
+    this.pluginUtils.listPluginNames(this.crowi.rootDir)
+      .map((name) => {
+        return this.pluginUtils.generatePluginDefinition(name);
+      })
+      .forEach((definition) => {
+        this.loadPlugin(definition);
       });
   }