const expand     = require('toolz/src/string/expand')
const concurrent = require('toolz/src/async/concurrent')
const readFile   = require('toolz/src/file/readFile')
const writeFile  = require('toolz/src/file/writeFile')
const isOr       = require('toolz/src/lang/isOr')
const display    = require('./display')
const path       = require('path')
const fs         = require('fs')
const assert     = require('assert')

function tmpls (opts, cb) {
  const license = opts.meta.license
  const tmpl = opts.files
  var files = []
  var filePath

  Object.keys(tmpl).forEach(function (name) {
    if (tmpl[name]) {
      if (isOr(name, 'gitignore', 'eslintrc')) return files.push(name)
      if (isOr(name, 'index', 'test')) return files.push(name + '.js')
      if (name === 'package') return files.push(name + '.json')
      if (name === 'readme') return files.push(name.toUpperCase() + '.md')
      if (name === 'travis') return files.push(name + '.yml')
      if (name === 'license') {
        if (license === 'ISC') return files.push(name.toUpperCase() + '-ISC')
        else if (license === 'MIT') return files.push(name.toUpperCase() + '-MIT')
        else return files.push(name.toUpperCase() + '-UN')
      }
    }
  })
  // console.log(files)

  display.heading('Templates')

  concurrent.each(files, function (file, key, next) {
    if (isOr(file, 'gitignore', 'travis.yml', 'eslintrc')) {
      filePath = './.' + file
    } else if (isOr(file, 'LICENSE-MIT', 'LICENSE-ISC', 'LICENSE-UN')) {
      filePath = './' + file.replace(/-(MIT|ISC|UN)/, '')
    } else {
      filePath = './' + file
    }

    return readFile(path.join(__dirname, '../lib/' + file), function (err, res) {
      assert.ifError(err)
      var tmpl = expand(res, opts.meta)
      writeFile(filePath, tmpl, function (err) {
        assert.ifError(err)
        display.event('create:', file, 'white')
        next(null)
      })
    })
  }, function (err, results) {
    assert.ifError(err)
    cb(null)
  })
}

module.exports = tmpls

@@ -5,25 +5,24 @@
 t expand    
- 
 = require('t
@@ -52,26 +52,25 @@
 )%0Aconst 
-concurrent
+iterate  
  = requi
@@ -89,26 +89,23 @@
 c/async/
-concurrent
+iterate
 ')%0Aconst
@@ -115,17 +115,16 @@
 adFile  
- 
 = requir
@@ -164,25 +164,24 @@
 t writeFile 
- 
 = require('t
@@ -218,25 +218,24 @@
 t isOr      
- 
 = require('t
@@ -271,17 +271,16 @@
 splay   
- 
 = requir
@@ -306,25 +306,24 @@
 t path      
- 
 = require('p
@@ -340,25 +340,24 @@
 t fs        
- 
 = require('f
@@ -372,25 +372,24 @@
 t assert    
- 
 = require('a
@@ -1265,18 +1265,15 @@
 %0A%0A  
-concurrent
+iterate
 .eac

revert back to iterating serially over templates