plugin.service.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const isEnabledPlugins = this.crowi.configManager.getConfig('crowi', 'plugin:isEnabledPlugins');
  11. // import plugins
  12. if (isEnabledPlugins) {
  13. logger.debug('Plugins are enabled');
  14. this.loadPlugins(this.pluginUtils.listPluginNames(this.crowi.rootDir));
  15. }
  16. }
  17. /**
  18. * load plugins
  19. *
  20. * @memberOf PluginService
  21. */
  22. loadPlugins(pluginNames) {
  23. pluginNames
  24. .map((name) => {
  25. return this.pluginUtils.generatePluginDefinition(name);
  26. })
  27. .forEach((definition) => {
  28. this.loadPlugin(definition);
  29. });
  30. }
  31. loadPlugin(definition) {
  32. const meta = definition.meta;
  33. switch (meta.pluginSchemaVersion) {
  34. // v1 is deprecated
  35. case 1:
  36. logger.warn('pluginSchemaVersion 1 is deprecated', definition);
  37. break;
  38. // v2 is deprecated
  39. case 2:
  40. logger.warn('pluginSchemaVersion 2 is deprecated', definition);
  41. break;
  42. case 3:
  43. logger.info(`load plugin '${definition.name}'`);
  44. definition.entries.forEach((entryPath) => {
  45. const entry = require(entryPath);
  46. entry(this.crowi, this.app);
  47. });
  48. break;
  49. default:
  50. logger.warn('Unsupported schema version', meta.pluginSchemaVersion);
  51. }
  52. }
  53. }
  54. module.exports = PluginService;