plugin-utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import loggerFactory from '~/utils/logger';
  2. import { resolveFromRoot } from '~/utils/project-dir-utils';
  3. import { PluginUtilsV4 } from './plugin-utils-v4';
  4. const fs = require('graceful-fs');
  5. const logger = loggerFactory('growi:plugins:plugin-utils');
  6. const pluginUtilsV4 = new PluginUtilsV4();
  7. class PluginUtils {
  8. /**
  9. * return a definition objects that has following structure:
  10. *
  11. * {
  12. * name: 'growi-plugin-X',
  13. * meta: require('growi-plugin-X'),
  14. * entries: [
  15. * 'growi-plugin-X/lib/client-entry'
  16. * ]
  17. * }
  18. *
  19. * @param {string} pluginName
  20. * @return
  21. * @memberOf PluginService
  22. */
  23. async generatePluginDefinition(name, isForClient = false) {
  24. const meta = require(name);
  25. let definition;
  26. switch (meta.pluginSchemaVersion) {
  27. // v1, v2 and v3 is deprecated
  28. case 1:
  29. logger.debug('pluginSchemaVersion 1 is deprecated');
  30. break;
  31. case 2:
  32. logger.debug('pluginSchemaVersion 2 is deprecated');
  33. break;
  34. case 3:
  35. logger.debug('pluginSchemaVersion 3 is deprecated');
  36. break;
  37. // v4 or above
  38. case 4:
  39. definition = await pluginUtilsV4.generatePluginDefinition(name, isForClient);
  40. break;
  41. default:
  42. logger.warn('Unsupported schema version', meta.pluginSchemaVersion);
  43. }
  44. return definition;
  45. }
  46. /**
  47. * list plugin module objects
  48. * that starts with 'growi-plugin-' or 'crowi-plugin-'
  49. * borrowing from: https://github.com/hexojs/hexo/blob/d1db459c92a4765620343b95789361cbbc6414c5/lib/hexo/load_plugins.js#L17
  50. *
  51. * @returns array of objects
  52. * [
  53. * { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
  54. * { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
  55. * ...
  56. * ]
  57. *
  58. * @memberOf PluginService
  59. */
  60. listPlugins() {
  61. const packagePath = resolveFromRoot('package.json');
  62. // Make sure package.json exists
  63. if (!fs.existsSync(packagePath)) {
  64. return [];
  65. }
  66. // Read package.json and find dependencies
  67. const content = fs.readFileSync(packagePath);
  68. const json = JSON.parse(content);
  69. const deps = json.dependencies || {};
  70. const pluginNames = Object.keys(deps).filter((name) => {
  71. return /^@growi\/plugin-/.test(name);
  72. });
  73. return pluginNames.map((name) => {
  74. return {
  75. name,
  76. requiredVersion: deps[name],
  77. installedVersion: this.getVersion(name),
  78. };
  79. });
  80. }
  81. /**
  82. * list plugin module names that starts with 'crowi-plugin-'
  83. *
  84. * @returns array of plugin names
  85. *
  86. * @memberOf PluginService
  87. */
  88. listPluginNames() {
  89. const plugins = this.listPlugins();
  90. return plugins.map((plugin) => { return plugin.name });
  91. }
  92. getVersion(packageName) {
  93. const packagePath = resolveFromRoot(`../../node_modules/${packageName}/package.json`);
  94. // Read package.json and find version
  95. const content = fs.readFileSync(packagePath);
  96. const json = JSON.parse(content);
  97. return json.version || '';
  98. }
  99. }
  100. module.exports = PluginUtils;
  101. export default PluginUtils;