/*jslint node: true */
'use strict';
var gulp = require('gulp');
var $ = require('gulp-load-plugins')();
var browserify = require('browserify');
var watchify = require('watchify');
var buffer = require('vinyl-buffer');
var source = require('vinyl-source-stream');
var sequence = require('run-sequence');
var browserSync = require('browser-sync');

var config = {
  fileSaver: {
    scripts: './src/*.js',
  },
  docs: {
    src: './docs/src',
    index: './docs/index.html',
    styles: './docs/assets/stylesheets/**/*.css',
    dest: './docs/dist'
  },
  browserSync: {
    port: '3000',
    server: './docs'
  },
  // Predefined browserify configs to keep tasks DRY
  browserify: {
    fileSaver: {
      type: 'dragular',
      entryPoint: './src/angular-file-saver.js',
      bundleName: 'angular-file-saver.js',
      dest: './dist',
    },
    docs: {
      type: 'docs',
      entryPoint: './docs/assets/js/custom.js',
      bundleName: 'examples.js',
      dest: './docs/dist'
    }
  },
  // A flag attribute to switch modes.
  isProd: false
};

var browserifyDefaults = config.browserify.fileSaver;

function handleErrors(err) {
  $.util.log(err.toString());
  this.emit('end');
}

/*
* See http://blog.avisi.nl/2014/04/25/how-to-keep-a-fast-build-with-browserify-and-reactjs/
*/
function buildScript() {

  var bundler = browserify({
    entries: browserifyDefaults.entryPoint,
    debug: false,
    cache: {},
    packageCache: {},
    fullPaths: false
  }, watchify.args);

  // Watch files for changes and only rebuilds what it needs to
  if (!config.isProd) {
    bundler = watchify(bundler);
    bundler.on('update', function() {
      rebundle();
    });
  }

  function rebundle() {
    var stream = bundler.bundle();

    return stream.on('error', handleErrors)
      .pipe(source(browserifyDefaults.bundleName))
      .pipe(buffer())
      .pipe(gulp.dest(browserifyDefaults.dest))
      .pipe($.if(config.isProd, $.uglify({
        compress: { drop_console: true }
      })))
      .pipe($.if(config.isProd, $.rename({
        suffix: '.min'
      })))
      .pipe($.size({
        title: 'Scripts: '
      }))
      .pipe(gulp.dest(browserifyDefaults.dest))
      .pipe($.if(browserSync.active, browserSync.stream()));
  }

  return rebundle();
}

gulp.task('browserify', function() {
  return buildScript();
});

gulp.task('styles:docs', function() {

  return gulp.src([
    config.docs.styles
  ])
  .pipe($.concat('examples.css'))
  .pipe(gulp.dest(config.docs.dest))
  .pipe($.if(browserSync.active, browserSync.stream()));
});

gulp.task('serve', function () {

  browserSync({
    port: config.browserSync.port,
    server: {
      baseDir: config.browserSync.server,
    },
    logConnections: true,
    logFileChanges: true,
    notify: true
  });
});

gulp.task('deploy', function() {
  return gulp.src('./docs/**/*')
    .pipe($.ghPages());
});

gulp.task('bump', function() {
  return gulp.src('./*.json')
    .pipe($.bump({type: 'minor'}))
    .pipe(gulp.dest('./'));
});

gulp.task('build', function() {
  config.isProd = true;
  browserifyDefaults = config.browserify.fileSaver;

  sequence(['browserify']);
});

gulp.task('build:docs', function() {
  config.isProd = true;
  browserifyDefaults = config.browserify.docs;

  sequence(['browserify', 'styles:docs']);
});

gulp.task('dev:docs', function() {
  config.isProd = false;
  browserifyDefaults = config.browserify.docs;

  sequence(['browserify', 'styles:docs'], 'watch:docs');
});

gulp.task('watch:docs', ['serve'], function() {
  gulp.watch(config.docs.styles,  ['styles:docs']);
});

gulp.task('default', ['build']);

@@ -339,16 +339,40 @@
 -sync');
+%0Avar fs = require('fs');
 %0A%0Avar co
@@ -1209,16 +1209,328 @@
 d');%0A%7D%0A%0A
+function getUpdateType() %7B%0A  var env = $.util.env;%0A%0A  if (env.type) %7B%0A    return %7B type: env.type %7D;%0A  %7D%0A%0A  if (env.version) %7B%0A    return %7B version: env.version %7D;%0A  %7D%0A%0A  return %7B type: 'patch' %7D;%0A%7D%0A%0Afunction getPackageJsonVersion() %7B%0A    return JSON.parse(fs.readFileSync('./package.json', 'utf8')).version;%0A%7D%0A%0A
 /*%0A* See
@@ -3130,16 +3130,21 @@
 ('deploy
+:docs
 ', funct
@@ -3217,137 +3217,8 @@
 );%0A%0A
-gulp.task('bump', function() %7B%0A  return gulp.src('./*.json')%0A    .pipe($.bump(%7Btype: 'minor'%7D))%0A    .pipe(gulp.dest('./'));%0A%7D);%0A%0A
 gulp
@@ -3730,16 +3730,16 @@
 ion() %7B%0A
-
   gulp.w
@@ -3779,32 +3779,162 @@
 s:docs'%5D);%0A%7D);%0A%0A
+gulp.task('bump', function() %7B%0A%0A  return gulp.src('./*.json')%0A    .pipe($.bump(getUpdateType()))%0A    .pipe(gulp.dest('./'));%0A%7D);%0A%0A
 gulp.task('defau

Add ability to pass arguments from CLI