plugin.service.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const debug = require('debug')('growi:plugins:PluginService');
  2. const PluginUtils = require('./plugin-utils');
  3. class PluginService {
  4. constructor(crowi, app) {
  5. this.crowi = crowi;
  6. this.app = app;
  7. this.pluginUtils = new PluginUtils();
  8. }
  9. autoDetectAndLoadPlugins() {
  10. this.loadPlugins(this.pluginUtils.listPluginNames(this.crowi.rootDir));
  11. }
  12. /**
  13. * load plugins
  14. *
  15. * @memberOf PluginService
  16. */
  17. loadPlugins(pluginNames) {
  18. pluginNames
  19. .map((name) => {
  20. return this.pluginUtils.generatePluginDefinition(name);
  21. })
  22. .forEach((definition) => {
  23. this.loadPlugin(definition);
  24. });
  25. }
  26. loadPlugin(definition) {
  27. const meta = definition.meta;
  28. switch (meta.pluginSchemaVersion) {
  29. // v1 is deprecated
  30. case 1:
  31. console.warn('pluginSchemaVersion 1 is deprecated');
  32. debug('pluginSchemaVersion 1 is deprecated');
  33. break;
  34. // v2 or above
  35. default:
  36. debug(`load plugin '${definition.name}'`);
  37. definition.entries.forEach((entryPath) => {
  38. const entry = require(entryPath);
  39. entry(this.crowi, this.app);
  40. });
  41. }
  42. }
  43. }
  44. module.exports = PluginService;