Просмотр исходного кода

ensure to load plugins easily when development

Yuki Takei 9 лет назад
Родитель
Сommit
9563f88816

+ 17 - 1
bin/generate-plugin-definitions-source.js

@@ -14,8 +14,24 @@ const OUT = helpers.root('tmp/plugins/plugin-definitions.js');
 const PluginUtils = require('../lib/plugins/plugin-utils');
 const pluginUtils = new PluginUtils();
 
+// list plugin names
+let pluginNames = pluginUtils.listPluginNames(helpers.root());
+if (process.env.NODE_ENV === 'development'
+    && process.env.PLUGIN_NAMES_TOBE_LOADED !== undefined
+    && process.env.PLUGIN_NAMES_TOBE_LOADED.length > 0) {
+
+  pluginNamesDev = process.env.PLUGIN_NAMES_TOBE_LOADED.split(',');
+
+  // merge and remove duplicates
+  if (pluginNamesDev.length > 0) {
+    pluginNames = pluginNames.concat(pluginNamesDev);
+    pluginNames = Array.from(new Set(pluginNames));
+  }
+}
+
+
 // get definitions
-const definitions = pluginUtils.listPluginNames(helpers.root())
+const definitions = pluginNames
   .map((name) => {
     return pluginUtils.generatePluginDefinition(name, true);
   })

+ 7 - 0
config/env.dev.js

@@ -0,0 +1,7 @@
+module.exports = {
+  NODE_ENV: 'development',
+  PLUGIN_NAMES_TOBE_LOADED: [
+    // 'crowi-plugin-lsx',
+    // 'crowi-plugin-pukiwiki-like-linker',
+  ]
+}

+ 27 - 10
lib/crowi/dev.js

@@ -3,34 +3,40 @@ const path = require('path');
 const webpack = require('webpack');
 const helpers = require('./helpers')
 
