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

improve project structure for development plugins

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

+ 4 - 2
bin/generate-plugin-definitions-source.js

@@ -4,7 +4,7 @@
  * @author Yuki Takei <yuki@weseek.co.jp>
  */
 const fs = require('graceful-fs');
-const slash = require('slash');
+const normalize = require('normalize-path');
 const swig = require('swig');
 const helpers = require('../config/helpers');
 
@@ -14,8 +14,10 @@ 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());
+// add from PLUGIN_NAMES_TOBE_LOADED when development
 if (process.env.NODE_ENV === 'development'
     && process.env.PLUGIN_NAMES_TOBE_LOADED !== undefined
     && process.env.PLUGIN_NAMES_TOBE_LOADED.length > 0) {
@@ -38,7 +40,7 @@ const definitions = pluginNames
   .map((definition) => {
     // convert backslash to slash
     definition.entries = definition.entries.map((entryPath) => {
-      return slash(entryPath);
+      return normalize(entryPath);
     });
     return definition;
   });

+ 40 - 0
lib/plugins/plugin-utils-v2.js

@@ -0,0 +1,40 @@
+const path = require('path');
+
+class PluginUtilsV2 {
+
+  /**
+   * return a definition objects that has following structure:
+   *
+   * {
+   *   name: 'crowi-plugin-X',
+   *   meta: require('crowi-plugin-X'),
+   *   entries: [
+   *     'crowi-plugin-X/lib/client-entry'
+   *   ]
+   * }
+   *
+   *
+   * @param {string} pluginName
+   * @return
+   * @memberOf PluginService
+   */
+  generatePluginDefinition(name, isForClient = false) {
+    const meta = require(name);
+    let entries = (isForClient) ? meta.clientEntries : meta.serverEntries;
+
+    entries = entries.map((entryPath) => {
+      const moduleRoot = path.resolve(require.resolve(`${name}/package.json`), '..');
+      const entryRelativePath = path.relative(moduleRoot, entryPath);
+      return path.join(name, entryRelativePath);
+    });
+
+    return {
+      name,
+      meta,
+      entries,
+    }
+  }
+
+}
+
+module.exports = PluginUtilsV2

+ 17 - 6
lib/plugins/plugin-utils.js

@@ -1,6 +1,10 @@
 const path = require('path');
 const fs = require('graceful-fs');
 
+const PluginUtilsV2 = require('./plugin-utils-v2');
+
+const pluginUtilsV2 = new PluginUtilsV2();
+
 class PluginUtils {
 
   /**
@@ -14,20 +18,27 @@ class PluginUtils {
    *   ]
    * }
    *
-   *
    * @param {string} pluginName
    * @return
    * @memberOf PluginService
    */
   generatePluginDefinition(name, isForClient = false) {
     const meta = require(name);
-    const entries = (isForClient) ? meta.clientEntries : meta.serverEntries;
+    let definition;
 
-    return {
-      name,
-      meta,
-      entries,
+    switch (meta.pluginSchemaVersion) {
+      // v1 is deprecated
+      case 1:
+        console.log('pluginSchemaVersion 1 is deprecated');
+        debug('pluginSchemaVersion 1 is deprecated');
+        break;
+      // v2 or above
+      case 2:
+      default:
+        definition = pluginUtilsV2.generatePluginDefinition(name, isForClient);
     }
+
+    return definition;
   }
 
   /**

+ 13 - 13
lib/plugins/plugin.service.js

@@ -31,21 +31,21 @@ class PluginService {
   loadPlugin(definition) {
     const meta = definition.meta;
 
-    // v1 is deprecated
-    if (1 === meta.pluginSchemaVersion) {
-      debug('pluginSchemaVersion 1 is deprecated');
-      return;
+    switch (meta.pluginSchemaVersion) {
+      // v1 is deprecated
+      case 1:
+        console.warn('pluginSchemaVersion 1 is deprecated');
+        debug('pluginSchemaVersion 1 is deprecated');
+        break;
+      // v2 or above
+      default:
+        debug(`load plugin '${definition.name}'`);
+        definition.entries.forEach((entryPath) => {
+          const entry = require(entryPath);
+          entry(this.crowi, this.app);
+        });
     }
 
-    // v2
-    if (2 === meta.pluginSchemaVersion) {
-      debug(`load plugin '${definition.name}'`);
-
-      definition.entries.forEach((entryPath) => {
-        const entry = require(entryPath);
-        entry(this.crowi, this.app);
-      });
-    }
   }
 }
 

+ 2 - 1
package.json

@@ -97,6 +97,7 @@
     "node-sass": "^4.5.0",
     "nodemailer": "~2.7.0",
     "nodemailer-ses-transport": "~1.5.0",
+    "normalize-path": "^2.1.1",
     "optimize-js-plugin": "0.0.4",
     "react": "^15.4.2",
     "react-dom": "^15.4.2",
@@ -104,7 +105,6 @@
     "reveal.js": "~3.4.0",
     "rimraf": "^2.6.1",
     "sass-loader": "^6.0.3",
-    "slash": "^1.0.0",
     "socket.io": "~1.7.0",
     "socket.io-client": "~1.7.0",
     "sprintf": "~0.1.5",
@@ -118,6 +118,7 @@
   "devDependencies": {
     "chai": "^3.5.0",
     "concurrently": "^3.4.0",
+    "crowi-pluginkit": "^1.1.0",
     "easy-livereload": "^1.2.0",
     "mocha": "^3.2.0",
     "node-dev": "^3.1.3",

+ 7 - 5
resource/js/plugin.js

@@ -22,17 +22,19 @@ export default class CrowiPlugin {
 
     definitions.forEach((definition) => {
       const meta = definition.meta;
-      const entries = definition.entries;
 
+      switch (meta.pluginSchemaVersion) {
       // v1 is deprecated
-
-      // v2
-      if (2 === meta.pluginSchemaVersion) {
-        entries.forEach((entry) => {
+      case 1:
+        break;
+      // v2 or above
+      default:
+        definition.entries.forEach((entry) => {
           entry(crowi, crowiRenderer);
         });
       }
     });
+
   }
 
 }

+ 32 - 28
yarn.lock

@@ -138,8 +138,8 @@ arr-diff@^2.0.0:
     arr-flatten "^1.0.1"
 
 arr-flatten@^1.0.1:
-  version "1.0.1"
-  resolved "https://registry.yarnpkg.com/arr-flatten/-/arr-flatten-1.0.1.tgz#e5ffe54d45e19f32f216e91eb99c8ce892bb604b"
+  version "1.0.2"
+  resolved "https://registry.yarnpkg.com/arr-flatten/-/arr-flatten-1.0.2.tgz#1ec1e63439c54f67d6f72bb4299c3d4f73b2d996"
 
 array-back@^1.0.2, array-back@^1.0.3, array-back@^1.0.4:
   version "1.0.4"
@@ -1095,8 +1095,8 @@ caniuse-api@^1.5.2:
     lodash.uniq "^4.5.0"
 
 caniuse-db@^1.0.30000529, caniuse-db@^1.0.30000634, caniuse-db@^1.0.30000639:
-  version "1.0.30000649"
-  resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000649.tgz#1ee1754a6df235450c8b7cd15e0ebf507221a86a"
+  version "1.0.30000655"
+  resolved "https://registry.yarnpkg.com/caniuse-db/-/caniuse-db-1.0.30000655.tgz#e40b6287adc938848d6708ef83d65b5f54ac1874"
 
 cardinal@^1.0.0:
   version "1.0.0"
@@ -1556,6 +1556,10 @@ cross-spawn@^5.0.1:
     shebang-command "^1.2.0"
     which "^1.2.9"
 
+crowi-pluginkit@^1.1.0:
+  version "1.1.0"
+  resolved "https://registry.yarnpkg.com/crowi-pluginkit/-/crowi-pluginkit-1.1.0.tgz#c423f812a1d5198f57ba0180230b6be53120595d"
+
 cryptiles@2.x.x:
   version "2.0.5"
   resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
@@ -1683,8 +1687,8 @@ dashdash@^1.12.0:
     assert-plus "^1.0.0"
 
 date-fns@^1.23.0:
-  version "1.28.2"
-  resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.28.2.tgz#19e4192d68875c0bf7c9537e3f296a8ec64853ef"
+  version "1.28.3"
+  resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.28.3.tgz#145d87adc3f5a82c6bda668de97eee1132c97ea1"
 
 date-now@1.0.1:
   version "1.0.1"
@@ -2635,8 +2639,8 @@ hooks-fixed@1.2.0:
   resolved "https://registry.yarnpkg.com/hooks-fixed/-/hooks-fixed-1.2.0.tgz#0d2772d4d7d685ff9244724a9f0b5b2559aac96b"
 
 hosted-git-info@^2.1.4:
-  version "2.4.1"
-  resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.4.1.tgz#4b0445e41c004a8bd1337773a4ff790ca40318c8"
+  version "2.4.2"
+  resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.4.2.tgz#0076b9f46a270506ddbaaea56496897460612a67"
 
 html-comment-regex@^1.1.0:
   version "1.1.1"
@@ -2698,8 +2702,8 @@ https-proxy-agent@^1.0.0:
     extend "3"
 
 i18next-express-middleware@~1.0.2:
-  version "1.0.3"
-  resolved "https://registry.yarnpkg.com/i18next-express-middleware/-/i18next-express-middleware-1.0.3.tgz#46e080c6589503ce55d7bbfb68ff717727716c93"
+  version "1.0.4"
+  resolved "https://registry.yarnpkg.com/i18next-express-middleware/-/i18next-express-middleware-1.0.4.tgz#e17c1ea58d1e5719d1f9a2375ec16fec0a69ec38"
   dependencies:
     cookies "0.6.1"
 
@@ -3713,8 +3717,8 @@ mustache@~2.2.1:
   resolved "https://registry.yarnpkg.com/mustache/-/mustache-2.2.1.tgz#2c40ca21c278f53150682bcf9090e41a3339b876"
 
 nan@^2.3.0, nan@^2.3.2:
-  version "2.6.1"
-  resolved "https://registry.yarnpkg.com/nan/-/nan-2.6.1.tgz#8c84f7b14c96b89f57fbc838012180ec8ca39a01"
+  version "2.6.2"
+  resolved "https://registry.yarnpkg.com/nan/-/nan-2.6.2.tgz#e4ff34e6c95fdfb5aecc08de6596f43605a7db45"
 
 native-promise-only@^0.8.1:
   version "0.8.1"
@@ -3948,7 +3952,7 @@ normalize-package-data@^2.3.2, normalize-package-data@^2.3.4:
     semver "2 || 3 || 4 || 5"
     validate-npm-package-license "^3.0.1"
 
-normalize-path@^2.0.1:
+normalize-path@^2.0.1, normalize-path@^2.1.1:
   version "2.1.1"
   resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-2.1.1.tgz#1ab28b556e198363a8c1a6f7e6fa20137fe6aed9"
   dependencies:
@@ -4461,8 +4465,8 @@ postcss-zindex@^2.0.1:
     uniqs "^2.0.0"
 
 postcss@^5.0.10, postcss@^5.0.11, postcss@^5.0.12, postcss@^5.0.13, postcss@^5.0.14, postcss@^5.0.16, postcss@^5.0.2, postcss@^5.0.4, postcss@^5.0.5, postcss@^5.0.6, postcss@^5.0.8, postcss@^5.2.16:
-  version "5.2.16"
-  resolved "https://registry.yarnpkg.com/postcss/-/postcss-5.2.16.tgz#732b3100000f9ff8379a48a53839ed097376ad57"
+  version "5.2.17"
+  resolved "https://registry.yarnpkg.com/postcss/-/postcss-5.2.17.tgz#cf4f597b864d65c8a492b2eabe9d706c879c388b"
   dependencies:
     chalk "^1.1.3"
     js-base64 "^2.1.9"
@@ -4495,9 +4499,9 @@ promise@^7.1.1:
   dependencies:
     asap "~2.0.3"
 
-prop-types@^15.5.2, prop-types@~15.5.0:
-  version "15.5.6"
-  resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.5.6.tgz#797a915b1714b645ebb7c5d6cc690346205bd2aa"
+prop-types@^15.5.7, prop-types@~15.5.7:
+  version "15.5.8"
+  resolved "https://registry.yarnpkg.com/prop-types/-/prop-types-15.5.8.tgz#6b7b2e141083be38c8595aa51fc55775c7199394"
   dependencies:
     fbjs "^0.8.9"
 
@@ -4610,22 +4614,22 @@ rc@^1.1.7:
     strip-json-comments "~2.0.1"
 
 react-dom@^15.4.2:
-  version "15.5.3"
-  resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-15.5.3.tgz#2ee127ce942df55da53111ae303316e68072b5c5"
+  version "15.5.4"
+  resolved "https://registry.yarnpkg.com/react-dom/-/react-dom-15.5.4.tgz#ba0c28786fd52ed7e4f2135fe0288d462aef93da"
   dependencies:
     fbjs "^0.8.9"
     loose-envify "^1.1.0"
     object-assign "^4.1.0"
-    prop-types "~15.5.0"
+    prop-types "~15.5.7"
 
 react@^15.4.2:
-  version "15.5.3"
-  resolved "https://registry.yarnpkg.com/react/-/react-15.5.3.tgz#84055382c025dec4e3b902bb61a8697cc79c1258"
+  version "15.5.4"
+  resolved "https://registry.yarnpkg.com/react/-/react-15.5.4.tgz#fa83eb01506ab237cdc1c8c3b1cea8de012bf047"
   dependencies:
     fbjs "^0.8.9"
     loose-envify "^1.1.0"
     object-assign "^4.1.0"
-    prop-types "^15.5.2"
+    prop-types "^15.5.7"
 
 read-pkg-up@^1.0.1:
   version "1.0.1"
@@ -5262,8 +5266,8 @@ sprintf@~0.1.5:
   resolved "https://registry.yarnpkg.com/sprintf/-/sprintf-0.1.5.tgz#8f83e39a9317c1a502cb7db8050e51c679f6edcf"
 
 sshpk@^1.7.0:
-  version "1.11.0"
-  resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.11.0.tgz#2d8d5ebb4a6fab28ffba37fa62a90f4a3ea59d77"
+  version "1.13.0"
+  resolved "https://registry.yarnpkg.com/sshpk/-/sshpk-1.13.0.tgz#ff2a3e4fd04497555fed97b39a0fd82fafb3a33c"
   dependencies:
     asn1 "~0.2.3"
     assert-plus "^1.0.0"
@@ -5681,8 +5685,8 @@ verror@1.3.6:
     extsprintf "1.0.2"
 
 vlq@^0.2.1:
-  version "0.2.1"
-  resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.1.tgz#14439d711891e682535467f8587c5630e4222a6c"
+  version "0.2.2"
+  resolved "https://registry.yarnpkg.com/vlq/-/vlq-0.2.2.tgz#e316d5257b40b86bb43cb8d5fea5d7f54d6b0ca1"
 
 vm-browserify@0.0.4:
   version "0.0.4"