gulp-notify
Advanced tools
Comparing version 0.6.2 to 1.0.0-beta
@@ -93,2 +93,4 @@ | ||
custom.logLevel(1); | ||
gulp.src("../test/fixtures/*") | ||
@@ -95,0 +97,0 @@ .pipe(custom('<%= file.relative %>')) |
190
index.js
@@ -1,186 +0,18 @@ | ||
var through = require("through2"), | ||
gutil = require("gulp-util"), | ||
template = require("lodash.template"), | ||
notifier = require("node-notifier"); | ||
var api = require('./lib/extra_api'); | ||
"use strict"; | ||
var logLevel = 2; | ||
var logger = gutil.log; | ||
// Expose plugin | ||
module.exports = require('./lib/notify'); | ||
var plugin = module.exports = function (options) { | ||
// Expose onError behaviour | ||
module.exports.onError = api.onError; | ||
options = options || {}; | ||
var templateOptions = options.templateOptions || {}; | ||
// Expose to set log level | ||
module.exports.logLevel = api.logLevel; | ||
var reporter = options.notifier || notifier.notify; | ||
var lastFile = null; | ||
// Expose to set new logger | ||
module.exports.logger = api.logger; | ||
function notify (file, enc, callback) { | ||
var stream = this; | ||
report(reporter, file, options, templateOptions, function (err) { | ||
err && stream.emit("error", err); | ||
stream.push(file); | ||
callback(); | ||
}); | ||
} | ||
if (!options.onLast) { | ||
return through.obj(notify); | ||
} | ||
// Only send notification on the last file. | ||
return through.obj(function (file, enc, callback) { | ||
lastFile = file; | ||
this.push(file); | ||
callback(); | ||
}, function (callback) { | ||
var stream = this; | ||
if (!lastFile) { | ||
return callback(); | ||
} | ||
report(reporter, lastFile, options, templateOptions, function (err, file) { | ||
if (err) { | ||
stream.emit("error", err); | ||
return callback(); | ||
} | ||
}); | ||
lastFile = null; // reset | ||
return callback(); | ||
}); | ||
}; | ||
var onError = module.exports.onError = function (options) { | ||
options = options || {}; | ||
var reporter = options.notifier || notifier.notify; | ||
return function (error) { | ||
report(reporter, error, options); | ||
}; | ||
}; | ||
module.exports.withReporter = function (reporter) { | ||
if (!reporter) throw new gutil.PluginError("gulp-notify", "No custom reporter defined."); | ||
var inner = function (options) { | ||
options = options || {}; | ||
if (typeof options !== "object") { | ||
options = { | ||
message: options | ||
}; | ||
} | ||
options.notifier = reporter; | ||
return plugin(options); | ||
}; | ||
inner.onError = function (options) { | ||
options = options || {}; | ||
if (typeof options !== "object") { | ||
options = { | ||
message: options | ||
}; | ||
} | ||
options.notifier = reporter; | ||
return onError(options); | ||
} | ||
return inner; | ||
}; | ||
module.exports.setLogLevel = function (newLogLevel) { | ||
logLevel = newLogLevel; | ||
}; | ||
module.exports.logger = function (newLogger) { | ||
if (!newLogger) return logger; | ||
logger = newLogger; | ||
}; | ||
function logError (options, isError) { | ||
if (!logLevel) return; | ||
if (logLevel === 1 && !isError) return; | ||
color = isError ? "red" : "green"; | ||
if (!gutil.colors[color]) return; | ||
logger(gutil.colors.cyan('gulp-notify') + ':', | ||
'[' + gutil.colors.blue(options.title) + ']', | ||
gutil.colors[color].call(gutil.colors, options.message) | ||
); | ||
} | ||
function report (reporter, message, options, templateOptions, callback) { | ||
var self = this; | ||
callback = callback || function () {}; | ||
if (!reporter) return callback(new gutil.PluginError("gulp-notify", "No reporter specified.")); | ||
// Try/catch the only way to go to ensure catching all errors? Domains? | ||
try { | ||
var options = constructOptions(options, message, templateOptions); | ||
logError(options, (message instanceof Error)); | ||
reporter(options, function (err) { | ||
if (err) return callback(new gutil.PluginError("gulp-notify", err)); | ||
return callback(); | ||
}); | ||
} catch (err) { | ||
return callback(new gutil.PluginError("gulp-notify", err)); | ||
} | ||
} | ||
function constructOptions (options, object, templateOptions) { | ||
var message = object.path || object.message || object, | ||
title = !(object instanceof Error) ? "Gulp notification" : "Error running Gulp"; | ||
if (typeof options === "function") { | ||
message = options(object); | ||
} | ||
if (typeof options === "object") { | ||
if (typeof options.title === "function") { | ||
title = options.title(object); | ||
} else { | ||
title = options.title || title; | ||
} | ||
if (typeof options.message === "function") { | ||
message = options.message(object); | ||
} else { | ||
message = options.message || message; | ||
} | ||
} | ||
if (typeof options === "string") { | ||
message = options; | ||
} | ||
if (object instanceof Error) { | ||
var titleTemplate = template(title); | ||
var messageTemplate = template(message); | ||
return { | ||
title: titleTemplate({ | ||
error: object, | ||
options: templateOptions | ||
}), | ||
message: messageTemplate({ | ||
error: object, | ||
options: templateOptions | ||
}) | ||
}; | ||
} | ||
return { | ||
title: gutil.template(title, { | ||
file: object, | ||
options: templateOptions | ||
}), | ||
message: gutil.template(message, { | ||
file: object, | ||
options: templateOptions | ||
}) | ||
}; | ||
} | ||
// Syntactiv sugar | ||
module.exports.withReporter = require('./lib/withReporter'); |
{ | ||
"name": "gulp-notify", | ||
"version": "0.6.2", | ||
"version": "1.0.0-beta", | ||
"description": "A plugin for Gulp to send messages to Mac Notification Center or Linux' notify-send", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -10,2 +10,3 @@ # gulp-notify [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][depstat-image]][depstat-url] | ||
| Node Version | >= 0.8 | | ||
| Package Version | 1.0.0-beta | | ||
@@ -217,3 +218,3 @@ **Note: Without overriding the notifier, Mac OS X >= 10.8 or as of v0.3.2, Linux with `notify-send` installed is required for this to run.** | ||
### notify.setLogLevel(level) | ||
### notify.logLevel(level) | ||
Type: `Integer` | ||
@@ -223,3 +224,4 @@ Default: `2` | ||
Set if logger should be used or not. If log level is set to 0, | ||
no logging will be used. | ||
no logging will be used. If no new log level is passed, the | ||
current log level is returned. | ||
@@ -230,3 +232,2 @@ * `0`: No logging | ||
If logging is set to `> 0`, the title and | ||
@@ -255,7 +256,10 @@ message passed to `gulp-notify` will be logged like so: | ||
[gulp] ├── one | ||
[gulp] ├── message | ||
[gulp] ├── customReporter | ||
[gulp] ├── message | ||
[gulp] ├── template | ||
[gulp] ├── templateadv | ||
[gulp] ├── function | ||
[gulp] ├── onlast | ||
[gulp] └── error | ||
[gulp] ├── error | ||
[gulp] └── customError | ||
``` | ||
@@ -274,2 +278,7 @@ | ||
### `v1.0.0-beta` | ||
1. Major rewrites and restructure in code | ||
2. Lock down on API, changes `setLogLevel` to `logLevel` | ||
3. Exposes logLevel and logger on the withReporter object. | ||
### `v0.6.2` | ||
@@ -276,0 +285,0 @@ 1. Adds another logging level: 0 - none, 1 - error, 2 - all. |
@@ -26,3 +26,3 @@ /*global describe, it*/ | ||
beforeEach(function () { | ||
notify.setLogLevel(0); | ||
notify.logLevel(0); | ||
notify.logger(originalLogger); | ||
@@ -462,3 +462,3 @@ }); | ||
var notifier = notify.withReporter(mockGenerator); | ||
should.exist(notify.setLogLevel); | ||
should.exist(notify.logLevel); | ||
should.exist(notify.logger); | ||
@@ -470,3 +470,3 @@ done(); | ||
var srcFile = join(__dirname, "./fixtures/1.txt"); | ||
notify.setLogLevel(1); | ||
notify.logLevel(1); | ||
notify.logger(function (options) { | ||
@@ -490,3 +490,3 @@ should.exist(true); | ||
var hasBeenCalled = false; | ||
notify.setLogLevel(0); | ||
notify.logLevel(0); | ||
@@ -493,0 +493,0 @@ notify.logger(function () { |
36212
18
774
302