var gulp = require('gulp');
var taskListing = require('gulp-task-listing');
var bower = require('gulp-bower');
var tsd = require('gulp-tsd');
var typescript = require('gulp-typescript');
var electron = require('electron-connect').server.create();
var mocha = require('gulp-mocha');

gulp.task('help', taskListing);

gulp.task('build', ['build:clone',
                    'build:typescript']);

gulp.task('build:typescript', function(){
  gulp.src(['src/**/*.ts'])
    .pipe(typescript({ target: 'es5',
                       module: 'commonjs',
                       sourceMap: true }))
    .js
    .pipe(gulp.dest('build'));
});

gulp.task('build:clone', function() {
  return gulp.src(
    ['src/**/*.{html,css,js}'],
    { base: 'src' }
  )
  .pipe(gulp.dest('build'));
});

gulp.task('watch', ['watch:typescript', 'watch:clone']);

gulp.task('watch:typescript', function(){
  gulp.watch('src/**/*.ts', ['build:typescript']);
});

gulp.task('watch:clone', function(){
  gulp.watch('src/**/*.{html,css}', ['build:clone']);
});

gulp.task('build:test', ['build'], function() {
  gulp.src(['test/**/*.ts'])
    .pipe(typescript({ target: 'es5',
                       module: 'commonjs',
                       sourceMap: true }))
    .js
    .pipe(gulp.dest('test-build'));
  return gulp.src(
    ['test/**/*.js', 'test/testdata/*'],
    { base: 'test' }
  )
  .pipe(gulp.dest('test-build'));
});

gulp.task('test', ['build:test'], function() {
  return gulp.src(['test-build/**/*_test.js'], { read: false })
    .pipe(mocha({ reporter: 'dot'}));
});

gulp.task('serve', ['watch'], function () {
  electron.start();

  // Restart electron when resources loaded from BrowserProcess updates
  gulp.watch(['build/browser/**/*.js'], electron.restart);

  // Reload a page when resources loaded from RendererProcess updates
  gulp.watch(['build/renderer/**/*.{html,css.css}'], electron.reload);
});

gulp.task('install', ['install:bower', 'install:tsd']);

gulp.task('install:bower', function() {
  return bower();
});

gulp.task('install:tsd', function(callback) {
  tsd({ command: 'reinstall',
        config: 'tsd.json'
      }, callback);
});

@@ -313,86 +313,168 @@
 );%0A%0A
-gulp.task('build', %5B'build:clone',%0A                    'build:typescript'%5D)
+%0A%5B'browser', 'renderer'%5D.forEach(function(dir)%7B%0A  var typescript_glob = 'src/' + dir + '/**/*.ts';%0A  var static_glob = 'src/' + dir + '/**/*.%7Bhtml,css,js%7D'
 ;%0A%0A
+  
 gulp
@@ -486,27 +486,23 @@
 ('build:
-typescript'
+' + dir
 , functi
@@ -505,24 +505,26 @@
 nction()%7B%0A  
+ 
+ 
 gulp.src(%5B's
@@ -521,29 +521,31 @@
 lp.src(%5B
-'src/**/*.ts'
+typescript_glob
 %5D)%0A    .
@@ -701,107 +701,58 @@
 uild
-'));%0A%7D);%0A%0Agulp.task('build:clone', function() %7B%0A  return gulp.src(%0A    %5B'src/**/*.%7Bhtml,css,js%7D'
+/' + dir));%0A%0A    gulp.src(%0A      %5Bstatic_glob
 %5D,%0A
+  
     
@@ -765,26 +765,30 @@
 : 'src' %7D%0A  
+ 
+ 
 )%0A
+  
   .pipe(gulp
@@ -796,37 +796,41 @@
 dest('build'));%0A
+  
 %7D);%0A%0A
+  
 gulp.task('watch
@@ -833,67 +833,46 @@
 atch
-', %5B'watch:typescript', 'watch:clone'%5D);%0A%0Agulp.task('
+:' + dir, function()%7B%0A    gulp.
 watch
-:
+(%5B
 type
@@ -881,171 +881,170 @@
 ript
-', function()%7B%0A  gulp.watch('src/**/*.ts', %5B'build:typescript'%5D);%0A%7D);%0A%0Agulp.task('watch:clone', function()%7B%0A  gulp.watch('src/**/*.%7Bhtml,css%7D', %5B'build:clone'%5D);%0A%7D
+_glob, static_glob%5D, %5B'build:' + dir%5D);%0A  %7D);%0A%7D);%0A%0Agulp.task('watch', %5B'watch:browser', 'watch:renderer'%5D);%0Agulp.task('build', %5B'build:browser', 'build:renderer'%5D
 );%0A%0A

Fix gulpfile to watch each directories