// Gulp plugins
var gulp = require('gulp');
var imagemin = require('gulp-imagemin');
var livereload = require('gulp-livereload');
var notify = require('gulp-notify');
var sass = require('gulp-ruby-sass');
var sourcemaps = require('gulp-sourcemaps');
var uglify = require('gulp-uglify');

// Source Folders
var imageFolder = 'images';
var jsFolder = 'js';
var mainSassFile = 'main.scss';
var sassFolder = 'scss';

// Build Folders
var buildCssFolder = 'build/css';
var buildImageFolder = 'build/img';
var buildJsFolder = 'build/js';

function handleErrors() {
  console.log('handling error');
  var args = Array.prototype.slice.call(arguments);
  notify.onError({
    title: 'Compile Error',
    message: '<%= error.message %>'
  }).apply(this, args);
  this.emit('end'); // Keep gulp from hanging on this task
}

function buildScript(file, watch) {
  var props = {
    entries: ['./' + jsFolder + '/' + file],
    debug : true,
    transform:  [reactify]
  };

  // watchify() if watch requested, otherwise run browserify() once
  var bundler = watch ? watchify(browserify(props)) : browserify(props);

  function rebundle() {
    var stream = bundler.bundle();
    return stream
      .on('error', handleErrors)
      .pipe(source(file))
      .pipe(buffer())
      //.pipe(uglify())
      .pipe(gulp.dest('./' + buildJsFolder + '/'))
      .pipe(livereload());
  }

  // listen for an update and run rebundle
  bundler.on('update', function() {
    rebundle();
    console.log('Rebundle...');
  });

  // run it once the first time buildScript is called
  return rebundle();
}

/**
 * Compiles SCSS to CSS and minifies CSS
 */
gulp.task('styles', function () {
 var sassOptions = {
    'sourcemap': true,
    'style': 'compressed'
  };

  return sass('scss/**/*.scss', sassOptions)
    .on('error', function (err) {
        console.error("Error", err.message);
    })
    .pipe(sourcemaps.init({debug: true}))
    .pipe(sourcemaps.write('./', {
      includeContent: true,
      sourceRoot: './'
    }))
    .pipe(gulp.dest('./' + buildCssFolder))
    .pipe(livereload());
});

/**
 * Watches for changes in files and does stuff
 */
gulp.task('watch', function () {
  var server = livereload.listen();
  gulp.watch([jsFolder + '/**/*.js'], ['scripts']);
  gulp.watch([sassFolder + '/**/*.scss'], ['styles']);
  gulp.watch([imageFolder + '/**/*'], ['images']);
});

/**
 * Minifies JS files for production
 */
gulp.task('scripts', function () {
  gulp.src(jsFolder + '/*.js')
    .pipe(uglify())
    .on('error', handleErrors)
    .pipe(gulp.dest(buildJsFolder))
    .pipe(livereload());
});

/**
 * Compresses image files for production
 */
gulp.task('images', function () {
  gulp.src(imageFolder + '/*')
    .on('error', handleErrors)
    .pipe(imagemin())
    .pipe(gulp.dest(buildImageFolder));
})

/**
 * Runs by default
 */
gulp.task('default', ['images', 'scripts', 'styles', 'watch'], function () {});
@@ -556,41 +556,8 @@
 ) %7B%0A
-  console.log('handling error');%0A
   va
@@ -604,16 +604,17 @@
 ments);%0A
+%0A
   notify
@@ -712,16 +712,17 @@
  args);%0A
+%0A
   this.e
@@ -736,420 +736,234 @@
 d');
- // Keep gulp from hanging on this task%0A%7D%0A%0Afunction buildScript(file, watch) %7B%0A  var props = %7B%0A    entries: %5B'./' + jsFolder + '/' + file%5D,%0A    debug : true,%0A    transform:  %5Breactify%5D%0A  %7D;%0A%0A  // watchify() if watch requested, otherwise run browserify() once%0A  var bundler = watch ? watchify(browserify(props)) : browserify(props);%0A%0A  function rebundle() %7B%0A    var stream = bundler.bundle();%0A    return stream%0A  
+%0A%7D%0A%0A/**%0A * Runs by default%0A */%0Agulp.task('default', %5B'images', 'scripts', 'styles', 'watch'%5D, function () %7B%7D);%0A%0A/**%0A * Compresses image files for production%0A */%0Agulp.task('images', function () %7B%0A  gulp.src(imageFolder + '/*')%0A
     
@@ -997,222 +997,133 @@
     
-  
 .pipe(
-source(file))%0A      .pipe(buffer())%0A      //.pipe(uglify())%0A      .pipe(gulp.dest('./' + buildJsFolder + '/'))%0A      .pipe(livereload());%0A  %7D%0A%0A  // listen for an update and run rebundle%0A  bundler.on('update
+imagemin())%0A    .pipe(gulp.dest(buildImageFolder));%0A%7D);%0A%0A/**%0A * Minifies JS files for production%0A */%0Agulp.task('scripts
 ', f
@@ -1133,144 +1133,160 @@
 tion
+ 
 () %7B%0A  
-  rebundle();
+gulp.src(jsFolder + '/*.js')%0A    .pipe(uglify())
 %0A    
-console.log('Rebundle...');%0A  %7D);%0A%0A  // run it once the first time buildScript is called%0A  return rebundle
+.on('error', handleErrors)%0A    .pipe(gulp.dest(buildJsFolder))%0A    .pipe(livereload
 ()
+)
 ;%0A%7D
+);
 %0A%0A/*
@@ -1508,75 +1508,20 @@
 r', 
-function (err) %7B%0A        console.error(%22Error%22, err.message);%0A    %7D
+handleErrors
 )%0A  
@@ -2018,549 +2018,4 @@
 %7D);%0A
-%0A/**%0A * Minifies JS files for production%0A */%0Agulp.task('scripts', function () %7B%0A  gulp.src(jsFolder + '/*.js')%0A    .pipe(uglify())%0A    .on('error', handleErrors)%0A    .pipe(gulp.dest(buildJsFolder))%0A    .pipe(livereload());%0A%7D);%0A%0A/**%0A * Compresses image files for production%0A */%0Agulp.task('images', function () %7B%0A  gulp.src(imageFolder + '/*')%0A    .on('error', handleErrors)%0A    .pipe(imagemin())%0A    .pipe(gulp.dest(buildImageFolder));%0A%7D)%0A%0A/**%0A * Runs by default%0A */%0Agulp.task('default', %5B'images', 'scripts', 'styles', 'watch'%5D, function () %7B%7D);

reorder code for sense