Javascript 在 gulp 事件上 console.log 到 stdout
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27975129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
console.log to stdout on gulp events
提问by Rimian
I want to log to stdout (the config environment) when a gulptask is running or has run.
当gulp任务正在运行或已经运行时,我想登录到标准输出(配置环境)。
Something like this:
像这样的东西:
gulp.task('scripts', function () {
var enviroment = argv.env || 'development';
var config = gulp.src('config/' + enviroment + '.json')
.pipe(ngConstant({name: 'app.config'}));
var scripts = gulp.src('js/*');
return es.merge(config, scripts)
.pipe(concat('app.js'))
.pipe(gulp.dest('app/dist'))
.on('success', function() {
console.log('Configured environment: ' + environment);
});
});
I am not sure what event I should be responding to or where to find a list of these. Any pointers? Many thanks.
我不确定我应该响应什么事件或在哪里可以找到这些事件的列表。任何指针?非常感谢。
回答by Jacob Budin
(In December 2017, the gulp-utilmodule, which provided logging, was deprecated. The Gulp team recommended that developers replace this functionality with the fancy-logmodule. This answer has been updated to reflect that.)
(在 2017 年 12 月,gulp-util提供日志记录的模块被弃用。Gulp 团队建议开发人员用fancy-log模块替换此功能。此答案已更新以反映这一点。)
fancy-logprovides logging and was originally built by the Gulp team.
fancy-log提供日志记录,最初由 Gulp 团队构建。
var log = require('fancy-log');
log('Hello world!');
To add logging, Gulp's API documentationtell us that .srcreturns:
要添加日志记录,Gulp 的 API 文档告诉我们.src返回:
Returns a stream of Vinyl files that can be piped to plugins.
返回可以通过管道传输到插件的 Vinyl 文件流。
Node.js's Streamdocumentation provides a list of events. Put together, here's an example:
Node.js 的Stream文档提供了一个事件列表。放在一起,这是一个例子:
gulp.task('default', function() {
return gulp.src('main.scss')
.pipe(sass({ style: 'expanded' }))
.on('end', function(){ log('Almost there...'); })
.pipe(minifycss())
.pipe(gulp.dest('.'))
.on('end', function(){ log('Done!'); });
});
Note: The endevent may be called before the plugin is complete (and has sent all of its own output), because the event is called when "all data has been flushed to the underlying system".
注意:该end事件可能在插件完成之前被调用(并且已经发送了它自己的所有输出),因为该事件是在“所有数据都已刷新到底层系统”时调用的。
回答by Trevedhek
(PLEASE NOTE - In December 2017, the gulp-utilmodule was deprecated.)
(请注意 - 2017 年 12 月,该gulp-util模块已弃用。)
To build on the answer by Jacob Budin, I recently tried this and found it useful.
为了以Jacob Budin的答案为基础,我最近尝试了这个并发现它很有用。
var gulp = require("gulp");
var util = require("gulp-util");
var changed = require("gulp-changed");
gulp.task("copyIfChanged", function() {
var nSrc=0, nDes=0, dest="build/js";
gulp.src("app/**/*.js")
.on("data", function() { nSrc+=1;})
.pipe(changed(dest)) //filter out src files not newer than dest
.pipe(gulp.dest(dest))
.on("data", function() { nDes+=1;})
.on("finish", function() {
util.log("Results for app/**/*.js");
util.log("# src files: ", nSrc);
util.log("# dest files:", nDes);
});
}
回答by bsesic
Sadly gulp.utilist depreciated.
Use fancy-loginstead:
https://www.npmjs.com/package/fancy-log.
可悲的是,它gulp.util贬值了。使用fancy-log来代替:
https://www.npmjs.com/package/fancy-log。

