hooks.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. "use strict";
  2. var _ = require("./lodash.custom");
  3. var Immutable = require("immutable");
  4. var snippetUtils = require("./snippet").utils;
  5. module.exports = {
  6. /**
  7. *
  8. * @this {BrowserSync}
  9. * @returns {String}
  10. */
  11. "client:js": function (hooks, data) {
  12. var js = snippetUtils.getClientJs(data.port, data.options);
  13. return hooks.reduce(function (acc, hook) {
  14. if (typeof hook === "function") {
  15. return acc.concat(hook);
  16. }
  17. return acc.concat(String(hook));
  18. }, [js]);
  19. },
  20. /**
  21. * @this {BrowserSync}
  22. * @returns {Array}
  23. */
  24. "client:events": function (hooks, clientEvents) {
  25. hooks.forEach(function (hook) {
  26. var result = hook(this);
  27. if (Array.isArray(result)) {
  28. clientEvents = _.union(clientEvents, result);
  29. }
  30. else {
  31. clientEvents.push(result);
  32. }
  33. }, this);
  34. return clientEvents;
  35. },
  36. /**
  37. * @returns {Array}
  38. */
  39. "server:middleware": function (hooks, initial) {
  40. initial = initial || [];
  41. _.each(hooks, function (hook) {
  42. var result = hook(this);
  43. if (Array.isArray(result)) {
  44. result.forEach(function (res) {
  45. if (_.isFunction(res)) {
  46. initial = initial.push(res);
  47. }
  48. });
  49. }
  50. else {
  51. if (_.isFunction(result)) {
  52. initial = initial.push(result);
  53. }
  54. }
  55. }, this);
  56. return initial;
  57. },
  58. /**
  59. * @param {Array} hooks
  60. * @param {Map|List} initial
  61. * @param pluginOptions
  62. * @returns {any}
  63. */
  64. "files:watch": function (hooks, initial, pluginOptions) {
  65. var opts;
  66. if (pluginOptions) {
  67. opts = Immutable.fromJS(pluginOptions);
  68. opts.forEach(function (value, key) {
  69. if (!value) {
  70. return;
  71. }
  72. var files = value.get("files");
  73. if (files) {
  74. var fileArg = require("./cli/cli-options").makeFilesArg(files);
  75. if (fileArg) {
  76. initial = initial.set(key, Immutable.fromJS(fileArg));
  77. }
  78. }
  79. });
  80. }
  81. return initial;
  82. }
  83. };
  84. //# sourceMappingURL=hooks.js.map