report.js 3.4 KB

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