'use strict'

const fs = require('fs')
const path = require('path')

const del = require('del')
const gitRevSync = require('git-rev-sync')
const ghpages = require('gh-pages')
const gulp = require('gulp')
const $ = require('gulp-load-plugins')()

const pkg = require('./package.json')

let paths = {
  src: 'app',
  dist: 'public'
}

paths = Object.assign(paths, {
  html: `${paths.dist}/**/*.html`,
  images: `${paths.dist}/**/*.{gif,jpg,png}`,
  scripts: `${paths.src}/**/*.js`,
  styles: `${paths.src}/**/*.scss`
})

gulp.task('default', [
  'lint',
  'watch'
])

gulp.task('lint', [
  'sass-lint',
  'standard'
])

gulp.task('minify', [
  'htmlmin',
  'imagemin'
])

gulp.task('watch', [
  'watch:html',
  'watch:scripts',
  'watch:styles'
])

gulp.task('clean', () => (del(paths.dist)))

gulp.task('htmlhint', () => {
  return gulp.src(paths.html)
    .pipe($.htmlhint())
    .pipe($.htmlhint.failReporter())
})

gulp.task('standard', () => {
  return gulp.src(paths.scripts)
    .pipe($.standard())
    .pipe($.standard.reporter('default', {
      breakOnError: true
    }))
})

gulp.task('sass-lint', () => {
  return gulp.src(paths.styles)
    .pipe($.sassLint())
    .pipe($.sassLint.format())
    .pipe($.sassLint.failOnError())
})

gulp.task('watch:html', () => {
  return gulp.src(paths.html)
    .pipe($.watch(paths.html))
    .pipe($.plumber())
    .pipe($.htmlhint())
    .pipe($.htmlhint.reporter())
})

gulp.task('watch:scripts', () => {
  return gulp.src(paths.scripts)
    .pipe($.watch(paths.scripts))
    .pipe($.plumber())
    .pipe($.standard())
    .pipe($.standard.reporter('default'))
})

gulp.task('watch:styles', () => {
  return gulp.src(paths.styles)
    .pipe($.watch(paths.styles))
    .pipe($.plumber())
    .pipe($.sassLint())
    .pipe($.sassLint.format())
})

gulp.task('imagemin', () => {
  return gulp.src(paths.images)
    .pipe($.imagemin())
    .pipe(gulp.dest(paths.dist))
})

gulp.task('htmlmin', () => {
  return gulp.src(paths.html)
    .pipe($.htmlmin({
      collapseBooleanAttributes: true,
      collapseWhitespace: true,
      preserveLineBreaks: true,
      removeComments: true,
      removeScriptTypeAttributes: true,
      removeStyleLinkTypeAttributes: true,
      minifyCSS: true,
      minifyJS: true
    }))
    .pipe(gulp.dest(paths.dist))
})

gulp.task('deploy', (done) => {
  fs.openSync(path.join(paths.dist, '.nojekyll'), 'w')

  return ghpages.publish(paths.dist, {
    clone: '.deploy',
    depth: 2,
    dotfiles: true,
    message: `Deploy ${gitRevSync.short()} from v${pkg.version} [ci skip]`,
    repo: process.env.DEPLOY_REPO || `git@github.com:${pkg.repository}.git`,
    branch: process.env.DEPLOY_BRANCH || 'gh-pages',
    logger: (message) => {
      console.log(`[ deploy ] ${message}`)
    },
    user: {
      name: process.env.DEPLOY_NAME || pkg.author.name,
      email: process.env.DEPLOY_EMAIL || pkg.author.email
    }
  }, done)
})

@@ -809,35 +809,28 @@
 int', () =%3E 
-%7B
+(
 %0A  
-return 
 gulp.src(pat
@@ -891,33 +891,33 @@
 failReporter())%0A
-%7D
+)
 )%0A%0Agulp.task('st
@@ -927,35 +927,28 @@
 ard', () =%3E 
-%7B
+(
 %0A  
-return 
 gulp.src(pat
@@ -1059,17 +1059,17 @@
     %7D))%0A
-%7D
+)
 )%0A%0Agulp.
@@ -1088,35 +1088,28 @@
 int', () =%3E 
-%7B
+(
 %0A  
-return 
 gulp.src(pat
@@ -1206,25 +1206,25 @@
 lOnError())%0A
-%7D
+)
 )%0A%0Agulp.task
@@ -1240,35 +1240,28 @@
 tml', () =%3E 
-%7B
+(
 %0A  
-return 
 gulp.src(pat
@@ -1376,25 +1376,25 @@
 reporter())%0A
-%7D
+)
 )%0A%0Agulp.task
@@ -1413,35 +1413,28 @@
 pts', () =%3E 
-%7B
+(
 %0A  
-return 
 gulp.src(pat
@@ -1568,17 +1568,17 @@
 ault'))%0A
-%7D
+)
 )%0A%0Agulp.
@@ -1600,35 +1600,28 @@
 les', () =%3E 
-%7B
+(
 %0A  
-return 
 gulp.src(pat
@@ -1742,17 +1742,17 @@
 rmat())%0A
-%7D
+)
 )%0A%0Agulp.
@@ -1770,35 +1770,28 @@
 min', () =%3E 
-%7B
+(
 %0A  
-return 
 gulp.src(pat
@@ -1850,33 +1850,33 @@
 st(paths.dist))%0A
-%7D
+)
 )%0A%0Agulp.task('ht
@@ -1893,19 +1893,12 @@
  =%3E 
-%7B
+(
 %0A  
-return 
 gulp
@@ -2235,17 +2235,17 @@
 .dist))%0A
-%7D
+)
 )%0A%0Agulp.

Use implicit return in gulpfile