gulp       = require 'gulp'
gutil      = require 'gulp-util'
webpack    = require 'webpack'
path       = require 'path'

# gulp.task 'copy', ->
#   gulp.src 'src/index.html'
#     .pipe gulp.dest 'dist'
#   gulp.src 'src/assets/**/*.*'
#     .pipe gulp.dest 'dist/assets'

# gulp.task 'sass', ->
#   gulp.src './src/sass/**/*.scss'
#     .pipe sass()
#     .pipe concat 'main.css'
#     .pipe gulp.dest('./dist/css/')

gulp.task 'lib', (callback) ->
  webpack({
    entry:
      './src/DatePicker.cjsx'
    target: 'node'
    output:
      path: path.join(__dirname, "lib"),
      filename: 'bundle.js'
      libraryTarget: 'umd'
    resolveLoader:
      modulesDirectories: ['node_modules']
    resolve:
      extensions: ['', '.js', '.cjsx', '.coffee']
    externals: [
      'react'
      'moment'
      'radium'
    ]
    module:
      loaders: [
        {test: /\.cjsx$/, loaders: ['coffee-loader', 'cjsx-loader']},
        {test: /\.coffee$/, loader: 'coffee-loader'}
      ]
    debug: true
    devtool:
      'inline-source-map'
    },
    (err, stats) ->
      if err then throw new gutil.PluginError("webpack", err)
      gutil.log "[webpack]", stats.toString()
      callback()
    )

# transform example files.
gulp.task 'app', (callback) ->
  webpack({
    entry:
      './src/examples/app.cjsx'
    output:
      path: path.join(__dirname, "examples/js"),
      filename: 'app.js'
    resolveLoader:
      modulesDirectories: ['node_modules']
    resolve:
      extensions: ['', '.js', '.cjsx', '.coffee']
    module:
      loaders: [
        {test: /\.cjsx$/, loaders: ['coffee-loader', 'cjsx-loader']},
        {test: /\.coffee$/, loader: 'coffee-loader'}
      ]
    debug: true
    devtool:
      'inline-source-map'
    },
    (err, stats) ->
      if err then throw new gutil.PluginError("webpack", err)
      gutil.log "[webpack]", stats.toString()
      callback()
    )

gulp.task 'default', ['app']

gulp.task 'watch', ->
  gulp.watch 'src/**/*.*', ['app']
