plugin-utils.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const path = require('path');
  2. const fs = require('hexo-fs');
  3. class PluginUtils {
  4. /**
  5. * return following structure for client:
  6. *
  7. * [
  8. * 'crowi-plugin-X': {
  9. * meta: require('crowi-plugin-X'),
  10. * entries: [
  11. * require('crowi-plugin-X/lib/client-entry')
  12. * ]
  13. * },
  14. * ]
  15. *
  16. * usage:
  17. * 1. define at webpack.DefinePlugin
  18. * 2. retrieve from resource/js/plugin.js
  19. *
  20. * @return
  21. * @memberOf PluginService
  22. */
  23. generatePluginDefinitions() {
  24. // TODO impl
  25. }
  26. /**
  27. * list plugin module names that starts with 'crowi-plugin-'
  28. * borrowing from: https://github.com/hexojs/hexo/blob/d1db459c92a4765620343b95789361cbbc6414c5/lib/hexo/load_plugins.js#L17
  29. *
  30. * @returns
  31. *
  32. * @memberOf PluginService
  33. */
  34. listPluginNames(rootDir) {
  35. var packagePath = path.join(rootDir, 'package.json');
  36. // Make sure package.json exists
  37. return fs.exists(packagePath).then(function(exist) {
  38. if (!exist) return [];
  39. // Read package.json and find dependencies
  40. return fs.readFile(packagePath).then(function(content) {
  41. var json = JSON.parse(content);
  42. var deps = json.dependencies || {};
  43. return Object.keys(deps);
  44. });
  45. }).filter(function(name) {
  46. // Ignore plugins whose name is not started with "crowi-"
  47. return /^crowi-plugin-/.test(name);
  48. });
  49. }
  50. }
  51. module.exports = PluginUtils