<commit_before>Path = require 'path'
request = require('superagent')
fs = require 'fs'
Q = require 'q'

module.exports = (grunt, config)->
  flatten = (a, b)-> a.concat b
  prefix = (prefix)-> (str)->"#{prefix}#{str}"

  testFileOrdering = grunt.expandFileArg(
    config.find('client.root', 'src/client/')
    config.find('client.test.glob', 'CLIENT_TEST_GLOB', '**/')
  )

  grunt.Config =
    notify:
      client:
        options:
          message: 'Client Tests Complete.'
    watch:
      client:
        files: [
          'src/client/**'
        ]
        tasks: [
          'client'
          'notify:client'
        ]

  # butt - Browser Under Test Tools
  butt = config.find('client.browsers', [])
  unless process.env.DEBUG
    if process.env.BAMBOO
      butt.push 'PhantomJS'
    else if process.env.TRAVIS
      butt.push 'Firefox'
    else
      butt.push 'Chrome'

  preprocessors =
    'src/client/**/*test.coffee': [ 'coffee' ]
    'src/client/**/*mock.coffee': [ 'coffee' ]
    'src/client/tools/**/*.coffee': [ 'coffee' ]

  writeTarget = config.find('client.write.target', 'WRITE_TARGET', './www')
  if true and (not process.env.DEBUG or process.env.COVER)
    app = "#{writeTarget}/application.js"
    preprocessors[app] = [ 'coverage' ]

  grunt.Config =
    karma:
      client:
        options:
          browsers: butt
          frameworks: [ 'mocha', 'sinon-chai' ]
          reporters: [ 'spec', 'junit', 'coverage' ]
          singleRun: true,
          logLevel: 'INFO'
          preprocessors: preprocessors
          files: [
            config.find('client.files', [])
            [
              'vendors.js'
              'templates.js'
              'application.js'
            ].map (_)-> "#{writeTarget}/#{_}"

            config.append('client.test.tools', [
              'src/client/tools/**'
              'src/client/**/*mock.{js,coffee}'
            ])

            testFileOrdering
          ].reduce(flatten, [])
          junitReporter:
            outputFile: 'build/reports/karma.xml'
          coverageReporter:
            type: 'lcov'
            dir: 'build/reports/coverage/'

  grunt.registerTask 'writeClient', ->
    done = @async()
    options = @options
      dest: writeTarget
      files: config.prepend 'client.write.files', [
        'index.html'
        'application.js'
        'application.js.map'
        'templates.js'
        'vendors.js'
        'vendors.css'
        'all.css'
        'screen.css'
        'print.css'
      ]
    options.dest = Path.resolve process.cwd(), options.dest
    getFile = (file)->
      defer = Q.defer()
      grunt.verbose.writeln "Starting request for #{file}..."
      request.get("#{process.env.URL}#{file}")
      .buffer()
      .end (err, res)->
        return defer.reject err if err
        return defer.reject res if res.status isnt 200
        grunt.file.write Path.join(options.dest, file), res.text
        defer.resolve()
      defer.promise

    writeFiles = ->
      pass = true
      process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
      grunt.verbose.writeln 'Stassets compiled all files!'
      promises = options.files.map getFile
      Q.all(promises)
      .catch (err)->
        grunt.debug.write err
        grunt.debug.writeln ''
        pass = false
      .then ->
        process.env.NODE_TLS_REJECT_UNAUTHORIZED = null
        done pass

    try
      base = require(config.find('server'))
      base.start().then ->
        base.app.stassets.promise.then writeFiles
    catch e
      # Assume it's already running. TODO look for EACCESS
      grunt.log.error 'Failed to start client'
      grunt.log.writeln e
      grunt.verbose.writeln e.stack
      process.env.URL = 'localhost:8080' # Next best guess
      writeFiles()<commit_msg>Cleaned up some config client.files usage.<commit_after>Path = require 'path'
request = require('superagent')
fs = require 'fs'
Q = require 'q'

