I want to display a notification when:
1. An error occurs
2. When the script was successfully executed
All notifications are actually working but the success notification is always displayed, even when there is an error.
How can I make sure the success notification is only displayed when no errors have occurred?
This is my current task:
var gulp = require('gulp');
var sass = require('gulp-ruby-sass');
var rename = require('gulp-rename');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');
var minifycss = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');
gulp.task('sass', function(){
return gulp.src('assets/scss/global.scss')
.pipe(plumber({errorHandler: notify.onError("Error: <%= error.message %>")}))
.pipe(sass({ style: 'expanded' }))
.pipe(autoprefixer())
.pipe(gulp.dest('assets/css/'))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest('assets/css/'))
.pipe(notify({
title: 'Gulp',
subtitle: 'success',
message: 'Sass task',
sound: "Pop"
}));
});
gulp.task('watch', function(){
gulp.watch('assets/scss/**/*.scss', ['sass']);
});
gulp.task('default', ['sass', 'watch']);
I encountered the same issue and what worked for me was gulp-if and a custom error handler. Basically, I set errorFree = false
when an error is thrown then at the end when it's time to show the success message I use gulp-if
to determine if notify()
gets called or not.
Working with your gulpfile:
var gulp = require('gulp');
var sass = require('gulp-ruby-sass');
var rename = require('gulp-rename');
var notify = require('gulp-notify');
var plumber = require('gulp-plumber');
var minifycss = require('gulp-minify-css');
var autoprefixer = require('gulp-autoprefixer');
gulp.task('sass', function(){
var onError = function(err) {
notify.onError({
title: "Gulp",
subtitle: "Failure!",
message: "Error: <%= error.message %>",
sound: "Beep"
})(err);
this.emit('end');
};
return gulp.src('assets/scss/global.scss')
.pipe(plumber({errorHandler: onError}))
.pipe(sass({ style: 'expanded' }))
.pipe(autoprefixer())
.pipe(gulp.dest('assets/css/'))
.pipe(rename({suffix: '.min'}))
.pipe(minifycss())
.pipe(gulp.dest('assets/css/'))
.pipe(notify({ // Add gulpif here
title: 'Gulp',
subtitle: 'success',
message: 'Sass task',
sound: "Pop"
}));
});
gulp.task('watch', function(){
gulp.watch('assets/scss/**/*.scss', ['sass']);
});
gulp.task('default', ['sass', 'watch']);
In my gulpfile I actually wrap each step in gulp-if
so that the stream stops processing. (I haven't found a way to stop the stream without killing the process, which defeats the purpose of watch IMO.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With