"use strict";

import gulp from 'gulp';
import sass from 'gulp-sass';
import browserSync from 'browser-sync';
import cleanCSS from 'gulp-clean-css';
import htmltidy from 'gulp-htmltidy';
import rmEmptyLines from 'gulp-remove-empty-lines';
import uglify from 'gulp-uglify';
import child_process from 'child_process';

const exec = child_process.exec;
const paths = {
  build: '_site',
  css: 'css',
  sass: ['_sass'],
  scripts: ['js']
};

const sassFiles = [
  'css/**/*',
  '_sass/**/*'
]

const jsFiles = [
  'js/**/*.js'
]

const jekyllFiles = [
  '*.{html,yml,md}',
  '_posts/*.{markdown,md}',
  '_layouts/**/*.html',
  '_includes/**/*.html'
];

const messages = {
  jekyllBuild: '<span style="color: grey">Running:</span> $ jekyll build'
};

const errorHandler = error => {
  console.error(String(error));
  this.emit('end');
  browserSync.notify('Error');
}

gulp.task('jekyll-build', done => {
  exec('bundle exec jekyll build', (err, stdout, stderr) => {
    console.log(stdout);
    console.log(stderr);
    done(err);
  });
});

gulp.task('js', () => {
  return gulp.src(jsFiles)
    .pipe(gulp.dest(paths.build + '/' + paths.scripts))
    .pipe(browserSync.reload({
      stream: true
    }));
});

gulp.task('sass', () => {
  return gulp.src(paths.sass + '/*.{sass,scss}')
    .pipe(sass({
       includePaths: ['node_modules']
    }).on('error', errorHandler))
    // .pipe(cleanCSS({
    //   debug: true,
    //   keepBreaks: true,
    //   keepSpecialComments: false
    // }, function(details) {
    //   console.log(details.name + ': ' + details.stats.originalSize + ' -> ' + details.stats.minifiedSize);
    // }) )
    .pipe(gulp.dest(paths.build + '/' + paths.css))
});

gulp.task('htmltidy', gulp.series('jekyll-build', () => {
  return gulp.src([paths.build + '/**/*.html'])
    .pipe(htmltidy({doctype: 'html5',
      hideComments: true,
      indent: true}))
    .pipe(rmEmptyLines())
    .pipe(gulp.dest(paths.build));
}));

gulp.task('minify', () => {
  return gulp.src([paths.build + '/' + paths.css + '/*.css'])
    .pipe(
      cleanCSS({
        debug: true,
        keepBreaks: true,
        keepSpecialComments: false
      }, function(details) {
        console.log(details.name + ': ' + details.stats.originalSize + ' ==> ' + details.stats.minifiedSize);
      }))
    .pipe(gulp.dest(paths.build + '/' + paths.css))
});

gulp.task('serve', gulp.series(gulp.parallel('js', 'minify','jekyll-build'), done => {

  browserSync.init({
    server: {
      baseDir: paths.build,
    },
    notify: false
  });

  gulp.watch(jekyllFiles).on('all', gulp.series('jekyll-build'));
  gulp.watch(sassFiles).on('change', gulp.series('jekyll-build'));
  gulp.watch(jsFiles).on('change', gulp.series('js'));
  gulp.watch(paths.build).on('all', browserSync.reload);
  return console.log('Initializing Server...'), done();

}));

gulp.task('travis', gulp.series(gulp.parallel('jekyll-build', 'js', 'htmltidy', 'minify'), done => {
  console.log('complete'), done();
}));

gulp.task('default', gulp.series('serve'));
@@ -1220,16 +1220,44 @@
 ('sass',
+ gulp.series('jekyll-build',
  () =%3E %7B
@@ -1249,32 +1249,32 @@
 build', () =%3E %7B%0A
-
   return gulp.sr
@@ -1399,269 +1399,8 @@
 r))%0A
-    // .pipe(cleanCSS(%7B%0A    //   debug: true,%0A    //   keepBreaks: true,%0A    //   keepSpecialComments: false%0A    // %7D, function(details) %7B%0A    //   console.log(details.name + ': ' + details.stats.originalSize + ' -%3E ' + details.stats.minifiedSize);%0A    // %7D) )%0A
     
@@ -1441,32 +1441,33 @@
 + paths.css))%0A%7D)
+)
 ;%0A%0Agulp.task('ht
@@ -2174,29 +2174,12 @@
 ', '
-minify','jekyll-build
+sass
 '), 
@@ -2396,36 +2396,28 @@
 ulp.series('
-jekyll-build
+sass
 '));%0A  gulp.
@@ -2631,28 +2631,20 @@
 rallel('
-jekyll-build
+sass
 ', 'js',

Update sass task