简体   繁体   English

在完成所有任务后,如何在gulp中处理通知?

[英]How do I process a notification in gulp after all tasks are complete?

I am getting into the many streams of Gulp and have run across a confounding subject. 我正在进入许多Gulp流,并遇到了一个混乱的主题。 I'd like to post a notification when all the tasks are actually complete. 我想在所有任务实际完成时发布通知。 I see that the tasks are executed but running asynchronously by default. 我看到任务已执行但默认情况下是异步运行的。

What if I want to show display a notification after each step is complete...and at the end when all steps are complete? 如果我想在每个步骤完成后显示通知,并且在所有步骤完成后结束,该怎么办?

What is the best way to gain more control over the timing of tasks in gulp? 在gulp中获得更多控制任务时间的最佳方法是什么?

Currently, I'm using gulp-notify to display notifications. 目前,我正在使用gulp-notify来显示通知。

UPDATE X 2 更新X 2

I'm not really having any errors, but would like to better understand the order of operations here and how I can trigger my own notification of when all tasks have been complete. 我真的没有任何错误,但是想更好地了解这里的操作顺序以及如何触发我自己的所有任务完成时的通知。 Here is the example. 这是一个例子。

var gulp = require('gulp'),
    gp_concat = require('gulp-concat'),
    gp_notify = require('gulp-notify');

gulp.task('task1', function() {
    return gulp.src(['file1.js','file2.js'])
        .pipe(gp_concat('file1_2.js')
        .pipe(gp_notify({ message: "file1_2 created." }
})

gulp.task('task2', function() {
    return gulp.src(['file3.js','file4.js'])
        .pipe(gp_concat('file3_4.js')
        .pipe(gp_notify({ message: "file3_4 created." }
})

gulp.task('mainTask', ['task1','task2'], function() {
    gulp.src('file*_*.js')
        .pipe(gp_notify({ message: "All tasks complete." }))
});

In the console, the notifications are now timed correctly, however at the end of the execution, right before Finished 'mainTask' after xx ms the final 'All tasks complete' message fires off [n-1] times, where n is the count of sub tasks. 在控制台中,通知现在正确计时,但是在执行结束时,在xx ms之后的完成'mainTask'之前,最后的'所有任务完成'消息将触发[n-1]次,其中n是计数子任务。

What is causing this final notification to get triggered so many times and how can that be suppressed? 导致此最终通知被多次触发的原因是什么,以及如何抑制?

I guess you tried to append a call to gulp-notify at the end of every task and one to the end of a general task who depends on the previous ones. 我猜你试图在每个任务结束时附加一个gulp-notify的调用,并且在依赖于前一个任务的一般任务结束时调用一个。

So the problem probably is related to the tasks not communicating correctly their "finished status" to the main task. 所以问题可能与任务没有正确地将他们的“完成状态”正确地传达给主任务有关。 For this topic you can check the Running tasks in series, ie Task Dependency recipe or the always handy run-sequence package. 对于本主题,您可以检查系列中的正在运行的任务,即任务依赖关系配方或始终方便的运行顺序包。

Update 更新

In order to let a task know that its subtasks are done is to make them return a stream so in the example code the subtasks can simply return the gulp.src pipe. 为了让任务知道它的子任务完成是让它们返回一个流,所以在示例代码gulp.src任务可以简单地返回gulp.src管道。

gulp-notify when called with a string message will output that message for every file present in the passed stream so, in the final task of the example, it will be called twice. gulp-notify在使用字符串消息调用时将为传递的流中存在的每个文件输出该消息,因此,在示例的最后一个任务中,它将被调用两次。 The call to gp_notify should be changed to: gp_notify({ message: "All tasks complete." , onLast: true }). 对gp_notify的调用应更改为:gp_notify({message:“所有任务完成。” ,onLast:true })。 This way the message will be notified only for the last file. 这样,将仅通知最后一个文件的消息。

By default, gulp-notify sends notifications for each file in the stream. 默认情况下, gulp-notify为流中的每个文件发送通知。 If you only want one notification per-stream, add onLast: true to the options passed to notify() . 如果您只需要每个流一个通知,请将onLast: true添加到传递给notify()的选项中。 eg 例如

var gulp = require('gulp'),
    gp_concat = require('gulp-concat'),
    gp_notify = require('gulp-notify');

gulp.task('task1', function() {
    return gulp.src(['file1.js','file2.js'])
        .pipe(gp_concat('file1_2.js'))
        .pipe(gp_notify({ message: "file1_2 created.", onLast: true }));
});

gulp.task('task2', function() {
    return gulp.src(['file3.js','file4.js'])
        .pipe(gp_concat('file3_4.js'))
        .pipe(gp_notify({ message: "file3_4 created.", onLast: true }));
});

gulp.task('mainTask', ['task1','task2'], function() {
    gulp.src('file*_*.js')
        .pipe(gp_notify({ message: "All tasks complete.", onLast: true }));
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM