简体   繁体   English

当观察任务完成时,我可以通知吗?

[英]Can I gulp-notify when a watched task completes?

We have a gulpfile with ~12 tasks, 4 of which are activated by a gulp.watch . 我们有一个包含约12个任务的gulpfile,其中4个由gulp.watch激活。 I would like to use gulp-notify when a task started by gulp.watch completes. gulp.watch启动的任务完成时,我想使用gulp.watch gulp-notify I don't want gulp-notify to do anything if a task is run directly. 如果直接运行任务,我不希望gulp-notify做任何事情。 Sample code below: 示例代码如下:

const
    debug = require("gulp-debug"),
    gulp = require("gulp"),
    notify = require("gulp-notify");

gulp.task("scripts:app", function () {
    return gulp.src(...)
        .pipe(debug({ title: "tsc" }))
        .pipe(...);                // <--- if i add notify here, 
                                   //      I will always get a notification
});

gulp.task("watch", function () {
    gulp.watch("ts/**/*.ts", ["scripts:app"]);
});

If I pipe to notify inside the 'scripts:app' task, it will make a notification every time that task runs, regardless of how that task was started. 如果我管notify里面'scripts:app'任务,它会使通知每一个任务运行,不管该任务是如何开始的时间。 Again, I want to notify when the watched task completes. 同样,我想在观察任务完成时通知。

I considered adding a task 'scripts:app:notify' that depends on 'scripts:app' , but if possible I'd like to avoid creating "unnecessary" tasks. 我考虑添加一个任务'scripts:app:notify' ,它取决于'scripts:app' ,但如果可能的话,我想避免创建“不必要的”任务。

I also tried the following: 我也尝试过以下方法:

gulp.watch("ts/**/*.ts", ["scripts:app"])
    .on("change", function (x) { notify('changed!').write(''); });

But that results in a notification for every file changed. 但这导致每个文件的通知都发生了变化。 I want a notification when the task completes . 我想在任务完成时收到通知。

In other words, if I run gulp scripts:app , I should not get a notification. 换句话说,如果我运行gulp scripts:app ,我不应该收到通知。 When I run gulp watch and change a watched file, I should get a notification. 当我运行gulp watch并更改监视文件时,我应该收到通知。

How can I do this? 我怎样才能做到这一点?

Try adding params to your build script: 尝试将params添加到构建脚本中:

function buildApp(notify){
    return gulp.src(...)
        .pipe(...)
        .pipe(function(){
            if (notify) {
                //drop notification
            }
        });
    });      
}

//Register watcher
gulp.watch("ts/**/*.ts", function(){
    var notify = true;
    buildApp(notify);
});

//Register task so we can still call it manually
gulp.task("scripts:app", buildApp.bind(null, false));

As you can see, buildApp is a simple function. 如您所见, buildApp是一个简单的功能。 It's callable through a watcher or a "normal" task registration. 它可以通过观察者或“正常”任务注册来调用。

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

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