import MemoryFileSystem from 'memory-fs'
import tty from 'tty'
import path from 'path'
import webpack from 'webpack'

export default function plugin (config) {
  const compiler = webpack(config)
  compiler.outputFileSystem = new MemoryFileSystem()
  return function (files, metalsmith, done) {
    compiler.run((err, stats) => {
      if (err) {
        done(err)
        return
      }
      const info = stats.toString({ chunkModules: false, colors: useColors() })
      if (stats.hasErrors()) {
        done(info)
        return
      }
      console.log(info)
      compiler.outputFileSystem.readdirSync(config.output.path).forEach(file => {
        const filePath = path.join(config.output.path, file)
        const key = path.relative(metalsmith.destination(), filePath)
        files[key] = {
          contents: compiler.outputFileSystem.readFileSync(filePath)
        }
      })
      return done()
    })
  }
}

function useColors () {
  return tty.isatty(1 /* stdout */)
}

@@ -415,16 +415,24 @@
 String(%7B
+%0A       
  chunkMo
@@ -444,16 +444,24 @@
 : false,
+%0A       
  colors:
@@ -465,19 +465,84 @@
 rs: 
-useColors()
+isStdoutATTY()  // Output colors iff stdout has a controlling terminal%0A     
  %7D)%0A
@@ -1009,17 +1009,20 @@
 ion 
-useColors
+isStdoutATTY
  () 

Rename useColors -> isStdoutATTY