resolve-plugins.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. var fs = require("fs");
  2. var path = require("path");
  3. var Immutable = require("immutable");
  4. /**
  5. * Take Browsersync plugins and determine if
  6. * any UI is provided by looking at data in the the
  7. * modules package.json file
  8. * @param plugins
  9. * @returns {*}
  10. */
  11. module.exports = function (plugins) {
  12. return require("immutable")
  13. .fromJS(plugins)
  14. /**
  15. * Exclude the UI
  16. */
  17. .filter(function (plugin) {
  18. return plugin.get("name") !== "UI";
  19. })
  20. /**
  21. * Attempt to retrieve a plugins package.json file
  22. */
  23. .map(function (plugin) {
  24. var moduleName = plugin.getIn(["opts", "moduleName"]);
  25. var pkg = {};
  26. if (!moduleName) {
  27. return plugin;
  28. }
  29. try {
  30. pkg = require("immutable").fromJS(require(path.join(moduleName, "package.json")));
  31. } catch (e) {
  32. console.error(e);
  33. return plugin;
  34. }
  35. plugin = plugin.set("pkg", pkg);
  36. return plugin.set("relpath", path.dirname(require.resolve(moduleName)));
  37. })
  38. /**
  39. * Try to load markup for each plugin
  40. */
  41. .map(function (plugin) {
  42. if (!plugin.hasIn(["pkg", "browser-sync:ui"])) {
  43. return plugin;
  44. }
  45. var markup = plugin.getIn(["pkg", "browser-sync:ui", "hooks", "markup"]);
  46. if (markup) {
  47. plugin = plugin.set("markup", fs.readFileSync(path.resolve(plugin.get("relpath"), markup), "utf8"));
  48. }
  49. return plugin;
  50. })
  51. /**
  52. * Load any template files for the plugin
  53. */
  54. .map(function (plugin) {
  55. if (!plugin.hasIn(["pkg", "browser-sync:ui"])) {
  56. return plugin;
  57. }
  58. return resolveIfPluginHas(["pkg", "browser-sync:ui", "hooks", "templates"], "templates", plugin);
  59. })
  60. /**
  61. * Try to load Client JS for each plugin
  62. */
  63. .map(function (plugin) {
  64. if (!plugin.hasIn(["pkg", "browser-sync:ui"])) {
  65. return plugin;
  66. }
  67. return resolveIfPluginHas(["pkg", "browser-sync:ui", "hooks", "client:js"], "client:js", plugin);
  68. });
  69. };
  70. /**
  71. * If a plugin contains this option path, resolve/read the files
  72. * @param {Array} optPath - How to access the collection
  73. * @param {String} propName - Key for property access
  74. * @param {Immutable.Map} plugin
  75. * @returns {*}
  76. */
  77. function resolveIfPluginHas(optPath, propName, plugin) {
  78. var opt = plugin.getIn(optPath);
  79. if (opt.size) {
  80. return plugin.set(
  81. propName,
  82. resolvePluginFiles(opt, plugin.get("relpath"))
  83. );
  84. }
  85. return plugin;
  86. }
  87. /**
  88. * Read & store a file from a plugin
  89. * @param {Array|Immutable.List} collection
  90. * @param {String} relPath
  91. * @returns {any}
  92. */
  93. function resolvePluginFiles (collection, relPath) {
  94. return Immutable.fromJS(collection.reduce(function (all, item) {
  95. var full = path.join(relPath, item);
  96. if (fs.existsSync(full)) {
  97. all[full] = fs.readFileSync(full, "utf8");
  98. }
  99. return all;
  100. }, {}));
  101. }