report.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. var template = require("lodash.template");
  2. var PluginError = require('plugin-error');
  3. var api = require("./extra_api");
  4. var extend = require("node.extend");
  5. var path = require("path");
  6. "use strict";
  7. var defaults = {
  8. error: {
  9. icon: path.join(__dirname, '..', 'assets', 'gulp-error.png'),
  10. sound: 'Frog'
  11. },
  12. regular: {
  13. icon: path.join(__dirname, '..', 'assets', 'gulp.png')
  14. }
  15. };
  16. module.exports = function (reporter, message, options, templateOptions, callback) {
  17. var self = this;
  18. callback = callback || function () {};
  19. if (!reporter) return callback(new PluginError("gulp-notify", "No reporter specified."));
  20. // Try/catch the only way to go to ensure catching all errors? Domains?
  21. try {
  22. var options = constructOptions(options, message, templateOptions);
  23. if (!options) {
  24. return callback();
  25. }
  26. api.logError(options, (message instanceof Error));
  27. reporter(options, function (err) {
  28. if (err) return callback(new PluginError("gulp-notify", err));
  29. return callback();
  30. });
  31. } catch (err) {
  32. return callback(new PluginError("gulp-notify", err));
  33. }
  34. };
  35. function generate (outputData, object, title, message, subtitle, open, templateOptions) {
  36. if (object instanceof Error) {
  37. var titleTemplate = template(title);
  38. var messageTemplate = template(message);
  39. var openTemplate = template(open);
  40. var subtitleTemplate = template(subtitle);
  41. return extend(defaults.error, outputData, {
  42. title: titleTemplate({
  43. error: object,
  44. options: templateOptions
  45. }),
  46. message: messageTemplate({
  47. error: object,
  48. options: templateOptions
  49. }),
  50. open: openTemplate({
  51. error: object,
  52. options: templateOptions
  53. }),
  54. subtitle: subtitleTemplate({
  55. error: object,
  56. options: templateOptions
  57. })
  58. });
  59. }
  60. return extend(defaults.regular, outputData, {
  61. title: template(title)({
  62. file: object,
  63. options: templateOptions
  64. }),
  65. message: template(message)({
  66. file: object,
  67. options: templateOptions
  68. })
  69. });
  70. }
  71. function constructOptions (options, object, templateOptions) {
  72. var message = object.path || object.message || object,
  73. title = !(object instanceof Error) ? "Gulp notification" : "Error running Gulp",
  74. open = "",
  75. subtitle = "",
  76. outputData = {};
  77. if (typeof options === "function") {
  78. message = options(object);
  79. if (typeof message === "object") {
  80. options = message;
  81. }
  82. if (!message) {
  83. return false;
  84. }
  85. }
  86. if (typeof options === "string") {
  87. message = options;
  88. }
  89. if (typeof options === "object") {
  90. outputData = extend(true, {}, options);
  91. if (typeof outputData.title === "function") {
  92. title = outputData.title(object);
  93. } else {
  94. title = outputData.title || title;
  95. }
  96. if (typeof outputData.subtitle === "function") {
  97. subtitle = outputData.subtitle(object);
  98. } else {
  99. subtitle = outputData.subtitle || subtitle;
  100. }
  101. if (typeof outputData.open === "function") {
  102. open = outputData.open(object);
  103. } else {
  104. open = outputData.open || open;
  105. }
  106. if (typeof outputData.message === "function") {
  107. message = outputData.message(object);
  108. if (!message) {
  109. return false;
  110. }
  111. } else {
  112. message = outputData.message || message;
  113. }
  114. }
  115. return generate(outputData, object, title, message, subtitle, open, templateOptions);
  116. }