var gulp = require('gulp');
var babel = require('gulp-babel');
var jshint = require('gulp-jshint');
var nodemon = require('gulp-nodemon');
var uglify = require('gulp-uglify');
var mocha = require('gulp-mocha');
var webpack = require('webpack-stream');

gulp.task('build', ['build-client', 'build-server', 'test']);

gulp.task('test', ['lint-client', 'lint-server'], function () {
    gulp.src(['test/**/*.js'])
        .pipe(mocha());
});

gulp.task('lint-client', function () {
  return gulp.src('client/js/*.js')
    .pipe(jshint())
    .pipe(jshint.reporter('default', { verbose: true}));
});

gulp.task('build-client', ['lint-client', 'move-client'], function () {
  return gulp.src(['client/js/app.js'])
    .pipe(webpack(require('./webpack.config.js')))
    //.pipe(uglify())
    .pipe(gulp.dest('bin/client/js/'));
});

gulp.task('move-client', function () {
  return gulp.src(['client/**/*.*', '!client/js/*.js'])
    .pipe(gulp.dest('./bin/client/'));
});

gulp.task('lint-server', function () {
  return gulp.src('server/*.js')
    .pipe(jshint())
    .pipe(jshint.reporter('default', { verbose: true}));
});

gulp.task('build-server', ['lint-server', 'move-server'], function () {
  return gulp.src('server/*.js')
    .pipe(gulp.dest('bin/server/'));
});

gulp.task('move-server', function () {
  return gulp.src(['server/**/*.*', 'server/**/*.js'])
    .pipe(gulp.dest('./bin/server/'));
});

gulp.task('watch', ["build"], function () {
  gulp.watch('client/**/*.*', ['build-client', 'move-client']);
  gulp.watch('server/*.*', 'server/**/*.js', ['build-server']);
  gulp.start("run");
});

gulp.task('run', ["build"], function () {
    nodemon({
        delay: 10,
        script: 'server/server.js',
        cwd: "./bin/",
        args: ["/server/config.json"],
        ext: 'html js css'
    })
    .on('restart', function () {
        console.log('restarted!');
    });
});

var gulp = require('gulp');
var babel = require('gulp-babel');
var jshint = require('gulp-jshint');
var nodemon = require('gulp-nodemon');
var uglify = require('gulp-uglify');
var mocha = require('gulp-mocha');
var webpack = require('webpack-stream');

gulp.task('build', ['build-client', 'build-server', 'test']);

gulp.task('test', ['lint'], function () {
    gulp.src(['test/**/*.js'])
        .pipe(mocha());
});

gulp.task('lint', function () {
  return gulp.src(['**/*.js', '!node_modules/**/*.js', '!bin/**/*.js'])
    .pipe(jshint())
    .pipe(jshint.reporter('default', { verbose: true}))
    .pipe(jshint.reporter('fail'));
});

gulp.task('build-client', ['lint', 'move-client'], function () {
  return gulp.src(['client/js/app.js'])
    .pipe(webpack(require('./webpack.config.js')))
    //.pipe(uglify())
    .pipe(gulp.dest('bin/client/js/'));
});

gulp.task('move-client', function () {
  return gulp.src(['client/**/*.*', '!client/js/*.js'])
    .pipe(gulp.dest('./bin/client/'));
});


gulp.task('build-server', ['lint', 'move-server'], function () {
  return gulp.src('server/*.js')
    .pipe(gulp.dest('bin/server/'));
});

gulp.task('move-server', function () {
  return gulp.src(['server/**/*.*', 'server/**/*.js'])
    .pipe(gulp.dest('./bin/server/'));
});

gulp.task('watch', ["build"], function () {
  gulp.watch('client/**/*.*', ['build-client', 'move-client']);
  gulp.watch('server/*.*', 'server/**/*.js', ['build-server']);
  gulp.start("run");
});

gulp.task('run', ["build"], function () {
    nodemon({
        delay: 10,
        script: 'server/server.js',
        cwd: "./bin/",
        args: ["/server/config.json"],
        ext: 'html js css'
    })
    .on('restart', function () {
        console.log('restarted!');
    });
});

Unify lint-client and lint-server to lint.