123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- var template = require("lodash.template");
- var PluginError = require('plugin-error');
- var api = require("./extra_api");
- var extend = require("node.extend");
- var path = require("path");
- "use strict";
- var defaults = {
- error: {
- icon: path.join(__dirname, '..', 'assets', 'gulp-error.png'),
- sound: 'Frog'
- },
- regular: {
- icon: path.join(__dirname, '..', 'assets', 'gulp.png')
- }
- };
- module.exports = function (reporter, message, options, templateOptions, callback) {
- callback = callback || function () {};
- if (!reporter) return callback(new PluginError("gulp-notify", "No reporter specified."));
- // Try/catch the only way to go to ensure catching all errors? Domains?
- try {
- options = constructOptions(options, message, templateOptions);
- if (!options) {
- return callback();
- }
- api.logError(options, (message instanceof Error));
- reporter(options, function (err) {
- if (err) return callback(new PluginError("gulp-notify", err));
- return callback();
- });
- } catch (err) {
- return callback(new PluginError("gulp-notify", err));
- }
- };
- function generate (outputData, object, title, message, subtitle, open, templateOptions) {
- if (object instanceof Error) {
- var titleTemplate = template(title);
- var messageTemplate = template(message);
- var openTemplate = template(open);
- var subtitleTemplate = template(subtitle);
- return extend(defaults.error, outputData, {
- title: titleTemplate({
- error: object,
- options: templateOptions
- }),
- message: messageTemplate({
- error: object,
- options: templateOptions
- }),
- open: openTemplate({
- error: object,
- options: templateOptions
- }),
- subtitle: subtitleTemplate({
- error: object,
- options: templateOptions
- })
- });
- }
- return extend(defaults.regular, outputData, {
- title: template(title)({
- file: object,
- options: templateOptions
- }),
- message: template(message)({
- file: object,
- options: templateOptions
- })
- });
- }
- function constructOptions (options, object, templateOptions) {
- var message = object.path || object.message || object,
- title = !(object instanceof Error) ? "Gulp notification" : "Error running Gulp",
- open = "",
- subtitle = "",
- outputData = {};
- if (typeof options === "function") {
- message = options(object);
- if (typeof message === "object") {
- options = message;
- }
- if (!message) {
- return false;
- }
- }
- if (typeof options === "string") {
- message = options;
- }
- if (typeof options === "object") {
- outputData = extend(true, {}, options);
- if (typeof outputData.title === "function") {
- title = outputData.title(object);
- } else {
- title = outputData.title || title;
- }
- if (typeof outputData.subtitle === "function") {
- subtitle = outputData.subtitle(object);
- } else {
- subtitle = outputData.subtitle || subtitle;
- }
- if (typeof outputData.open === "function") {
- open = outputData.open(object);
- } else {
- open = outputData.open || open;
- }
- if (typeof outputData.message === "function") {
- message = outputData.message(object);
- if (!message) {
- return false;
- }
- } else {
- message = outputData.message || message;
- }
- }
- return generate(outputData, object, title, message, subtitle, open, templateOptions);
- }
|