// Generated on 2016-02-09 using generator-chrome-extension 0.5.2
require('babel-core/register');

import gulp from 'gulp';
import gulpLoadPlugins from 'gulp-load-plugins';
import del from 'del';
import casperJs from 'gulp-casperjs';
import runSequence from 'run-sequence';
import {stream as wiredep} from 'wiredep';
import jscs from 'gulp-jscs';
import jshint from 'gulp-jshint';
import download from 'gulp-download';
import decompress from 'gulp-decompress';

const $ = gulpLoadPlugins();

gulp.task('extras', () => {
  return gulp.src([
    'app/*.*',
    'app/_locales/**',
    '!app/*.json',
    '!app/*.html',
    '!app/styles.scss',
  ], {
    base: 'app',
    dot: true,
  }).pipe(gulp.dest('dist'));
});

gulp.task('lint', () => {
  return gulp.src([
    'app/scripts/**/*.js',
    'test/**/*.js',
    '!app/scripts/utils/*.js',
  ])
    .pipe(jshint('.jshintrc'))
    .pipe(jshint.reporter('default'));
});

gulp.task('jscs', () => {
  return gulp.src([
    'app/scripts/**/*.js',
    'test/**/*.js',
    '!app/scripts/utils/tld.js',
  ])
    .pipe(jscs('.jscsrc'))
    .pipe(jscs.reporter());
});

gulp.task('images', () => {
  return gulp.src('app/images/**/*')
    .pipe($.if($.if.isFile, $.cache($.imagemin({
      progressive: true,
      interlaced: true,
      // Don't remove IDs from SVGs, they are often used
      // as hooks for embedding and styling
      svgoPlugins: [{cleanupIDs: false}],
    }))
    .on('error', function(err) {
      console.log(err);
      this.end();
    })))
    .pipe(gulp.dest('dist/images'));
});
gulp.task('styles', () => {
  return gulp.src('app/styles.scss/*.scss')
    .pipe($.plumber())
    .pipe($.sass.sync({
      outputStyle: 'expanded',
      precision: 10,
      includePaths: ['.'],
    }).on('error', $.sass.logError))
    .pipe(gulp.dest('app/styles'));
});

gulp.task('html', ['styles'], () => {
  const assets = $.useref.assets({searchPath: ['.tmp', 'app', '.']});

  return gulp.src('app/*.html')
    .pipe(assets)
    .pipe($.sourcemaps.init())
    .pipe($.if('*.js', $.uglify()))
    .pipe($.if('*.css', $.minifyCss({compatibility: '*'})))
    .pipe($.sourcemaps.write())
    .pipe(assets.restore())
    .pipe($.useref())
    .pipe($.if('*.html', $.minifyHtml({conditionals: true, loose: true})))
    .pipe(gulp.dest('dist'));
});

gulp.task('fonts', () => {
  return gulp.src('app/bower_components/font-awesome/fonts/**')
    .pipe(gulp.dest('dist/fonts'));
});

gulp.task('lang', () => {
  return download('https://localise.biz:443/api/export/archive/json.zip?' +
  'key=SfHrKVzhFhxgC1I4dT2r_vRs3Duvw4iu&format=chrome')
      .pipe(decompress({strip: 1}))
      .pipe(gulp.dest('app'));
});

gulp.task('chromeManifest', () => {
  return gulp.src('app/manifest.json')
    .pipe($.chromeManifest({
      background: {
        target: 'scripts/background.js',
      },
    }))
  .pipe($.if('*.css', $.minifyCss({compatibility: '*'})))
  .pipe($.if('*.js', $.sourcemaps.init()))
  .pipe($.if('*.js', $.uglify()))
  .pipe($.if('*.js', $.sourcemaps.write('.')))
  .pipe(gulp.dest('dist'));
});

gulp.task('clean', del.bind(null, ['.tmp', 'dist']));

gulp.task('watch', ['lint', 'html'], () => {
  $.livereload.listen();

  gulp.watch([
    'app/*.html',
    'app/scripts/**/*.js',
    'app/images/**/*',
    'app/styles/**/*',
    'app/_locales/**/*.json',
  ]).on('change', $.livereload.reload);

  gulp.watch('app/scripts/**/*.js', ['lint']);
  gulp.watch('app/styles.scss/**/*.scss', ['styles']);
  gulp.watch('bower.json', ['wiredep']);
});

gulp.task('size', () => {
  return gulp.src('dist/**/*').pipe($.size({title: 'build', gzip: true}));
});

gulp.task('wiredep', () => {
  gulp.src('app/*.html')
    .pipe(wiredep({
      ignorePath: /^(\.\.\/)*\.\./,
    }))
    .pipe(gulp.dest('app'));
});

gulp.task('package', function() {
  var manifest = require('./dist/manifest.json');
  del.sync(['dist/README.txt', 'dist/**/*.map']);
  return gulp.src('dist/**')
      .pipe($.zip('hon kconnect chrome extension-' + manifest.version + '.zip'))
      .pipe(gulp.dest('package'));
});

gulp.task('test', function() {
  runSequence(['lint', 'jscs']);
  gulp.src('test/casper.js')
    .pipe(casperJs({command: 'test --web-security=no'}));
  // Run casperjs test
});

gulp.task('build', (cb) => {
  runSequence(
    ['fonts', 'html', 'lang', 'images', 'extras'],
    'chromeManifest', 'size', cb);
});

gulp.task('chrome-prod', ['clean'], (cb) => {
  runSequence('build', 'package', cb);
});

gulp.task('default', ['clean'], cb => {
  runSequence('test', 'build', cb);
});

@@ -193,46 +193,8 @@
 l';%0A
-import casperJs from 'gulp-casperjs';%0A
 impo
@@ -275,152 +275,8 @@
 ep';
-%0Aimport jscs from 'gulp-jscs';%0Aimport jshint from 'gulp-jshint';%0Aimport download from 'gulp-download';%0Aimport decompress from 'gulp-decompress';
 %0A%0Aco
@@ -660,24 +660,26 @@
 )%0A    .pipe(
+$.
 jshint('.jsh
@@ -697,16 +697,18 @@
   .pipe(
+$.
 jshint.r
@@ -870,24 +870,26 @@
 )%0A    .pipe(
+$.
 jscs('.jscsr
@@ -903,16 +903,18 @@
   .pipe(
+$.
 jscs.rep
@@ -2288,16 +2288,18 @@
  return 
+$.
 download
@@ -2425,16 +2425,18 @@
   .pipe(
+$.
 decompre
@@ -3980,16 +3980,18 @@
   .pipe(
+$.
 casperJs

clean gulp file with gulpLoadPlugins