extra_api.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var through = require("through2");
  2. var gutil = require("gulp-util");
  3. var notifier = require("node-notifier");
  4. var report = require("./report");
  5. "use strict";
  6. // Default log level
  7. var logLevel = 2;
  8. // Default logger
  9. var fnLog = gutil.log;
  10. var logError = module.exports.logError = function (options, isError) {
  11. if (!logLevel) return;
  12. if (logLevel === 1 && !isError) return;
  13. color = isError ? "red" : "green";
  14. if (!gutil.colors[color]) return;
  15. fnLog(gutil.colors.cyan('gulp-notify') + ':',
  16. '[' + gutil.colors.blue(options.title) + ']',
  17. gutil.colors[color].call(gutil.colors, options.message)
  18. );
  19. };
  20. // Expose onError behaviour
  21. module.exports.onError = function (options, callback) {
  22. var reporter;
  23. options = options || {};
  24. var templateOptions = options.templateOptions || {};
  25. var callback = callback || function (err) {
  26. err && logError({
  27. title: "Error running notifier",
  28. message: "Could not send message: " + err.message
  29. }, true);
  30. };
  31. if (options.notifier) {
  32. reporter = options.notifier;
  33. } else {
  34. reporter = notifier.notify.bind(notifier);
  35. }
  36. return function (error) {
  37. var self = this;
  38. report(reporter, error, options, templateOptions, function () {
  39. callback.apply(self, arguments);
  40. self.emit && self.emit('end');
  41. });
  42. };
  43. };
  44. // Expose to set log level
  45. module.exports.logLevel = function (newLogLevel) {
  46. if (newLogLevel === void 0) return logLevel;
  47. logLevel = newLogLevel;
  48. };
  49. // Expose to set new logger
  50. module.exports.logger = function (newLogger) {
  51. if (!newLogger) return fnLog;
  52. fnLog = newLogger;
  53. };