plugin-utils.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. class PluginUtils {
  7. /**
  8. * list plugin module objects
  9. * that starts with 'growi-plugin-' or 'crowi-plugin-'
  10. * borrowing from: https://github.com/hexojs/hexo/blob/d1db459c92a4765620343b95789361cbbc6414c5/lib/hexo/load_plugins.js#L17
  11. *
  12. * @returns array of objects
  13. * [
  14. * { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
  15. * { name: 'growi-plugin-...', requiredVersion: '^1.0.0', installedVersion: '1.0.0' },
  16. * ...
  17. * ]
  18. *
  19. * @memberOf PluginService
  20. */
  21. listPlugins() {
  22. const packagePath = resolveFromRoot('package.json');
  23. // Make sure package.json exists
  24. if (!fs.existsSync(packagePath)) {
  25. return [];
  26. }
  27. // Read package.json and find dependencies
  28. const content = fs.readFileSync(packagePath);
  29. const json = JSON.parse(content);
  30. const deps = json.dependencies || {};
  31. const pluginNames = Object.keys(deps).filter((name) => {
  32. return /^@growi\/plugin-/.test(name);
  33. });
  34. return pluginNames.map((name) => {
  35. return {
  36. name,
  37. requiredVersion: deps[name],
  38. installedVersion: this.getVersion(name),
  39. };
  40. });
  41. }
  42. /**
  43. * list plugin module names that starts with 'crowi-plugin-'
  44. *
  45. * @returns array of plugin names
  46. *
  47. * @memberOf PluginService
  48. */
  49. listPluginNames() {
  50. const plugins = this.listPlugins();
  51. return plugins.map((plugin) => { return plugin.name });
  52. }
  53. getVersion(packageName) {
  54. const packagePath = resolveFromRoot(`../../node_modules/${packageName}/package.json`);
  55. // Read package.json and find version
  56. const content = fs.readFileSync(packagePath);
  57. const json = JSON.parse(content);
  58. return json.version || '';
  59. }
  60. }
  61. module.exports = PluginUtils;
  62. export default PluginUtils;