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

impl server side plugin loader

Yuki Takei 9 лет назад
Родитель
Сommit
c7078fdc9a
2 измененных файлов с 48 добавлено и 1 удалено
  1. 20 1
      lib/crowi/index.js
  2. 28 0
      lib/plugins/index.js

+ 20 - 1
lib/crowi/index.js

@@ -326,8 +326,15 @@ Crowi.prototype.buildServer = function() {
     ;
 
   require('./express-init')(this, app);
-  require('../routes')(this, app);
 
+  // import plugins
+  var isEnabledPlugins = true;    // TODO retrieve from config
+  if (isEnabledPlugins) {
+    require('../plugins')(this, app);
+  }
+
+  require('../routes')(this, app);
+  
   if (env == 'development') {
     //swig.setDefaults({ cache: false });
     app.use(errorHandler({ dumpExceptions: true, showStack: true }));
@@ -346,6 +353,18 @@ Crowi.prototype.buildServer = function() {
   return Promise.resolve(app);
 };
 
+/**
+ * require API for plugins
+ * 
+ * @param {string} modulePath
+ * @return {module}
+ * 
+ * @memberof Crowi
+ */
+Crowi.prototype.require = function(modulePath) {
+  return require(modulePath);
+}
+
 Crowi.prototype.exitOnError = function(err) {
   debug('Critical error occured.');
   console.error(err);

+ 28 - 0
lib/plugins/index.js

@@ -0,0 +1,28 @@
+const plugins = {
+  // 'crowi-plugin-X': {
+  //   meta: require('crowi-plugin-X'),
+  //   entries: [
+  //     require('crowi-plugin-X/lib/server-entry')
+  //   ]
+  // },
+}
+
+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);
+      });
+    }
+  }
+}