'use strict';

var gulp = require('gulp');
var del = require('del');


var path = require('path');


// Load plugins
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var watchify = require('watchify');
var source = require('vinyl-source-stream'),
    
    sourceFile = './app/scripts/app.coffee',
    
    destFolder = './dist/scripts',
    destFileName = 'app.js';


// Styles
gulp.task('styles', function () {
    return gulp.src('app/styles/main.scss')
        .pipe($.rubySass({
            style: 'expanded',
            precision: 10,
            loadPath: ['app/bower_components']
        }))
        .pipe($.autoprefixer('last 1 version'))
        .pipe(gulp.dest('dist/styles'))
        .pipe($.size());
});


// Scripts
gulp.task('scripts', function () {
    var bundler = watchify(browserify({
        entries: [sourceFile],
        insertGlobals: true,
        cache: {},
        packageCache: {},
        fullPaths: true
    }));

    bundler.on('update', rebundle);

    function rebundle() {
        return bundler.bundle()
            // log errors if they happen
            .on('error', $.util.log.bind($.util, 'Browserify Error'))
            .pipe(source(destFileName))
            .pipe(gulp.dest(destFolder));
    }

    return rebundle();

});





gulp.task('jade', function () {
    return gulp.src('app/template/*.jade')
        .pipe($.jade({ pretty: true }))
        .pipe(gulp.dest('dist'));
})



// HTML
gulp.task('html', function () {
    return gulp.src('app/*.html')
        .pipe($.useref())
        .pipe(gulp.dest('dist'))
        .pipe($.size());
});

// Images
gulp.task('images', function () {
    return gulp.src('app/images/**/*')
        .pipe($.cache($.imagemin({
            optimizationLevel: 3,
            progressive: true,
            interlaced: true
        })))
        .pipe(gulp.dest('dist/images'))
        .pipe($.size());
});



gulp.task('jest', function () {
    var nodeModules = path.resolve('./node_modules');
    return gulp.src('app/scripts/**/__tests__')
        .pipe($.jest({
            scriptPreprocessor: nodeModules + '/gulp-jest/preprocessor.js',
            unmockedModulePathPatterns: [nodeModules + '/react']
        }));
});



// Clean
gulp.task('clean', function (cb) {
    cb(del.sync(['dist/styles', 'dist/scripts', 'dist/images']));
});


// Bundle
gulp.task('bundle', ['styles', 'scripts', 'bower'], function(){
    return gulp.src('./app/*.html')
               .pipe($.useref.assets())
               .pipe($.useref.restore())
               .pipe($.useref())
               .pipe(gulp.dest('dist'));
});

// Webserver
gulp.task('serve', function () {
    gulp.src('./dist')
        .pipe($.webserver({
            livereload: true,
            port: 9000
        }));
});

// Bower helper
gulp.task('bower', function() {
    gulp.src('app/bower_components/**/*.js', {base: 'app/bower_components'})
        .pipe(gulp.dest('dist/bower_components/'));

});

gulp.task('json', function() {
    gulp.src('app/scripts/json/**/*.json', {base: 'app/scripts'})
        .pipe(gulp.dest('dist/scripts/'));
});

// Robots.txt and favicon.ico
gulp.task('extras', function () {
    return gulp.src(['app/*.txt', 'app/*.ico'])
        .pipe(gulp.dest('dist/'))
        .pipe($.size());
});

// Watch
gulp.task('watch', ['html', 'bundle', 'serve'], function () {

    // Watch .json files
    gulp.watch('app/scripts/**/*.json', ['json']);

    // Watch .html files
    gulp.watch('app/*.html', ['html']);

    
    // Watch .scss files
    gulp.watch('app/styles/**/*.scss', ['styles']);
    


    // Watch .jade files
    gulp.watch('app/template/**/*.jade', ['jade', 'html']);


    // Watch image files
    gulp.watch('app/images/**/*', ['images']);
});

// Build
gulp.task('build', ['html', 'bundle', 'images', 'extras']);

// Default task
gulp.task('default', ['clean', 'build', 'jest' ]);

@@ -62,18 +62,16 @@
 'del');%0A
-%0A%0A
 var path
@@ -95,25 +95,8 @@
 );%0A%0A
-%0A// Load plugins%0A
 var 
@@ -252,21 +252,16 @@
 ream'),%0A
-    %0A
     sour
@@ -297,21 +297,16 @@
 offee',%0A
-    %0A
     dest
@@ -366,19 +366,8 @@
 ';%0A%0A
-%0A// Styles%0A
 gulp
@@ -706,20 +706,8 @@
 );%0A%0A
-%0A// Scripts%0A
 gulp
@@ -1210,17 +1210,16 @@
 ;%0A    %7D%0A
-%0A
     retu
@@ -1237,18 +1237,13 @@
 ();%0A
-%0A
 %7D);%0A%0A
-%0A%0A%0A%0A
 gulp
@@ -1395,18 +1395,8 @@
 %7D)%0A%0A
-%0A%0A// HTML%0A
 gulp
@@ -1550,18 +1550,8 @@
 );%0A%0A
-// Images%0A
 gulp
@@ -1831,18 +1831,16 @@
 );%0A%7D);%0A%0A
-%0A%0A
 gulp.tas
@@ -2151,19 +2151,8 @@
 );%0A%0A
-%0A%0A// Clean%0A
 gulp
@@ -2257,19 +2257,8 @@
 );%0A%0A
-%0A// Bundle%0A
 gulp
@@ -2517,21 +2517,8 @@
 );%0A%0A
-// Webserver%0A
 gulp
@@ -2672,24 +2672,8 @@
 );%0A%0A
-// Bower helper%0A
 gulp
@@ -3160,17 +3160,8 @@
 );%0A%0A
-// Watch%0A
 gulp
@@ -3223,33 +3223,8 @@
  %7B%0A%0A
-    // Watch .json files%0A
     
@@ -3275,33 +3275,8 @@
 );%0A%0A
-    // Watch .html files%0A
     
@@ -3316,38 +3316,8 @@
 );%0A%0A
-    %0A    // Watch .scss files%0A
     
@@ -3368,39 +3368,8 @@
 %5D);%0A
-    %0A%0A%0A    // Watch .jade files
 %0A   
@@ -3430,34 +3430,8 @@
 );%0A%0A
-%0A    // Watch image files%0A
     
@@ -3482,17 +3482,8 @@
 );%0A%0A
-// Build%0A
 gulp
@@ -3543,24 +3543,8 @@
 );%0A%0A
-// Default task%0A
 gulp

Remove gratuitous linespacing and comments