 watch = require 'gulp-watch'
 watchify = require 'watchify'
 
+dirs =
+  dist: './lib/'
+  src: './src/'
+  test: './test/'
+  tmpSrc: './.tmp/src/'
+  tmpTest: './.tmp/test/'
+
 ignoreError = (stream) ->
   stream.on 'error', (e) ->
     gutil.log e

  options =
    cache: {}
    packageCache: {}
    entries: ['./.tmp/src/index.js']
    debug: true
  b = browserify options
  w = watchify b

  run 'build:coffee(dev)', 'build:js(dev)', done

gulp.task 'build:coffee', ->
  gulp.src dirs.src + '**/*.coffee'
  .pipe coffee()
  .pipe gulp.dest dirs.tmpSrc

gulp.task 'build:coffee(dev)', ->
  gulp.src dirs.src + '**/*.coffee'
  .pipe sourcemaps.init()
  .pipe ignoreError coffee()
  .pipe sourcemaps.write()
  .pipe gulp.dest dirs.tmpSrc

gulp.task 'build:js', ->
  b = browserify
    entries: [dirs.tmpSrc + 'index.js']
    debug: false
  b.bundle()
  .pipe source 'bundle.js'
  .pipe buffer()
  .pipe uglify()
  .pipe gulp.dest dirs.dist

gulp.task 'build:js(dev)', ->
  b = browserify
    entries: [dirs.tmpSrc + 'index.js']
    debug: true
  b.bundle()
  .pipe source 'bundle.js'
  .pipe buffer()
  .pipe sourcemaps.init loadMaps: true
  .pipe sourcemaps.write './'
  .pipe gulp.dest dirs.dist

gulp.task 'build-test', ->
  gulp.src dirs.test + '**/*.coffee'
  .pipe sourcemaps.init()
  .pipe coffee()
  .pipe espower()
  .pipe sourcemaps.write()
  .pipe gulp.dest dirs.tmpTest

gulp.task 'build-test(dev)', ->
  gulp.src dirs.test + '**/*.coffee'
  .pipe sourcemaps.init()
  .pipe ignoreError coffee()
  .pipe ignoreError espower()
  .pipe sourcemaps.write()
  .pipe gulp.dest dirs.tmpTest

gulp.task 'clean', (done) ->
  del [
    dirs.tmpSrc
    dirs.tmpTest
    dirs.dist
  ], done
  null

