Grunt任务输出然后调用grunt-notify [英] Grunt task output then calling grunt-notify

查看:108
本文介绍了Grunt任务输出然后调用grunt-notify的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grunt通知: https://github.com/dylang/grunt-notify 很棒。但是,它似乎有点有限。据我所知,我所有的消息都需要预先生成。所以第一个问题是我怎样才能生成通知?



接下来,看起来grunt通知触发器基于某个任务的错误或成功。我猜基于std in / out / err?如果某些任务不使用这些任务,那么这个问题就会消失。如果有编译错误,grunt compass不使用stderr。那么如何在发生错误时运行grunt notify?这会导致下一个问题。我如何从咕噜任务中获取控制台输出或stderr?

解决方案

首先,使用咆哮。使用起来简单而灵活。要安装咆哮

  npm install growl --save-dev 

然后您需要钩子到进程的stderr / out流中。这样,每次有新消息到达stderr / out流时,您都可以创建通知。



这是我创建的。我制作了一个CommonJs模块,它将钩子添加到:


  • grunt.fail.warn() grunt.fail.fatal()

  • grunt.log.warn() grunt.log.error()

  • grunt.warn()

  • process.stderr.write()

  • process.stdout.write()(错误行)
  • (child) process.stderr.write()

  • (child) process.stdout.write()(错误行)


    它可以或多或少地工作,但可能需要进行一些调整。

    任务/ lib / notify.js

     (function(module){
    var grunt = require('grunt '),
    growl = require('growl'),
    Buffer = require('buffer')。Buffer;

    函数notify(obj,title){
    if(obj){
    var message = Buffer.isBuffer(obj)?obj.toString():(obj.message || obj);
    var msg = grunt.log.uncolor(message);
    $ b $ if(msg.length> 0){
    growl(msg,{
    title:title,
    image:'Console'
    }) ;



    $ b $ //为grunt.fail.warn()添加一个钩子,grunt.fail.fatal()
    ['
    $' forEach(function(level){
    grunt.util.hooker.hook(grunt.fail,level,function(obj){
    notify(obj);
    });
    });

    //为grunt.log.warn()添加一个钩子,grunt.log.error()
    ['warn','error']。forEach(function(level){
    grunt.util.hooker.hook(grunt.log,level,function(obj){
    notify(obj,level);
    });
    });

    //向grunt.warn()添加一个钩子
    grunt.util.hooker.hook(grunt,'warn',function(obj){
    notify(obj, 'warn');
    });

    //向process.stderr.write()添加一个钩子
    grunt.util.hooker.hook(process.stderr,'write',function(obj){
    var messages = grunt.log.uncolor((Buffer.isBuffer(obj)?obj.toString():(obj.message || obj)))。split('\\\
    ');
    messages.forEach (函数(message){
    notify(message,'stderr');
    });
    });

    //向process.stdout.write()添加一个钩子(只有错误行)
    grunt.util.hooker.hook(process.stdout,'write',function(obj) {
    var messages = grunt.log.uncolor((Buffer.isBuffer(obj)?obj.toString():(obj.message || obj)))。split('\\\
    '); $ b $ message.forEach(function(message){
    if(message& message.indexOf('error')> -1){
    notify(message,'stdout');
    }
    });
    });

    //为子进程添加一个钩子stdout / stderr write()(只有错误行)
    grunt.util.hooker.hook(grunt.util,'spawn',{
    post:function(child){
    child.stderr.on('data',function(data){
    var messages = grunt.log.uncolor(data.toString())。split() '\\\
    ');
    messages.forEach(function(message){
    notify(message,'stderr');
    });
    });
    child.stdout.on('data',function(data){
    var messages = grunt.log.uncolor(data.toString())。split('\\\
    ');
    messages .forEach(function(message){
    if(message& message.indexOf('error')> -1){
    notify(message,'stdout');
    }
    });
    });
    }
    });
    })(module);

    然后您需要在 Gruntfile.js 中加入 require 语句:

      module.exports = function(grunt){

    grunt.initConfig({
    ...
    });
    require('./ tasks / lib / notify');

    ...
    };

    PS我将文件放在 tasks / lib / notify.js中,但随意放置在其他地方。


    Grunt notify: https://github.com/dylang/grunt-notify is great. However, it seems a bit limited. As far as I can tell all my messages need to be pre-generated. So the first question is how can I generate notifications?

    Next, It seems that grunt notify triggers based on error or success of some task. I guess based on std in/out/err? The problem where this breaks down is if some task doesn't use these. grunt compass doesn't use stderr if there are compile errors. So how can I run grunt notify when it has an error? This then leads to the next question. How can I grab the console output or stderr from a grunt task?

    解决方案

    First of all, use growl. It is easy and flexible to use. To install growl:

    npm install growl --save-dev
    

    Then you need to hook into the stderr/out stream of the process. This way you can create a notification each time a new message arrives at the stderr/out stream.

    This is what I've created. I've made a CommonJs module which adds hooks to:

    • grunt.fail.warn(), grunt.fail.fatal()
    • grunt.log.warn(), grunt.log.error()
    • grunt.warn()
    • process.stderr.write()
    • process.stdout.write() (error lines)
    • (child) process.stderr.write()
    • (child) process.stdout.write() (error lines)

    It works more or less, but it may need some tweaking.

    tasks/lib/notify.js

    (function (module) {
        var grunt = require('grunt'),
            growl = require('growl'),
            Buffer = require('buffer').Buffer;
    
        function notify(obj, title) {
            if (obj) {
                var message = Buffer.isBuffer(obj) ? obj.toString() : (obj.message || obj);
                var msg = grunt.log.uncolor(message);
    
                if (msg.length > 0) {
                    growl(msg, {
                        title: title,
                        image: 'Console'
                    });
                }
            }
        }
    
    // add a hook to grunt.fail.warn(), grunt.fail.fatal()
        ['warn', 'fatal'].forEach(function (level) {
            grunt.util.hooker.hook(grunt.fail, level, function(obj) {
                notify(obj);
            });
        });
    
    // add a hook to grunt.log.warn(), grunt.log.error()
        ['warn', 'error'].forEach(function (level) {
            grunt.util.hooker.hook(grunt.log, level, function(obj) {
                notify(obj, level);
            });
        });
    
    // add a hook to grunt.warn()
        grunt.util.hooker.hook(grunt, 'warn', function(obj) {
            notify(obj, 'warn');
        });
    
    // add a hook to process.stderr.write()
        grunt.util.hooker.hook(process.stderr, 'write', function(obj) {
            var messages = grunt.log.uncolor((Buffer.isBuffer(obj) ? obj.toString() : (obj.message || obj))).split('\n');
            messages.forEach(function (message) {
                notify(message, 'stderr');
            });
        });
    
    // add a hook to process.stdout.write() (only error lines)
        grunt.util.hooker.hook(process.stdout, 'write', function(obj) {
            var messages = grunt.log.uncolor((Buffer.isBuffer(obj) ? obj.toString() : (obj.message || obj))).split('\n');
            messages.forEach(function (message) {
                if (message && message.indexOf('error ') > -1) {
                    notify(message, 'stdout');
                }
            });
        });
    
    // add a hook to child process stdout/stderr write() (only error lines)
        grunt.util.hooker.hook(grunt.util, 'spawn', {
            post: function(child) {
                child.stderr.on('data', function (data) {
                    var messages = grunt.log.uncolor(data.toString()).split('\n');
                    messages.forEach(function (message) {
                        notify(message, 'stderr');
                    });
                });
                child.stdout.on('data', function (data) {
                    var messages = grunt.log.uncolor(data.toString()).split('\n');
                    messages.forEach(function (message) {
                        if (message && message.indexOf('error ') > -1) {
                            notify(message, 'stdout');
                        }
                    });
                });
            }
        });
    }) (module);
    

    Then you need to include it in your Gruntfile.js with a require statement:

    module.exports = function (grunt) {
    
        grunt.initConfig({
           ...
        });
        require('./tasks/lib/notify');
    
        ...
    };
    

    PS I've placed the file in tasks/lib/notify.js, but feel free to place it somewhere else.

    这篇关于Grunt任务输出然后调用grunt-notify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