import gulp from 'gulp'
import nodemon from 'gulp-nodemon'
import livereload from 'gulp-livereload'
import { watch, bundle } from './tasks/browserify'
import { lessDev, lessDist } from './tasks/less'
import { spawn } from 'child_process'
import { debounce } from 'lodash'
import upload from './tasks/upload'
import updateHeroku from './tasks/updateHeroku'
import loadHerokuEnv from './tasks/loadHerokuEnv'
import rev from 'gulp-rev'
import { exec } from 'shelljs'
import runSequence from 'run-sequence'

// make gulp respond to Ctrl-C
process.once('SIGINT', () => process.exit(0))

gulp.task('watch-js', watch)
gulp.task('build-dev-css', lessDev)

gulp.task('serve', function () {
  nodemon({
    script: './src/server',
    exec: './node_modules/.bin/babel-node',
    ignore: ['public/**/*', 'dist/**/*']
  })
})

function clearScreen () {
  process.stdout.write('\u001b[2J')
  process.stdout.write('\u001b[1;3H')
  process.stdout.write(new Date().toString() + '\n')
}

gulp.task('autotest', function () {
  const argv = require('minimist')(process.argv)
  const file = argv.file || argv.f
  const run = './node_modules/.bin/babel-node ./node_modules/.bin/_mocha -R progress'
  const cmd = file
    ? `${run} -- ${file}`
    : `${run} && ${run} -- test/client/index.js`
  gulp.watch(['src/**/*', 'test/**/*'], debounce(function () {
    clearScreen()
    spawn('bash', ['-c', cmd], {stdio: 'inherit'})
  }, 5000, true))
})

gulp.task('watch', function () {
  require('dotenv').load({silent: true})
  if (process.env.LIVERELOAD) livereload.listen()
  gulp.watch('css/**/*.less', ['build-dev-css'])
})

gulp.task('default', ['watch-js', 'serve', 'watch'])

// ---------------------------------------------------------------------
// deployment tasks
// ---------------------------------------------------------------------

gulp.task('clean-dist', function () {
  return exec('rm -r dist', {silent: true})
})

gulp.task('copy-dist-images', function () {
  return gulp.src('public/img/**/*', {base: 'public'})
  .pipe(rev())
  .pipe(gulp.dest('dist'))
  .pipe(rev.manifest({base: 'dist', path: 'dist/manifest.json', merge: true}))
  .pipe(gulp.dest('dist'))
})

gulp.task('load-heroku-env', loadHerokuEnv)

// build-dist-css depends upon copy-dist-images because it needs to read
// the manifest to rewrite image urls in CSS
gulp.task('build-dist-css', ['copy-dist-images'], lessDist)
gulp.task('build-dist-js', bundle)
gulp.task('upload', upload)
gulp.task('update-heroku', updateHeroku)

gulp.task('build', function (done) {
  runSequence(
    'clean-dist',
    'load-heroku-env',
    ['build-dist-css', 'build-dist-js'],
    done
  )
})

gulp.task('dotenv', () => require('dotenv').load())

gulp.task('build-dev', function (done) {
  runSequence(
    'clean-dist',
    'dotenv',
    ['build-dist-css', 'build-dist-js'],
    done
  )
})

gulp.task('deploy', function (done) {
  runSequence(
    'clean-dist',
    'load-heroku-env',
    ['build-dist-css', 'build-dist-js'],
    'upload',
    'update-heroku',
    done
  )
})

@@ -2496,33 +2496,76 @@
 sk('
-build', function (
+dotenv', () =%3E require('dotenv').load())%0A%0Agulp.task('build', 
 done
-) %7B
+ =%3E
 %0A  r
@@ -2567,37 +2567,32 @@
 %3E%0A  runSequence(
-%0A    
 'clean-dist',%0A  
@@ -2592,37 +2592,9 @@
 st',
-%0A    'load-heroku-env',%0A    %5B
+ 
 'bui
@@ -2626,81 +2626,97 @@
 -js'
-%5D,%0A    done%0A  )%0A%7D)%0A%0Agulp.task('dotenv', () =%3E require('dotenv').load(
+, done))%0A%0Agulp.task('build-prod', done =%3E runSequence('load-heroku-env', 'build', done
 ))%0A
-%0A
 gulp
@@ -2738,128 +2738,52 @@
 v', 
-function (done) %7B%0A  runSequence(%0A    'clean-dist',%0A    'dotenv',%0A    %5B'build-dist-css', 'build-dist-js'%5D,%0A    done%0A  )%0A%7D
+done =%3E runSequence('dotenv', 'build', done)
 )%0A%0Ag
@@ -2805,25 +2805,15 @@
 y', 
-function (
 done
-) %7B
+ =%3E
 %0A  r
@@ -2827,94 +2827,21 @@
 nce(
-%0A    'clean-dist',%0A    'load-heroku-env',%0A    %5B'build-dist-css', 'build-dist-js'%5D,%0A   
+'build-prod',
  'up
@@ -2846,20 +2846,16 @@
 upload',
-%0A   
  'update
@@ -2867,21 +2867,12 @@
 ku',
-%0A   
  done
-%0A  )%0A%7D
+)
 )%0A

refactor build tasks