 async = require 'async'
 espruino = require '../../gulp-espruino/src/gulp-espruino'
 Table = require 'cli-table'
+Q = require 'q'
 
 howMuchMemory = (options, sourceFile, sourceFiles, callback) ->
   gulp.src(sourceFiles)

formatResults = (results) ->
  table = new Table(head: ['Source File', 'Memory Usage (blocks)'], colWidths: [50, 25])
  total = 0
  for result in results
    table.push([result.sourceFile, result.memoryUsage])
    total += result.memoryUsage


module.exports = (options) ->
  files = glob.sync('./src/main/**/*.coffee')

  amdMemoryMeasured = Q.denodeify(howMuchMemoryForAMD)(options)
  eachFileMemoryMeasured = Q.denodeify(async.mapSeries)(files, howMuchMemoryForFile(options))

  Q.all([eachFileMemoryMeasured, amdMemoryMeasured])
    .spread (fileResults, amdResults) ->
      gutil.log formatResults(fileResults.concat([amdResults]))
    .done()
