plugin.service.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import loggerFactory from '~/utils/logger';
  2. const PluginUtils = require('./plugin-utils');
  3. const logger = loggerFactory('growi:plugins:PluginService');
  4. class PluginService {
  5. constructor(crowi, app) {
  6. this.crowi = crowi;
  7. this.app = app;
  8. this.pluginUtils = new PluginUtils();
  9. }
  10. async autoDetectAndLoadPlugins() {
  11. const isEnabledPlugins = this.crowi.configManager.getConfig('crowi', 'plugin:isEnabledPlugins');
  12. // import plugins
  13. if (isEnabledPlugins) {
  14. logger.debug('Plugins are enabled');
  15. return this.loadPlugins(this.pluginUtils.listPluginNames(this.crowi.rootDir));
  16. }
  17. }
  18. /**
  19. * load plugins
  20. *
  21. * @memberOf PluginService
  22. */
  23. async loadPlugins(pluginNames) {
  24. // get definitions
  25. const definitions = [];
  26. for (const pluginName of pluginNames) {
  27. // eslint-disable-next-line no-await-in-loop
  28. const definition = await this.pluginUtils.generatePluginDefinition(pluginName);
  29. if (definition != null) {
  30. this.loadPlugin(definition);
  31. }
  32. }
  33. }
  34. loadPlugin(definition) {
  35. const meta = definition.meta;
  36. switch (meta.pluginSchemaVersion) {
  37. // v1, v2 and v3 is deprecated
  38. case 1:
  39. logger.warn('pluginSchemaVersion 1 is deprecated', definition);
  40. break;
  41. case 2:
  42. logger.warn('pluginSchemaVersion 2 is deprecated', definition);
  43. break;
  44. case 3:
  45. logger.warn('pluginSchemaVersion 3 is deprecated', definition);
  46. break;
  47. // v4 or above
  48. case 4:
  49. logger.info(`load plugin '${definition.name}'`);
  50. definition.entries.forEach((entryPath) => {
  51. const entry = require(entryPath);
  52. entry(this.crowi, this.app);
  53. });
  54. break;
  55. default:
  56. logger.warn('Unsupported schema version', meta.pluginSchemaVersion);
  57. }
  58. }
  59. }
  60. module.exports = PluginService;