console.log到gulp事件的标准输出 [英] console.log to stdout on gulp events

查看:129
本文介绍了console.log到gulp事件的标准输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 gulp 任务正在运行或运行时登录到stdout(配置环境)。

I want to log to stdout (the config environment) when a gulp task is running or has run.

类似这样:

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.

推荐答案

(2017年12月, gulp-util 模块,它提供日志记录,已被弃用 .Gulp团队建议开发人员用 fancy-log 模块替换这个功能,这个答案已经更新以反映这一点。)

(In December 2017, the gulp-util module, which provided logging, was deprecated. The Gulp team recommended that developers replace this functionality with the fancy-log module. This answer has been updated to reflect that.)

fancy-log 提供了日志记录功能,最初由Gulp团队构建。

fancy-log provides logging and was originally built by the Gulp team.

var log = require('fancy-log');
log('Hello world!');






要添加日志记录, Gulp的API文档告诉我们 .src 返回:


To add logging, Gulp's API documentation tell us that .src returns:


返回一个可以传送给插件的乙烯基文件流。

Returns a stream of Vinyl files that can be piped to plugins.

Node.js的 Stream 文档提供了事件列表。放在一起,下面是一个例子:

Node.js's Stream documentation provides a list of events. Put together, here's an example:

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 end event 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".

这篇关于console.log到gulp事件的标准输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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