-
 class CrowiDev {
 
   /**
    * Creates an instance of CrowiDev.
    * @param {Crowi} crowi
-   * @param {any} server http server
-   * @param {any} app express
    *
    * @memberOf CrowiDev
    */
-  constructor(crowi, server, app) {
+  constructor(crowi) {
     this.crowi = crowi;
-    this.server = server;
-    this.app = app;
   }
 
-  setupTools() {
-    this.setupEasyLiveReload();
+  init() {
   }
 
-  setupEasyLiveReload() {
+  /**
+   *
+   *
+   * @param {any} server http server
+   * @param {any} app express
+   *
+   * @memberOf CrowiDev
+   */
+  setup(server, app) {
+    this.setupEasyLiveReload(app);
+  }
+
+  setupEasyLiveReload(app) {
     if (!helpers.hasProcessFlag('watch')) {
       return;
     }
 
     const livereload = require('easy-livereload');
-    this.app.use(livereload({
+    app.use(livereload({
       watchDirs: [
         path.join(this.crowi.viewsDir),
         path.join(this.crowi.publicDir),
@@ -41,6 +47,17 @@ class CrowiDev {
     }));
   }
 
+  loadPlugins(app) {
+    if (process.env.PLUGIN_NAMES_TOBE_LOADED === undefined) {
+      return
+    }
+
+    const pluginNames = process.env.PLUGIN_NAMES_TOBE_LOADED.split(',');
+
+    var PluginService = require('../plugins/plugin.service');
+    var pluginService = new PluginService(this.crowi, app);
+    pluginService.loadPlugins(pluginNames);
+  }
 }
 
 module.exports = CrowiDev

+ 16 - 5
lib/crowi/index.js

@@ -295,6 +295,13 @@ Crowi.prototype.start = function() {
     , server
     , io;
 
+  // init CrowiDev
+  if (self.node_env === 'development') {
+    const CrowiDev = require('./dev');
+    this.crowiDev = new CrowiDev(self);
+    this.crowiDev.init();
+  }
+
   return Promise.resolve()
     .then(function() {
       return self.init()
@@ -307,11 +314,9 @@ Crowi.prototype.start = function() {
         console.log(`[${self.node_env}] Express server listening on port ${self.port}`);
       });
 
-      // setup Live Reload Tools
+      // setup
       if (self.node_env === 'development') {
-        const CrowiDev = require('./dev');
-        const crowiDev = new CrowiDev(self, server, app);
-        crowiDev.setupTools();
+        self.crowiDev.setup(server, app);
       }
 
       io = require('socket.io')(server);
@@ -341,7 +346,13 @@ Crowi.prototype.buildServer = function() {
   var isEnabledPlugins = Config.isEnabledPlugins(this.config);
   if (isEnabledPlugins) {
     debug('plugins enabled');
-    require('../plugins')(this, app);
+    var PluginService = require('../plugins/plugin.service');
+    var pluginService = new PluginService(this, app);
+    pluginService.autoDetectAndLoadPlugins();
+
+    if (env == 'development') {
+      this.crowiDev.loadPlugins(app);
+    }
   }
 
   if (env == 'development') {

+ 0 - 7
lib/plugins/index.js

@@ -1,7 +0,0 @@
-const debug = require('debug')('crowi:plugins');
-const PluginService = require('./plugin.service');
-
-module.exports = function(crowi, app) {
-  const pluginService = new PluginService(crowi, app);
-  pluginService.loadPlugins();
-}

+ 6 - 2
lib/plugins/plugin.service.js

@@ -9,13 +9,17 @@ class PluginService {
     this.pluginUtils = new PluginUtils();
   }
 
+  autoDetectAndLoadPlugins() {
+    this.loadPlugins(this.pluginUtils.listPluginNames(this.crowi.rootDir));
+  }
+
   /**
    * load plugins
    *
    * @memberOf PluginService
    */
-  loadPlugins() {
-    this.pluginUtils.listPluginNames(this.crowi.rootDir)
+  loadPlugins(pluginNames) {
+    pluginNames
       .map((name) => {
         return this.pluginUtils.generatePluginDefinition(name);
       })

+ 2 - 1
package.json

@@ -28,7 +28,7 @@
     "clean": "npm run clean:js && npm run clean:dll",
     "mkdirp": "mkdirp",
     "plugin:def": "node bin/generate-plugin-definitions-source.js",
-    "prebuild:dev": "npm run plugin:def",
+    "prebuild:dev": "env-cmd config/env.dev.js npm run plugin:def",
     "prebuild:prod": "npm run plugin:def",
     "prestart": "npm run build:prod",
     "server:dev:watch": "node-dev app.js --watch",
@@ -70,6 +70,7 @@
     "diff2html": "^2.3.0",
     "elasticsearch": "^12.1.3",
     "emojify.js": "^1.1.0",
+    "env-cmd": "^5.0.0",
     "errorhandler": "~1.3.4",
     "express": "~4.15.2",
     "express-form": "~0.12.0",

+ 24 - 0
yarn.lock

@@ -1548,6 +1548,14 @@ cross-spawn@^3.0.0:
     lru-cache "^4.0.1"
     which "^1.2.9"
 
+cross-spawn@^5.0.1:
+  version "5.1.0"
+  resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
+  dependencies:
+    lru-cache "^4.0.1"
+    shebang-command "^1.2.0"
+    which "^1.2.9"
+
 cryptiles@2.x.x:
   version "2.0.5"
   resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
@@ -1942,6 +1950,12 @@ enhanced-resolve@^3.0.0:
     object-assign "^4.0.1"
     tapable "^0.2.5"
 
+env-cmd@^5.0.0:
+  version "5.0.0"
+  resolved "https://registry.yarnpkg.com/env-cmd/-/env-cmd-5.0.0.tgz#c10892176a18bfcc5658cd9a2880add281de31ce"
+  dependencies:
+    cross-spawn "^5.0.1"
+
 errno@^0.1.3:
   version "0.1.4"
   resolved "https://registry.yarnpkg.com/errno/-/errno-0.1.4.tgz#b896e23a9e5e8ba33871fc996abd3635fc9a1c7d"
@@ -5076,6 +5090,16 @@ shallow-clone@^0.1.2:
     lazy-cache "^0.2.3"
     mixin-object "^2.0.1"
 
+shebang-command@^1.2.0:
+  version "1.2.0"
+  resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"
+  dependencies:
+    shebang-regex "^1.0.0"
+
+shebang-regex@^1.0.0:
+  version "1.0.0"
+  resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
+
 shellwords@^0.1.0:
   version "0.1.0"
   resolved "https://registry.yarnpkg.com/shellwords/-/shellwords-0.1.0.tgz#66afd47b6a12932d9071cbfd98a52e785cd0ba14"