Search code examples
javascriptnode.jsgulpgulp-notify

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!

EDIT After qballers solution, only issue is my gulp-size plugin returning undefined for file sizes:

const s = gsize();

// Success Message
var notifyGeneric = {
    title: function () {
        return '<%= file.relative %> - ' + s.prettySize;
    },
    onLast: true,
    subtitle: "Successfully Compiled",
    message: "@ Time: <%= options.hour %>:<%= options.minute %>:<%= options.second %> ",
    templateOptions: {
        hour: date.getHours(),
        minute: date.getMinutes(),
        second: date.getSeconds()
    }
};

...

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

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

Solution

  • 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 %> - ' + this.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()
                },
                s: {}
            };
    gulp.task('build-css', function() {
        notifyGeneric.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(notifyGeneric.s)
            .pipe(notify(notifyGeneric))
    });