/* global require, __filename, __dirname */

var gulp = require('gulp'),
  concat = require('gulp-concat'),
  uglify = require('gulp-uglify'),
  connect = require('gulp-connect'),
  rename = require('gulp-rename'),
  jscs = require('gulp-jscs'),
  jshint = require('gulp-jshint'),
  open = require('gulp-open'),
  watch = require('gulp-watch'),
  batch = require('gulp-batch'),
  del = require('del'),
  Server = require('karma').Server;

var APPLICATION_DIRECTORY = 'app/src',
  APPLICATION_NAME = 'angular-confirm-field',
  CONNECTION_PORT = 8888,
  PACKAGE_DESTINATION = 'app/package/js';

var paths = {
  packageScripts: [
    APPLICATION_DIRECTORY + '/js/**/*.js',
    '!' + APPLICATION_DIRECTORY + '/js/**/*.spec.js'
  ],
  lintingScripts: [
    '*.js',
    APPLICATION_DIRECTORY + '/js/**/*.js',
    APPLICATION_DIRECTORY + '/js/**/*.spec.js'
  ]
};

gulp.task('test', ['jscs', 'jshint', 'unit']);

gulp.task('jscs', function () {
  return gulp.src(paths.lintingScripts)
    .pipe(jscs())
    .pipe(jscs.reporter());
});

gulp.task('jshint', function () {
  return gulp.src(paths.lintingScripts)
    .pipe(jshint())
    .pipe(jshint.reporter('default'));
});

gulp.task('connect', function () {
  connect.server({
    root: APPLICATION_DIRECTORY,
    port: 8888
  });
});

gulp.task('open', function () {
  var options = {
    uri: 'http://localhost:' + CONNECTION_PORT
  };
  gulp.src(__filename)
    .pipe(open(options));
});

gulp.task('unit', function (done) {
  new Server({
    configFile: __dirname + '/karma-unit.conf.js',
    singleRun: true
  }, function () {
    done();
  }).start();
});

gulp.task('clean', function () {
  return del([PACKAGE_DESTINATION]);
});

gulp.task('package', ['clean'], function () {
  // Minify and copy all JavaScript (except vendor scripts)
  // with sourcemaps all the way down
  return gulp.src(paths.packageScripts)
  // This will output the non-minified version
    .pipe(concat(APPLICATION_NAME + '.js'))
    .pipe(gulp.dest(PACKAGE_DESTINATION))
    .pipe(uglify())
    .pipe(rename({ extname: '.min.js' }))
  // This will minify and rename to *.min.js
    .pipe(gulp.dest(PACKAGE_DESTINATION));
});

gulp.task('watch', ['test'], function () {
  watch(paths.lintingScripts, batch(function (events, done) {
    gulp.start('test', done);
  }));
});

gulp.task('default', ['jscs', 'jshint', 'connect', 'open']);

@@ -477,135 +477,199 @@
 ,%0A  
-APPLICATION_NAME = 'angular-confirm-field',%0A  CONNECTION_PORT = 8888,%0A  PACKAGE_DESTINATION = 'app/package/js';%0A%0Avar paths = %7B%0A
+CONNECTION_PORT = 8888;%0A%0Avar config = %7B%0A  applicationName: 'angular-confirm-field',%0A  paths: %7B%0A    karmaConfig: __dirname + '/karma-unit.conf.js',%0A    packageDestination: 'app/package/js',%0A  
   pa
@@ -684,24 +684,26 @@
 ipts: %5B%0A    
+  
 APPLICATION_
@@ -725,24 +725,26 @@
 s/**/*.js',%0A
+  
     '!' + AP
@@ -788,13 +788,17 @@
 js'%0A
+  
   %5D,%0A
+  
   li
@@ -817,16 +817,18 @@
 : %5B%0A    
+  
 '*.js',%0A
@@ -823,32 +823,34 @@
     '*.js',%0A    
+ 
+ 
 APPLICATION_DIRE
@@ -868,24 +868,26 @@
 s/**/*.js',%0A
+  
     APPLICAT
@@ -925,12 +925,143 @@
 js'%0A
+  
   %5D%0A
+  %7D,%0A  connect: %7B%0A    root: APPLICATION_DIRECTORY,%0A    port: CONNECTION_PORT,%0A    uri: 'http://localhost:' + CONNECTION_PORT%0A  %7D%0A
 %7D;%0A%0A
@@ -1150,32 +1150,39 @@
 return gulp.src(
+config.
 paths.lintingScr
@@ -1286,24 +1286,31 @@
 rn gulp.src(
+config.
 paths.lintin
@@ -1451,45 +1451,58 @@
 ot: 
-APPLICATION_DIRECTORY,%0A    port: 8888
+config.connect.root,%0A    port: config.connect.port
 %0A  %7D
@@ -1572,45 +1572,26 @@
 ri: 
-'http://localhost:' + CONNECTION_PORT
+config.connect.uri
 %0A  %7D
@@ -1717,41 +1717,32 @@
 le: 
-__dirname + '/karma-unit.conf.js'
+config.paths.karmaConfig
 ,%0A  
@@ -1859,29 +1859,39 @@
 del(
-%5BPACKAGE_DESTINATION%5D
+config.paths.packageDestination
 );%0A%7D
@@ -2056,16 +2056,23 @@
 ulp.src(
+config.
 paths.pa
@@ -2153,24 +2153,30 @@
 cat(
-APPLICATION_NAME
+config.applicationName
  + '
@@ -2202,35 +2202,47 @@
 lp.dest(
-PACKAGE_DESTINATION
+config.paths.packageDestination
 ))%0A    .
@@ -2367,27 +2367,39 @@
 est(
-PACKAGE_DESTINATION
+config.paths.packageDestination
 ));%0A
@@ -2454,16 +2454,23 @@
   watch(
+config.
 paths.li

Tidy up Gulp config.