plugin-utils.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. const path = require('path');
  2. const fs = require('graceful-fs');
  3. const logger = require('@alias/logger')('growi:plugins:plugin-utils');
  4. const packageInstalledVersionSync = require('package-installed-version-sync');
  5. const PluginUtilsV2 = require('./plugin-utils-v2');
  6. const pluginUtilsV2 = new PluginUtilsV2();
  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. generatePluginDefinition(name, isForClient = false) {
  24. const meta = require(name);
  25. let definition;
  26. switch (meta.pluginSchemaVersion) {
  27. // v1 is deprecated
  28. case 1:
  29. logger.debug('pluginSchemaVersion 1 is deprecated');
  30. break;
  31. // v2 or above
  32. case 2:
  33. default:
  34. definition = pluginUtilsV2.generatePluginDefinition(name, isForClient);
  35. }
  36. return definition;
  37. }
  38. /**
  39. * list plugin module objects
  40. * that starts with 'growi-plugin-' or 'crowi-plugin-'
  41. * borrowing from: https://github.com/hexojs/hexo/blob/d1db459c92a4765620343b95789361cbbc6414c5/lib/hexo/load_plugins.js#L17
  42. *
  43. * @returns array of objects
  44. * [
  45. * { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
  46. * { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
  47. * ...
  48. * ]
  49. *
  50. * @memberOf PluginService
  51. */
  52. listPlugins(rootDir) {
  53. const packagePath = path.join(rootDir, 'package.json');
  54. // Make sure package.json exists
  55. if (!fs.existsSync(packagePath)) {
  56. return [];
  57. }
  58. // Read package.json and find dependencies
  59. const content = fs.readFileSync(packagePath);
  60. const json = JSON.parse(content);
  61. const deps = json.dependencies || {};
  62. const pluginNames = Object.keys(deps).filter((name) => {
  63. return /^(crowi|growi)-plugin-/.test(name);
  64. });
  65. const plugins = pluginNames.map((name) => {
  66. return {
  67. name,
  68. requiredVersion: deps[name],
  69. installedVersion: packageInstalledVersionSync(name),
  70. };
  71. });
  72. return plugins;
  73. }
  74. /**
  75. * list plugin module names that starts with 'crowi-plugin-'
  76. *
  77. * @returns array of plugin names
  78. *
  79. * @memberOf PluginService
  80. */
  81. listPluginNames(rootDir) {
  82. const plugins = this.listPlugins(rootDir);
  83. return plugins.map((plugin) => { return plugin.name });
  84. }
  85. }
  86. module.exports = PluginUtils;