'use strict';

var gulp   = require('gulp');
var plugins = require('gulp-load-plugins')();

var paths = {
  lint: ['./gulpfile.js', './lib/**/*.js'],
  watch: ['./gulpfile.js', './lib/**', './test/**/*.js', '!test/{temp,temp/**}'],
  tests: ['./test/**/*.js', '!test/{temp,temp/**}']<% if (istanbulModule) { %>,
  source: ['./lib/*.js']<% } %>
};

gulp.task('lint', function () {
  return gulp.src(paths.lint)
    .pipe(plugins.jshint('.jshintrc'))<% if (jscsModule) { %>
    .pipe(plugins.jscs())<% } %>
    .pipe(plugins.jshint.reporter('jshint-stylish'));
});<% if (istanbulModule) { %>

gulp.task('istanbul', function (cb) {
  gulp.src(paths.source)
    .pipe(plugins.istanbul()) // Covering files
    .on('finish', function () {
      gulp.src(paths.tests)
        .pipe(plugins.mocha())
        .pipe(plugins.istanbul.writeReports()) // Creating the reports after tests runned
        .on('finish', function() {
          process.chdir(__dirname);
          cb();
        });
    });
});<% } else { %>

gulp.task('mocha', function () {
  gulp.src(paths.tests, {cwd: __dirname})
    .pipe(mocha({ reporter: 'list' }));
});<% } %><% if (releaseModule) { %>

gulp.task('bump', ['test'], function () {
  var bumpType = process.env.BUMP || 'patch'; // major.minor.patch

  return gulp.src(['./package.json'])
    .pipe(plugins.bump({ type: bumpType }))
    .pipe(gulp.dest('./'));
});<% } %>

gulp.task('watch', function () {
  gulp.run('test');
  gulp.watch(paths.watch, ['test']);
});

gulp.task('default', ['test']);
gulp.task('test', ['lint', <% if (istanbulModule) { %>'istanbul'<% } else { %>'mocha'<% } %>]);
<% if (releaseModule) { %>gulp.task('release', ['bump']);<% } %>

@@ -339,16 +339,213 @@
  %7D %25%3E%0A%7D;
+%0Avar watching = false;%0A%0Afunction onError(err) %7B%0A  console.log(err.toString());%0A  if (watching) %7B%0A    this.emit('end');%0A  %7D else %7B%0A    // if you want to be really specific%0A    process.exit(1);%0A  %7D%0A%7D
 %0A%0Agulp.t
@@ -981,16 +981,37 @@
 .mocha()
+.on('error', onError)
 )%0A      
@@ -1332,16 +1332,37 @@
 list' %7D)
+.on('error', onError)
 );%0A%7D);%3C%25
@@ -1622,24 +1622,24 @@
 %7D);%3C%25 %7D %25%3E%0A%0A
-
 gulp.task('w
@@ -1655,24 +1655,43 @@
 nction () %7B%0A
+  watching = true;%0A
   gulp.run('

Handle gulp-mocha error when `gulp watch`