module.exports = (grunt, config)->
  flatten = (a, b)-> a.concat b
  prefix = (prefix)-> (str)->"#{prefix}#{str}"

  testFileOrdering = grunt.expandFileArg(
    config.find('client.root', 'src/client/')
    config.find('client.test.glob', 'CLIENT_TEST_GLOB', '**/')
  )

  grunt.Config =
    notify:
      client:
        options:
          message: 'Client Tests Complete.'
    watch:
      client:
        files: [
          'src/client/**'
        ]
        tasks: [
          'client'
          'notify:client'
        ]

  # butt - Browser Under Test Tools
  butt = config.find('client.browsers', [])
  unless process.env.DEBUG
    if process.env.BAMBOO
      butt.push 'PhantomJS'
    else if process.env.TRAVIS
      butt.push 'Firefox'
    else
      butt.push 'Chrome'

  preprocessors =
    'src/client/**/*test.coffee': [ 'coffee' ]
    'src/client/**/*mock.coffee': [ 'coffee' ]
    'src/client/tools/**/*.coffee': [ 'coffee' ]

  writeTarget = config.find('client.write.target', 'WRITE_TARGET', './www')
  if true and (not process.env.DEBUG or process.env.COVER)
    app = "#{writeTarget}/application.js"
    preprocessors[app] = [ 'coverage' ]

  config.append('client.test.tools', [
    'tools/**'
    '**/*mock.{js,coffee}'
  ])

  config.map('cient.test.tools', (_)-> Path.join config.find('client.root'), _)

  grunt.Config =
    karma:
      client:
        options:
          browsers: butt
          frameworks: [ 'mocha', 'sinon-chai' ]
          reporters: [ 'spec', 'junit', 'coverage' ]
          singleRun: true,
          logLevel: 'INFO'
          preprocessors: preprocessors
          files: [
            config.prepend 'client.files', [
                'vendors.js'
                'templates.js'
                'application.js'
              ].map (_)-> "#{writeTarget}/#{_}"

            config.find('client.test.tools')

            testFileOrdering
          ].reduce(flatten, [])
          junitReporter:
            outputFile: 'build/reports/karma.xml'
          coverageReporter:
            type: 'lcov'
            dir: 'build/reports/coverage/'

  grunt.registerTask 'writeClient', ->
    done = @async()
    options = @options
      dest: writeTarget
      files: config.prepend 'client.write.files', [
        'index.html'
        'application.js'
        'application.js.map'
        'templates.js'
        'vendors.js'
        'vendors.css'
        'all.css'
        'screen.css'
        'print.css'
      ]
    options.dest = Path.resolve process.cwd(), options.dest
    getFile = (file)->
      defer = Q.defer()
      grunt.verbose.writeln "Starting request for #{file}..."
      request.get("#{process.env.URL}#{file}")
      .buffer()
      .end (err, res)->
        return defer.reject err if err
        return defer.reject res if res.status isnt 200
        grunt.file.write Path.join(options.dest, file), res.text
        defer.resolve()
      defer.promise

    writeFiles = ->
      pass = true
      process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
      grunt.verbose.writeln 'Stassets compiled all files!'
      promises = options.files.map getFile
      Q.all(promises)
      .catch (err)->
        grunt.debug.write err
        grunt.debug.writeln ''
        pass = false
      .then ->
        process.env.NODE_TLS_REJECT_UNAUTHORIZED = null
        done pass

    try
      server = config.find('server', 'APP_SERVER', "#{process.cwd()}/app.js")
      base = require(server)
      base.start().then ->
        base.app.stassets.promise.then writeFiles
    catch e
      # Assume it's already running. TODO look for EACCESS
      grunt.log.error 'Failed to start client'
      grunt.log.writeln e
      grunt.verbose.writeln e.stack
      process.env.URL = 'localhost:8080' # Next best guess
      writeFiles()
{'fraction_non_alphanumeric': 0.14211348256116607, 'fraction_numerical': 0.002082248828735034, 'mean_word_length': 1.999609679937549, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4411533', 'n_tokens_mistral': 2485, 'n_tokens_neox': 2434, 'n_words': 489}