var gulp = require('gulp');
var babel = require('gulp-babel');
var debug = require('gulp-debug');
var cache = require('gulp-cached');
var replace = require('gulp-replace-task');
var electron = require('electron-connect').server.create();
var async = require('async');
var tcpPortUsed = require('tcp-port-used');
var CONFIG = require('./src/config');

var replaceOptions = {
  patterns: [
    {
      match: 'PORT',
      replace: CONFIG.PORT
    }
  ]
};

var isPortUsed = function (port, cb) {
  tcpPortUsed
    .check(port, '127.0.0.1')
    .then(function (inUse) {
      cb(null, inUse);
    }, function (err) {
      cb(err);
  });
};

gulp.task('check-port', function (cb) {
  isPortUsed(CONFIG.PORT, function (err, portInUse) {
    if (err) {
      return cb('An unknown error occured');
    }
    if (portInUse)
      return cb('The port ' + CONFIG.PORT + ' is already in use');
  
    cb();
  });
});

gulp.task('electron:start', ['check-port', 'transpile', 'copy'], function() {
  electron.start();
});

gulp.task('electron:restart', ['transpile', 'copy'], function() {
  // electron.stop();
  electron.restart();
});

gulp.task('electron:reload', ['copy'], electron.reload);

gulp.task('watch', ['electron:start'], function () {
  gulp.watch(['src/**/*.js'], [/*'transpile', 'copy', */'electron:restart']);
  gulp.watch(['src/**/*.{html,css}'], ['electron:reload']);
});

gulp.task('transpile', function (cb) {
  gulp.src(['src/**/*.js'])
    .pipe(cache('transpile'))
    .pipe(debug())
    .pipe(replace(replaceOptions))
    .pipe(babel({
      presets: ['es2015', 'react']
    }))
    .pipe(gulp.dest('dist'))
    .on('end', cb);
});

gulp.task('copy', function (cb) {
  gulp.src(['src/**/*.*', '!src/**/*.js'])
    .pipe(cache('copy'))
    .pipe(debug())
    .pipe(replace(replaceOptions))
    .pipe(gulp.dest('dist'))
    .on('end', cb);
});

gulp.task('default', ['transpile', 'copy', 'check-port', 'electron:start', 'watch']);

@@ -483,17 +483,23 @@
 (port, c
-b
+allback
 ) %7B%0A  tc
@@ -574,17 +574,23 @@
 %0A      c
-b
+allback
 (null, i
@@ -627,17 +627,23 @@
 %0A      c
-b
+allback
 (err);%0A 
@@ -682,25 +682,31 @@
  function (c
-b
+allback
 ) %7B%0A  isPort
@@ -759,26 +759,24 @@
     if (err)
- %7B
 %0A      retur
@@ -778,17 +778,23 @@
 return c
-b
+allback
 ('An unk
@@ -815,22 +815,16 @@
 ured');%0A
-    %7D%0A
     if (
@@ -848,17 +848,23 @@
 return c
-b
+allback
 ('The po
@@ -915,17 +915,23 @@
   %0A    c
-b
+allback
 ();%0A  %7D)
@@ -966,30 +966,16 @@
 tart', %5B
-'check-port', 
 'transpi
@@ -988,30 +988,21 @@
 copy'%5D, 
-function() %7B%0A 
+() =%3E
  electro
@@ -1006,27 +1006,24 @@
 tron.start()
-;%0A%7D
 );%0A%0Agulp.tas
@@ -1029,48 +1029,14 @@
 sk('
-electron:restart', %5B'transpile', 'copy'%5D
+watch'
 , fu
@@ -1045,149 +1045,83 @@
 tion
+ 
 () %7B%0A  
-// electron.stop();%0A  electron.restart();%0A%7D);%0A%0Agulp.task('electron:reload', %5B'copy'%5D, electron.reload);%0A%0Agulp.task('watch
+gulp.watch(%5B'src/**/*.js', '!src/client/*'%5D, %5B'transpile
 ', 
-%5B'
 electron
 :sta
@@ -1120,31 +1120,20 @@
 tron
-:
+.re
 start
-'%5D, function () %7B
+%5D);%0A
 %0A  g
@@ -1152,43 +1152,43 @@
 src/
-**/*.js'%5D, %5B/*'transpile', 
+client/**/*.%7Bhtml,css%7D'%5D, %5B
 'copy', 
 */'e
@@ -1187,19 +1187,16 @@
 y', 
-*/'
 electron
 :res
@@ -1195,17 +1195,15 @@
 tron
-:restart'
+.reload
 %5D);%0A
@@ -1225,37 +1225,48 @@
 src/
-**/*.%7Bhtml,css%7D'%5D, %5B'
+client/**/*.js'%5D, %5B'transpile', 
 electron
 :rel
@@ -1265,16 +1265,15 @@
 tron
-:
+.
 reload
-'
 %5D);%0A
@@ -1303,33 +1303,39 @@
 le', function (c
-b
+allback
 ) %7B%0A  gulp.src(%5B
@@ -1532,33 +1532,39 @@
     .on('end', c
-b
+allback
 );%0A%7D);%0A%0Agulp.tas
@@ -1584,17 +1584,23 @@
 ction (c
-b
+allback
 ) %7B%0A  gu
@@ -1762,17 +1762,23 @@
 'end', c
-b
+allback
 );%0A%7D);%0A%0A

Allow gulp to reload when client js updated