plugin-utils.js 2.6 KB

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