var gulp = require("gulp"),
  jshint = require("gulp-jshint"),
  concat = require("gulp-concat"),
  uglify = require("gulp-uglify"),
  rename = require("gulp-rename"),
  wrap   = require("gulp-wrap"),
  filter = require("gulp-filter");

/* Lint the file, this should be done since I'm using
 * an editor linter but it never hurts to double check. */
gulp.task("lint", function() {
  /* Ignore jquery ajax */
  var ignore = filter([ '*', '!jquery.js' ]);
  return gulp.src("public/javascripts/src/*.js")
    .pipe(ignore)
    .pipe(jshint())
    .pipe(jshint.reporter("default"));
});

/* Concatenate and uglify */
gulp.task("uglycat", function() {
  return gulp.src("public/javascripts/src/*.js")
    .pipe(wrap('(function(){\n"use strict";\n<%= contents %>})();'))
    .pipe(concat("redgrid.js"))
    .pipe(gulp.dest("public/javascripts/dist"))
    .pipe(rename("redgrid.min.js"))
    .pipe(uglify())
    .pipe(gulp.dest("public/javascripts/dist"));
});

gulp.task("watch", function() {
  gulp.watch("public/javascripts/src/*.js", [ "lint", "uglycat" ]);
});

gulp.task("default", [ "lint", "uglycat" ]);
