plugin-utils.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const path = require('path');
  2. const fs = require('graceful-fs');
  3. class PluginUtils {
  4. /**
  5. * return a definition objects that has following structure:
  6. *
  7. * {
  8. * name: 'crowi-plugin-X',
  9. * meta: require('crowi-plugin-X'),
  10. * entries: [
  11. * 'crowi-plugin-X/lib/client-entry'
  12. * ]
  13. * }
  14. *
  15. *
  16. * @param {string} pluginName
  17. * @return
  18. * @memberOf PluginService
  19. */
  20. generatePluginDefinition(name, isForClient = false) {
  21. const meta = require(name);
  22. const entries = (isForClient) ? meta.clientEntries : meta.serverEntries;
  23. return {
  24. name,
  25. meta,
  26. entries,
  27. }
  28. }
  29. /**
  30. * list plugin module names that starts with 'crowi-plugin-'
  31. * borrowing from: https://github.com/hexojs/hexo/blob/d1db459c92a4765620343b95789361cbbc6414c5/lib/hexo/load_plugins.js#L17
  32. *
  33. * @returns
  34. *
  35. * @memberOf PluginService
  36. */
  37. listPluginNames(rootDir) {
  38. var packagePath = path.join(rootDir, 'package.json');
  39. // Make sure package.json exists
  40. if (!fs.existsSync(packagePath)) {
  41. return [];
  42. }
  43. // Read package.json and find dependencies
  44. const content = fs.readFileSync(packagePath);
  45. const json = JSON.parse(content);
  46. const deps = json.dependencies || {};
  47. return Object.keys(deps).filter((name) => {
  48. // Ignore plugins whose name is not started with "crowi-"
  49. return /^crowi-plugin-/.test(name);
  50. });
  51. }
  52. }
  53. module.exports = PluginUtils