'use strict'

const gulp = require('gulp')
const plumber = require('gulp-plumber')
const sourcemaps = require('gulp-sourcemaps')
const watch = require('gulp-watch')
const gulpif = require('gulp-if')
const jade = require('gulp-jade')
const sass = require('gulp-sass')
const postcss = require('gulp-postcss')
const budo = require('budo')
const argv = require('yargs').argv;
const uglify = require('gulp-uglify')
const rename = require('gulp-rename')
const stream = require('gulp-streamify')
const source = require('vinyl-source-stream')
const browserify = require('browserify')
const babelify  = require('babelify').configure({
    presets: ['es2015', 'react'],
    plugins: ['transform-object-rest-spread']
})

const entry = './source/index.js'
const outfile = 'bundle.js'

const tasks = {
    jade: function jadeTask() {
        return gulp.src([
            './source/jade/**/*.jade',
            '!./source/jade/layouts/**/*.jade',
            '!./source/jade/includes/**/*.jade'
        ])
        .pipe(plumber())
        .pipe(jade({
            pretty: true
        }))
        .pipe(gulp.dest('./app'))
    },
    sass: function sassTask() {
        return gulp.src('./source/sass/global.scss')
            .pipe(plumber())
            .pipe(gulpif(!argv.production, sourcemaps.init()))
                .pipe(sass())
                .pipe(postcss([
                    require('postcss-assets')({
                        loadPaths: ['**'],
                        basePath: './app',
                        cachebuster: true
                    }),
                    require('autoprefixer')({ browsers: ['last 1 version'] }),
                    require('csswring')()
                ]))
            .pipe(gulpif(!argv.production, sourcemaps.write()))
            .pipe(gulp.dest('./app'))
    },
    script: function scriptTask() {
        var bundler = browserify(entry, {
            transform: babelify
        }).bundle()

        return bundler
            .pipe(source('index.js'))
            .pipe(gulpif(argv.production, stream(uglify({
                output: {
                    beautify: argv.beautify ? true : false
                },
                compress: {
                    global_defs: {
                        __DEV__: false
                    }
                }
            }))))
            .pipe(rename(outfile))
            .pipe(gulp.dest('./app'))
    },
    server: function createServer(callback) {
        // dev server
        let server = budo(entry, {
            serve: outfile,
            port: argv.port ? argv.port : 3000,
            live: true,
            dir: './app',
            open: argv.open,
            browserify: {
                transform: babelify
            },
            stream: process.stdout
        }).on('exit', callback)

        // watch files
        watch(['source/sass/**/*.{scss,sass}'], () => gulp.start('sass'))
        watch(['source/jade/**/*.jade'], () => gulp.start('jade'))
    },
}

gulp.task('jade', tasks.jade)
gulp.task('sass', tasks.sass)
gulp.task('script', tasks.script)

gulp.task('watch', ['jade', 'sass'], tasks.server)
gulp.task('bundle', ['jade', 'sass', 'script'])

@@ -806,32 +806,33 @@
 on jadeTask() %7B%0A
+%0A
         return g
@@ -1096,32 +1096,33 @@
 .dest('./app'))%0A
+%0A
     %7D,%0A    sass:
@@ -1136,32 +1136,33 @@
 on sassTask() %7B%0A
+%0A
         return g
@@ -1787,32 +1787,33 @@
 .dest('./app'))%0A
+%0A
     %7D,%0A    scrip
@@ -1835,24 +1835,25 @@
 iptTask() %7B%0A
+%0A
         var 
@@ -2387,24 +2387,25 @@
 t('./app'))%0A
+%0A
     %7D,%0A    s
@@ -2445,16 +2445,17 @@
 back) %7B%0A
+%0A
         
@@ -2878,32 +2878,18 @@
 '%5D, 
-() =%3E gulp.start('
+tasks.
 sass
-')
 )%0A  
@@ -2934,30 +2934,20 @@
  () 
-=%3E gulp.start('
+tasks.
 jade
-')
 )
+%0A
 %0A   

use direct functions