"use strict";

// Gulp
var gulp = require('gulp');

// Plugins
var gutil = require('gulp-util');
var sass = require('gulp-sass');
var plumber = require('gulp-plumber');
var prefix = require('gulp-autoprefixer');
var minifycss = require('gulp-minify-css');
var uglify = require('gulp-uglify');
var livereload = require('gulp-livereload');
var nodemon = require('gulp-nodemon');
var path = require('path');
var tinylr = require('tiny-lr');
var WritableStream = require('stream').Writable;
var svgSprites = require('gulp-svg-sprites');
var svg = svgSprites.svg;
var png = svgSprites.png;

var outputDir = 'website/dist';
var watching = false;

// Paths
var paths = {
  scripts: ['assets/js/*.js'],
  images: ['assets/img/**'],
  icons: ['assets/icons/*.svg']
};

var devnull = function () {
  var stream = new WritableStream({
    objectMode: true
  });
  stream._write = function (chunk, encoding, callback) {
    callback();
  };
  return stream;
};

var liveReloadifWatching = function () {
  if (watching) {
    return livereload();
  } else {
    return devnull();
  }
};

// Compile Sass
gulp.task('sass', function () {
  gulp.src(['assets/scss/*.scss', '!assets/scss/_variables.scss'])
    .pipe(plumber())
    .pipe(sass({
      sourceComments: 'map'
    }))
    .pipe(prefix(
      "last 1 version", "> 1%", "ie 8", "ie 7"
    ))
    .pipe(gulp.dest(outputDir + '/assets/css'))
    .pipe(minifycss())
    .pipe(gulp.dest(outputDir + '/assets/css'))
    .pipe(liveReloadifWatching());
});

// Uglify JS
gulp.task('uglify', function () {
  gulp.src(paths.scripts)
    .pipe(plumber())
    .pipe(uglify({
      outSourceMap: false
    }))
    .pipe(gulp.dest(outputDir + '/assets/js'))
    .pipe(liveReloadifWatching());
});

// Copy images.
gulp.task('copyimages', function () {
  gulp.src(paths.images)
    .pipe(gulp.dest(outputDir + '/assets/img'));
});

gulp.task('sprites', function () {
  gulp.src(paths.icons)
    .pipe(svg({
      className: ".%f-icon",
      cssFile: "_sprites.scss"        
    }))
    .pipe(gulp.dest('assets/scss'))
    .pipe(png())
    
});

// Watch files
gulp.task('watch', function (event) {
  gulp.watch('assets/scss/*.scss', ['sass']);
  gulp.watch(paths.images, ['copyimages']);
  gulp.watch(paths.icons, ['sprites']);
  gulp.watch(paths.scripts, ['uglify']);

  watching = true;
});

gulp.task('server', function () {
  var productConfig = require('./development-config.json');
  
  if (productConfig['port'] === -1) {
      console.log('You need to have a product config defined');
      process.exit(-1);
  }
      
  nodemon({
    script: 'server.js',
    env: productConfig
  })
    .on('restart', function () {
      console.log('restarted node');
    });
});

gulp.task('azure-exit', ['build'], function (event) {
	process.exit(0);
});


gulp.task('build', ['sprites', 'sass', 'uglify', 'copyimages']);
gulp.task('azure', ['azure-exit']);
gulp.task('default', ['build', 'watch', 'server']);
@@ -614,30 +614,8 @@
 st';
-%0Avar watching = false;
 %0A%0A//
@@ -735,323 +735,8 @@
 %7D;%0A%0A
-var devnull = function () %7B%0A  var stream = new WritableStream(%7B%0A    objectMode: true%0A  %7D);%0A  stream._write = function (chunk, encoding, callback) %7B%0A    callback();%0A  %7D;%0A  return stream;%0A%7D;%0A%0Avar liveReloadifWatching = function () %7B%0A  if (watching) %7B%0A    return livereload();%0A  %7D else %7B%0A    return devnull();%0A  %7D%0A%7D;%0A%0A
 // C
@@ -773,32 +773,39 @@
 function () %7B%0A  
+return 
 gulp.src(%5B'asset
@@ -1122,43 +1122,8 @@
 '))%0A
-    .pipe(liveReloadifWatching());%0A
 %7D);%0A
@@ -1164,32 +1164,39 @@
 function () %7B%0A  
+return 
 gulp.src(paths.s
@@ -1328,43 +1328,8 @@
 '))%0A
-    .pipe(liveReloadifWatching());%0A
 %7D);%0A
@@ -1377,32 +1377,39 @@
 function () %7B%0A  
+return 
 gulp.src(paths.i
@@ -1502,24 +1502,31 @@
 tion () %7B%0A  
+return 
 gulp.src(pat
@@ -1680,21 +1680,16 @@
 (png())%0A
-    %0A
 %7D);%0A%0A// 
@@ -1912,28 +1912,8 @@
 '%5D);
-%0A%0A  watching = true;
 %0A%7D);
@@ -1933,16 +1933,27 @@
 server',
+ %5B'watch'%5D,
  functio
@@ -1951,24 +1951,26 @@
 , function (
+cb
 ) %7B%0A  var pr
@@ -2350,29 +2350,26 @@
 , function (
-event
+cb
 ) %7B%0A%09process
@@ -2382,14 +2382,23 @@
 0);%0A
+%09cb(err);%0A
 %7D);%0A%0A
-%0A
 gulp

Make our dependency tasks async so gulp will give them a chance to finish. Need to remove livereload for now, but can add back later if we need it