【问题标题】:How can I get gulp to be silent for some tasks (unit tests,vet etc)?如何让 gulp 对某些任务(单元测试、兽医等)保持沉默?
【发布时间】:2015-09-30 09:58:04
【问题描述】:
我有一些在文件更改时触发的单元测试和审查 gulp 任务。
我希望 gulp 在我启动这些任务时表现得像 --silent 被传递给它,因为 gulps 默认输出使输出混乱,我不想每次都指定参数。这可能吗?
【问题讨论】:
标签:
javascript
node.js
unit-testing
gulp
【解决方案1】:
经过一番挖掘 gulp 的源代码:
Gulp 继承了继承 EventEmitter 的 Orchestrator。全局 gulp 需要本地 gulp 并附加一些事件侦听器(task_start 和 task_stop)。
我删除了这些处理程序。这是一个 AWFULL hack,但它成功了。
解决方案(将其放在 gulpfile.js 的顶部):
var gulp = require('gulp');
var cmd = String(process.argv[2]);
if (/^((watch|vet|unit-test|integration-test)(:.*)?)$/.test(cmd)) {
console.warn('Logging silenced');
var isWatching = /^(watch:.*)$/.test(cmd);
var firstCall = false; // Do not clear on first run
var on = gulp.on;
gulp.on = function (name, handler) {
if (/^(task_start|task_stop)$/.test(name)) {
// Do some inspection on the handler
// This is a ugly hack, and might break in the future
if (/gutil\.log\(\s*'(Starting|Finished)/.test(handler.toString())) {
return; //No operation
}
}
return on.apply(gulp, arguments);
};
gulp.on('start', function () {
// start fires multiple times
// make sure we only call this once
if (firstCall) {
if (isWatching) {
// Clear console
// Ref: https://stackoverflow.com/questions/5367068/clear-the-ubuntu-bash-screen-for-real
process.stdout.write('\033c');
}
console.log('Started task');
firstCall = false;
}
});
gulp.on('stop', function () {
console.log('Task finished');
firstCall = true;
});
}