var through = require('through2')
var path = require('path')
var walker = require('folder-walker')
var each = require('stream-each')

module.exports.listEach = function (opts, onEach, cb) {
  var stream = walker(opts.dir, {filter: function (data) {
    if (path.basename(data) === '.dat') return false
    if ((typeof opts.filter) === 'function') return opts.filter(data)
    return true
  }})
  var dirname = path.basename(opts.dir)

  each(stream, function (data, next) {
    var prefix = path.resolve(data.filepath) !== path.resolve(opts.dir)
    var item = {
      name: prefix ? path.join(dirname, data.relname) : data.relname,
      path: path.resolve(data.filepath),
      mode: data.stat.mode,
      uid: data.stat.uid,
      gid: data.stat.gid,
      mtime: data.stat.mtime.getTime(),
      ctime: data.stat.ctime.getTime(),
      size: data.stat.size
    }

    var isFile = data.stat.isFile()
    if (isFile) {
      item.type = 'file'
    }
    var isDir = data.stat.isDirectory()
    if (isDir) item.type = 'directory'
    onEach(item, next)
  }, cb)
}

// `stats` is for rendering progress bars
module.exports.createDownloadStream = function (archive, stats, opts) {
  if (!stats) stats = {progress: {}, fileQueue: []}
  if (!opts) opts = {}

  var downloader = through.obj(function (item, enc, next) {
    if (opts.files && (opts.files.indexOf(item.name)) === -1) next()
    else {
      var downloadStats = archive.download(item, function (err) {
        if (err) return next(err)
        if (item.type === 'directory') stats.progress.directories++
        else stats.progress.filesRead++
        next()
      })
      if (item.type === 'file') {
        stats.fileQueue.push({
          name: item.name,
          stats: downloadStats
        })
        downloadStats.on('ready', function () {
          stats.progress.bytesRead += downloadStats.bytesInitial
        })
      }
    }
  })
  downloader.stats = stats
  return downloader
}

@@ -1371,24 +1371,31 @@
  === -1)
+ return
  next()%0A
     else
@@ -1390,21 +1390,8 @@
 t()%0A
-    else %7B%0A  
     
@@ -1452,26 +1452,24 @@
 rr) %7B%0A      
- 
- 
 if (err) ret
@@ -1482,18 +1482,16 @@
 xt(err)%0A
-  
       if
@@ -1554,18 +1554,16 @@
 +%0A      
-  
 else sta
@@ -1592,18 +1592,16 @@
 +%0A      
- 
- 
 next()%0A 
@@ -1599,31 +1599,27 @@
  next()%0A    
-  
 %7D)%0A
-  
     if (item
@@ -1634,26 +1634,24 @@
 = 'file') %7B%0A
-  
       stats.
@@ -1671,26 +1671,24 @@
 h(%7B%0A        
-  
 name: item.n
@@ -1688,26 +1688,24 @@
  item.name,%0A
-  
         stat
@@ -1723,26 +1723,24 @@
 Stats%0A      
-  
 %7D)%0A        d
@@ -1736,18 +1736,16 @@
 )%0A      
- 
- 
 download
@@ -1772,26 +1772,24 @@
 nction () %7B%0A
-  
         stat
@@ -1849,20 +1849,10 @@
     
-  %7D)%0A      
 %7D
+)
 %0A   

remove the else