Par*_*ris 4 node.js gruntjs compass-sass grunt-notify
Grunt通知:https://github.com/dylang/grunt-notify很棒.但是,它似乎有点受限.据我所知,我需要预先生成所有消息.所以第一个问题是如何生成通知?
接下来,似乎grunt通知触发器基于某些任务的错误或成功.我猜基于std in/out/err?这种情况发生故障的问题是,如果某些任务不使用这些.如果存在编译错误,grunt指南针不使用stderr.那么如何在出现错误时运行grunt notify?然后这导致下一个问题.如何从grunt任务中获取控制台输出或stderr?
首先,使用咆哮.使用简单灵活.要安装咆哮:
npm install growl --save-dev
Run Code Online (Sandbox Code Playgroud)
然后你需要挂钩进程的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() (错误行)process.stderr.write()process.stdout.write()(错误行)它或多或少有效,但可能需要一些调整.
任务/ 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);
Run Code Online (Sandbox Code Playgroud)
然后你需要将它包含在你的Gruntfile.js中并带有一个require语句:
module.exports = function (grunt) {
grunt.initConfig({
...
});
require('./tasks/lib/notify');
...
};
Run Code Online (Sandbox Code Playgroud)
PS我已将文件放入tasks/lib/notify.js,但可以随意放在其他地方.
| 归档时间: |
|
| 查看次数: |
3003 次 |
| 最近记录: |