2
0

plugin-utils.js 1.5 KB

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