2
0

plugin.service.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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');
  32. break;
  33. // v2 or above
  34. default:
  35. logger.info(`load plugin '${definition.name}'`);
  36. definition.entries.forEach((entryPath) => {
  37. const entry = require(entryPath);
  38. entry(this.crowi, this.app);
  39. });
  40. }
  41. }
  42. }
  43. module.exports = PluginService;