更多gulp常用插件使用请访问:gulp常用插件汇总


gulp-notify这是一款gulp通知插件。

更多使用文档请点击访问gulp-notify工具官网

安装

一键安装不多解释

  1. npm install --save-dev gulp-notify

使用

例1:

  1. var notify = require("gulp-notify");
  2. gulp.src("./src/test.ext")
  3. .pipe(notify("Hello Gulp!"));

例2:

  1. var notify = require("gulp-notify");
  2. gulp.src("./src/test.ext")
  3. .pipe(notify("Found file: <%= file.relative %>!"));

有关更多输入,请参见示例,或参见API部分。

注释/提示

即使出错,gulp-notify也会传递vinyl files 。因此,如果您使用的是gulp-plumber ,如果通知程序返回错误,则运行不会中断。

如果你想通知错误,可以使用gulp-plumber 不中断运行,迫使你不得不重新启动gulp。

您可以使用notify.onError()作为gulp-plumbererrorHandler ,如下所示:

  1. gulp.src("../test/fixtures/*")
  2. .pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
  3. .pipe(through(function () {
  4. this.emit("error", new Error("Something happend: Error message!"))
  5. }));

API

  • notify(String)

    用于通知流中每个数据的消息。字符串可以是lodash模板,因为它通过gulp-util.template传递。
  • notify(Function)

    类型: function(VinylFile)

    来自gulp流的Vinyl File作为参数传入。

    该函数的结果可以是用作消息的字符串或选项对象(请参见下文)。如果返回值是一个字符串,则它可以是lodash模板,因为它是通过gulp-util.template传递的。

    如果false从函数返回,则通知将不会运行。
  • notify(options)

选项传递到报告程序上,因此在Windows上,您可以定义Growl主机,在Mac上,您可以传递contentImage,依此类推。

有关 所有选项,请参阅节点通知程序

默认通知值:

* 定期通知的Gulp徽标

* 错误时倒置了Gulp徽标

* 在Mac上,青蛙声音错误。

另请参见高级示例

  • options.onLast

    类型:Boolean

    默认值:false

    如果通知仅应在流的最后一个文件上发生。默认情况下,每个文件都会触发通知。
  • options.emitError

    类型:Boolean

    默认值:false

    返回的流是否应该发出错误。如果emitErrortrue,则必须.on('error') 手动处理,以防通知程序(gulp-notify)失败。如果false设置了默认值,则不会发出错误,而只是将其打印到控制台。

    这意味着您可以在CI系统上运行通知程序,而无需选择退出,而只是让其正常地失败。
  • options.message

    类型:String

    默认:流中的文件路径

    您希望附加到文件的消息。字符串可以是lodash模板,因为它通过gulp-util.template传递。

    范例:Created <%= file.relative %>
  • 作为功能函数

    类型: Function(vinylFile)

    请参阅notify(Function)
  • options.title

    类型:String

    默认:“ Gulp通知”

    通知的标题。字符串可以是lodash模板,因为它通过gulp-util.template传递。

    范例:Created <%= file.relative %>
  • 作为功能函数

    类型: Function(vinylFile)

    请参阅notify(Function)
  • options.templateOptions

    类型:Object

    默认值:{}

    传递给lodash模板的对象,用于传递给模板的其他属性。
  1. gulp.src("../test/fixtures/*")
  2. .pipe(notify({
  3. message: "Generated file: <%= file.relative %> @ <%= options.date %>",
  4. templateOptions: {
  5. date: new Date()
  6. }
  7. }))
  • options.notifier

    类型:Function(options, callback)

    默认:node-notifier模块

    通过传入函数来交换通知程序。该函数需要两个参数:optionscallback

    通知完成后必须调用回调。选项将同时包含标题和消息。

    请参阅notify.withReporter语法糖。

notify.on(event, function (notificationOptions)) - Events

如果该wait选项设置为true,则通知者将触发事件clicktimeout,无论用户单击通知还是超时。您在主通知对象(而不是产生流)上侦听这些事件。

  1. var notify = require('gulp-notify');
  2. notify.on('click', function (options) {
  3. console.log('I clicked something!', options);
  4. });
  5. notify.on('timeout', function (options) {
  6. console.log('The notification timed out', options);
  7. });
  8. gulp.task("click", function () {
  9. return gulp.src("some/glob/**")
  10. .pipe(notify({ message: 'Click or wait', wait: true }));
  11. });

notify.withReporter(Function)

类型: Reporter

包装options.notifier仅使用传入的报告程序返回新的通知功能。

例:

  1. var custom = notify.withReporter(function (options, callback) {
  2. console.log("Title:", options.title);
  3. console.log("Message:", options.message);
  4. callback();
  5. });
  6. gulp.src("../test/fixtures/1.txt")
  7. .pipe(custom("This is a message."));

