plugin.service.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const logger = require('@alias/logger')('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. logger.warn('pluginSchemaVersion 1 is deprecated', definition);
  32. break;
  33. // v2 is deprecated
  34. case 2:
  35. logger.warn('pluginSchemaVersion 2 is deprecated', definition);
  36. break;
  37. case 3:
  38. logger.info(`load plugin '${definition.name}'`);
  39. definition.entries.forEach((entryPath) => {
  40. const entry = require(entryPath);
  41. entry(this.crowi, this.app);
  42. });
  43. break;
  44. default:
  45. logger.warn('Unsupported schema version', meta.pluginSchemaVersion);
  46. }
  47. }
  48. }
  49. module.exports = PluginService;