plugin.service.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // when dev
  16. if (this.crowi.node_env === 'development') {
  17. this.autoDetectAndLoadPluginsForDev();
  18. }
  19. }
  20. }
  21. autoDetectAndLoadPluginsForDev() {
  22. if (process.env.PLUGIN_NAMES_TOBE_LOADED !== undefined
  23. && process.env.PLUGIN_NAMES_TOBE_LOADED.length > 0) {
  24. const pluginNames = process.env.PLUGIN_NAMES_TOBE_LOADED.split(',');
  25. logger.debug('[development] loading Plugins', pluginNames);
  26. // merge and remove duplicates
  27. if (pluginNames.length > 0) {
  28. this.crowi.pluginService.loadPlugins(pluginNames);
  29. }
  30. }
  31. }
  32. /**
  33. * load plugins
  34. *
  35. * @memberOf PluginService
  36. */
  37. loadPlugins(pluginNames) {
  38. pluginNames
  39. .map((name) => {
  40. return this.pluginUtils.generatePluginDefinition(name);
  41. })
  42. .forEach((definition) => {
  43. this.loadPlugin(definition);
  44. });
  45. }
  46. loadPlugin(definition) {
  47. const meta = definition.meta;
  48. switch (meta.pluginSchemaVersion) {
  49. // v1 is deprecated
  50. case 1:
  51. logger.warn('pluginSchemaVersion 1 is deprecated', definition);
  52. break;
  53. // v2 is deprecated
  54. case 2:
  55. logger.warn('pluginSchemaVersion 2 is deprecated', definition);
  56. break;
  57. case 3:
  58. logger.info(`load plugin '${definition.name}'`);
  59. definition.entries.forEach((entryPath) => {
  60. const entry = require(entryPath);
  61. entry(this.crowi, this.app);
  62. });
  63. break;
  64. default:
  65. logger.warn('Unsupported schema version', meta.pluginSchemaVersion);
  66. }
  67. }
  68. }
  69. module.exports = PluginService;