StuFu StuFu - 3 months ago 26
Node.js Question

Gulp Notify global success function

I'm looking to have gulp-notify success function that is called which I can reuse. I'm using the same format for all of my tasks and would like to clean it up. Here's an example of what I'm doing right now:

gulp.task('build-css', function() {
const s = gsize();

return gulp.src('src/css/main.css')
.pipe(plumber({ errorHandler: onError }))
.pipe(cssmin())
.pipe(rename({suffix: '.min'}))
.pipe(gulp.dest('dist/css'))
.pipe(s)
.pipe(notify({
title: function () {
return '<%= file.relative %> - ' + s.prettySize;
},
onLast: true,
subtitle: "Successfully Compiled",
message: "@ Time: <%= options.hour %>:<%= options.minute %>:<%= options.second %> ",
templateOptions: {
hour: new Date().getHours(),
minute: new Date().getMinutes(),
second: new Date().getSeconds()
}
}))
});


I'm re-using the same notify function with multiple tasks. I've tried doing something like this, but each attempt throws an error. This particular error is with plumber -
Can't Pipe to Undefined


var onSuccess = function () {
const s = gsize();
notify({
title: function () {
return '<%= file.relative %> - ' + s.prettySize;
},
onLast: true,
subtitle: "Successfully Compiled",
message: "@ Time: <%= options.hour %>:<%= options.minute %>:<%= options.second %> ",
templateOptions: {
hour: new Date().getHours(),
minute: new Date().getMinutes(),
second: new Date().getSeconds()
}
})
};

...

gulp.task('build-css', function() {
const s = gsize();

return gulp.src('src/css/main.css')
.pipe(plumber({ errorHandler: onError }))
.pipe(autoprefixer({
browsers: ['last 6 versions'],
cascade: false
}))
.pipe(cssmin())
.pipe(rename({suffix: '.min'}))
.pipe(s)
.pipe(onSuccess())
.pipe(gulp.dest('dist/css'))
.pipe(reload({stream: true}));
});


Any thoughts on how to accomplish this are appreciated!

Answer

Not sure if this the solution you are gunning for but you can just use object literals to save you code duplications.

var notifyGeneric = {
            title: function () {
                return '<%= file.relative %> - ' + s.prettySize;
            },
            onLast: true,
            subtitle: "Successfully Compiled",
            message: "@ Time: <%= options.hour %>:<%= options.minute %>:<%= options.second %> ",
            templateOptions: {
                hour: new Date().getHours(),
                minute: new Date().getMinutes(),
                second: new Date().getSeconds()
            }
        };
gulp.task('build-css', function() {
    const s = gsize();

    return gulp.src('src/css/main.css')
        .pipe(plumber({ errorHandler: onError }))
        .pipe(cssmin())
        .pipe(rename({suffix: '.min'}))
        .pipe(gulp.dest('dist/css'))
        .pipe(s)
        .pipe(notify(notifyGeneric))
});
Comments