Grunt任务输出然后调用grunt-notify [英] Grunt task output then calling 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模块,它将钩子添加到: 它可以或多或少地工作,但可能需要进行一些调整。 任务/ lib / notify.js 然后您需要在 Gruntfile.js 中加入 PS我将文件放在 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: 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: It works more or less, but it may need some tweaking. tasks/lib/notify.js Then you need to include it in your Gruntfile.js with a PS I've placed the file in 这篇关于Grunt任务输出然后调用grunt-notify的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
grunt.fail.warn()
, grunt.fail.fatal()
grunt.log.warn()
, grunt.log.error()
grunt.warn()
process.stderr.write()
process.stdout.write()
(错误行)
process.stderr.write()
process.stdout.write()
(错误行)
(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);
require
语句:
module.exports = function(grunt){
grunt.initConfig({
...
});
require('./ tasks / lib / notify');
...
};
tasks / lib / notify.js中
,但随意放置在其他地方。npm install growl --save-dev
grunt.fail.warn()
, grunt.fail.fatal()
grunt.log.warn()
, grunt.log.error()
grunt.warn()
process.stderr.write()
process.stdout.write()
(error lines)process.stderr.write()
process.stdout.write()
(error lines)(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);
require
statement:module.exports = function (grunt) {
grunt.initConfig({
...
});
require('./tasks/lib/notify');
...
};
tasks/lib/notify.js
, but feel free to place it somewhere else.