这将与

  1. gulp.src("../test/fixtures/1.txt")
  2. .pipe(notify({
  3. message: "This is a message."
  4. notifier: function (options, callback) {
  5. console.log("Title:", options.title);
  6. console.log("Message:", options.message);
  7. callback();
  8. }
  9. }));

但是,很多漂亮。

notify.onError()

using完全相同的API notify(),但是在vinyl File 传递a的地方,传递错误对象。

例:

  1. gulp.src("../test/fixtures/*")
  2. .pipe(through(function () {
  3. this.emit("error", new Error("Something happend: Error message!"))
  4. }))
  5. .on("error", notify.onError(function (error) {
  6. return "Message to the notifier: " + error.message;
  7. }));

或者简单地:

  1. gulp.src("../test/fixtures/*")
  2. .pipe(through(function () {
  3. this.emit("error", new Error("Something happend: Error message!"))
  4. }))
  5. .on("error", notify.onError("Error: <%= error.message %>"));
  1. gulp.src("../test/fixtures/*")
  2. .pipe(through(function () {
  3. this.emit("error", new Error("Something happend: Error message!"))
  4. }))
  5. .on("error", notify.onError({
  6. message: "Error: <%= error.message %>",
  7. title: "Error running something"
  8. }));

onError()终点不支持lodash.template

onError()会自动为您结束视频流。使观看更加轻松。

notify.logLevel(level)

类型:Integer

默认值:2

设置是否使用记录器。如果日志级别设置为0,将不使用任何日志记录。如果未传递新的日志级别,则返回当前日志级别。

  • 0:无日志记录
  • 1:登录错误
  • 2:记录错误和常规通知。

    如果将logging设置为> 0,则将记录标题和传递给的消息,gulp-notify如下所示:
  1. gulp-notify git:(master) gulp --gulpfile examples/gulpfile.js one
  2. [gulp] Using file /Users/example/gulp-notify/examples/gulpfile.js
  3. [gulp] Working directory changed to /Users/example/repos/gulp-notify/examples
  4. [gulp] Running 'one'...
  5. [gulp] Finished 'one' in 4.08 ms
  6. [gulp] gulp-notify: [Gulp notification] /Users/example/gulp-notify/test/fixtures/1.txt

禁用 gulp-notify

如果您运行的系统处理通知的能力很差,或者只是不想使用,gulp-notify而您的项目可以呢?您可以gulp-notify 使用环境变量禁用它DISABLE_NOTIFIER

  1. export DISABLE_NOTIFIER=true;

这将禁用所有方法。notify()notify.onErrornotify.withReporter

例子:

要查看从根目录运行的所有示例:

  1. $ gulp --gulpfile examples/gulpfile.js --tasks
  2. [gulp] Using file /Users/example/gulp-notify/examples/gulpfile.js
  3. [gulp] Working directory changed to /Users/example/gulp-notify/examples
  4. [gulp] Tasks for /Users/example/gulp-notify/examples/gulpfile.js
  5. [gulp] ├── multiple
  6. [gulp] ├── one
  7. [gulp] ├── message
  8. [gulp] ├── customReporter
  9. [gulp] ├── template
  10. [gulp] ├── templateadv
  11. [gulp] ├── function
  12. [gulp] ├── onlast
  13. [gulp] ├── advanceMac
  14. [gulp] ├── error
  15. [gulp] ├── forceGrowl
  16. [gulp] └── customError

运行示例:

  1. $ gulp --gulpfile examples/gulpfile.js multiple
  2. [gulp] Using file /Users/example/gulp-notify/examples/gulpfile.js
  3. [gulp] Working directory changed to /Users/example/gulp-notify/examples
  4. [gulp] Running 'multiple'...
  5. [gulp] Finished 'multiple' in 3.75 ms

作为jshint报告员

gulp-notify可以轻松用作jshint报告程序。当jshint在乙烯基文件上公开结果时,我们可以在如下函数中使用它们:

  1. gulp.task('lint', function() {
  2. gulp.src('/src/**/*.js')
  3. .pipe(jshint())
  4. // 使用gulp-notify作为jshint报告器
  5. .pipe(notify(function (file) {
  6. if (file.jshint.success) {
  7. // 不显示的东西,如果成功
  8. return false;
  9. }
  10. var errors = file.jshint.results.map(function (data) {
  11. if (data.error) {
  12. return "(" + data.error.line + ':' + data.error.character + ') ' + data.error.reason;
  13. }
  14. }).join("\n");
  15. return file.relative + " (" + file.jshint.results.length + " errors)\n" + errors;
  16. }));
  17. });

如果您在中使用消息功能gulp-notify,则不会显示该消息。直接使用function和都是如此{ message: function () {}}

