'use babel'

import {readFileSync} from 'fs'
import Path from 'path'
import {getConfig, getRules, scanFiles, saveFile, findRoot} from './helpers'
import Debug from 'debug'

const debugCompile = Debug('UCompiler:Compile')

export function compile(path, options = {}, defaultRules = {}) {
  options.ignored = options.ignored || ['{**/, }node_modules/**', '{**/, }.*']
  options.root = options.root || null

  const root = findRoot(path, options.root)
  const config = getConfig(root)
  const files = scanFiles(path, {root: root, ignored: options.ignored})
  const plugins = {compilers: [], general: [], minifiers: []}

  config.plugins.forEach(function(moduleName) {
    const module = moduleName.indexOf('ucompiler-plugin-') === 0 ? require(moduleName) :require(`ucompiler-plugin-${moduleName}`)
    if (module.compiler) {
      plugins.compilers.push(module)
    } else if (module.minifiers) {
      plugins.minifiers.push(module)
    } else {
      plugins.general.push(module)
    }
  })

  return Promise.all(files.map(function(relativePath) {
    // TODO (For the future): Reverse source maps when changed
    const file = Path.join(root, relativePath)
    const state = {sourceMap: null, root, relativePath}
    const rules = getRules({path, state, config, defaultRules})
    const contents = readFileSync(file, {encoding: 'utf8'})

    return plugins.compilers.reduce(function(promise, plugin) {
      return promise.then(function(contents) {
        return plugin.process({contents, file, config, state, rules})
      })
    }, Promise.resolve(contents)).then(function(contents) {
      return plugins.general.reduce(function(promise, plugin) {
        return promise.then(function(contents) {
          return plugin.process({contents, file, config, state, rules})
        })
      }, Promise.resolve(contents))
    }).then(function(contents) {
      return plugins.minifiers.reduce(function(promise, plugin) {
        return promise.then(function(contents) {
          return plugin.process({contents, file, config, state, rules})
        })
      }, Promise.resolve(contents))
    }).then(function(contents) {
      debugCompile(`Processed ${relativePath}`)
      return saveFile({contents, file, config, state, rules, root})
    })
  }))
}

@@ -213,16 +213,249 @@
 ompile')
+%0Aexport const DEFAULT_IGNORE = %5B%0A  'node_modules/**',%0A  'bower_components/**',%0A  'coverage/**',%0A  '%7Btmp,temp%7D/**',%0A  '**/*.min.js',%0A  '**/bundle.js',%0A  'fixture%7B-*,%7D.%7Bjs,jsx%7D',%0A  '%7Btest/,%7Dfixture%7Bs,%7D/**',%0A  'vendor/**',%0A  'dist/**'%0A%5D
 %0A%0Aexport
@@ -533,16 +533,17 @@
 nored = 
+(
 options.
@@ -558,46 +558,33 @@
 %7C%7C %5B
-'%7B**/, %7Dnode_modules/**', '%7B**/, %7D.*'%5D
+%5D).concat(DEFAULT_IGNORE)
 %0A  o

Improve default ignored list