gulp常用插件之gulp-notify使用的更多相关文章

  1. gulp常用插件之gulp-size使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-size这是一款显示项目的大小插件. 更多使用文档请点击访问gulp-size工具官网. 安装 一键安装不多解释 npm install ...

  2. gulp常用插件之browser-sync使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 browser-sync这是一个可以在多端(pc.移动.平板)实时监测文件修改,自动刷新浏览器的工具.其实这并不是转给gulp使用的,在其它构建工 ...

  3. 精通gulp常用插件

    本文主要展示的是gulp常用插件的使用方法和用途,通过对插件的熟练运用达到精通gulp.不定期更新.可以到github上面下载DEMO. github地址:lin-xin/gulp-plugins 匹 ...

  4. node和gulp实现前端工程自动化(附:gulp常用插件)

    /** * 1. LESS编译 压缩 合并 * 2. JS合并 压缩 混淆 * 3. img复制 * 4. html压缩 */ // 在gulpfile中先载入gulp包,因为这个包提供了一些APIv ...

  5. gulp常用插件之gulp-eslint使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 ** gulp-eslint**这是一个用于识别和报告在ECMAScript/JavaScript代码中找到的模式的Gulp插件.. 更多使用文档 ...

  6. gulp常用插件之gulp-babel使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 gulp-babel这是Babel的Gulp插件. 此自述文件适用于gulp-babel v8 + Babel v7检查7.x分支以了解使用Bab ...

  7. gulp常用插件之gulp-postcss使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 ** gulp-postcss**这是一款通过多个插件通过管道传递CSS,但是仅解析一次CSS. 更多使用文档请点击访问gulp-postcss工 ...

  8. gulp常用插件之cssnano使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 cssnano这是一款将你的 CSS 文件做 多方面的的优化,以确保最终生成的文件 对生产环境来说体积是最小的插件. 更多使用文档请点击访问cha ...

  9. gulp常用插件之pump使用

    更多gulp常用插件使用请访问:gulp常用插件汇总 pump这是一款小型节点模块,可将流连接在一起并在其中一个关闭时将其全部销毁. 使用标准source.pipe(dest)源时,如果dest发出关 ...

随机推荐

  1. 实例探究Aspectj,解析SentinelResourceAspect

    为了学习SentinelResourceAspect,这篇文章里我用Aspectj实现一个AOP实例,一起来看下. Sentinel 提供了 @SentinelResource 注解用于定义资源,支持 ...

  2. 【Debian】 Debian 安装源配置

    Debian 安装源配置 所有的Linux安装完后第一件事,就是要更新安装源 安装源是什么呢,安装源又称软件源,是指把软件的安装源地址放在一个pool里面,用一条命令(比如apt-get instal ...

  3. Flutter 基础布局Widgets之Align

    Align的作用是为了设置子child的对齐方式,一般作为其他控件的一个参数. 构造函数 const Align({ Key key, this.alignment = Alignment.cente ...

  4. 10-SpringMVC04

    FreeMarker 1.入门案例 1. 导包:freemarker.jar 2. 需要创建模板文件的路径:src/main/resources/template 3. 创建一个模板对象:hello. ...

  5. CentOS6.5下部署SVN

    查看系统版本,安装SVN软件及创建目录 [root@A-linux ~]# uname -r 2.6.32-431.el6.x86_64 [root@A-linux ~]# cat /etc/redh ...

  6. 大数四则运算之减法运算-----c语言版

    /* 分三种情况: 1.减数长度大于被减数 交换减数与被减数,输出负号,方便减 2.减数长度等于被减数(分三种情况) a.减数大于被减数,类似1情况1 b.减数等于被减数,两数相等,直接输出0,完成. ...

  7. 「Flink」配置使用Flink调试WebUI

    很多时候,我们在IDE中编写Flink代码,我们希望能够查看到Web UI,从而来了解Flink程序的运行情况.按照以下步骤操作即可,亲测有效. 1.添加Maven依赖 <dependency& ...

  8. Python股票量化第一步环境搭建

    很久之前就希望可以量化分析股票,那么国内的股票数据API也有个,最有名的就是tushare,然后还有baostock. 今天我们就来研究一下这个baostock吧. 首先,我们需要下载一个叫做anac ...

  9. mysql 报错:Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column. To disable safe mode, toggle the option in Preferences

    目录 #事故现场 #解决方法 #事故现场 mysql执行update操作报错: sql如下: update psmp.Users set name='Jack' where name='Lily'; ...

  10. Appium超详细环境搭建for Mac

      兜兜转转试用了一圈自动化框架后,回归到appium,与一年之前相比,appium有了很大的改变:1.iOS 9 之前一直以 instruments 下的 UIAutomation为驱动底层技术(弊 ...