{"size": 2012, "ext": "js", "max_stars_repo_path": "custom_reporters/csv-output.js", "max_stars_repo_name": "Fishbowler/nightwatch-handy-bits", "max_stars_repo_stars_event_min_datetime": "2017-06-22T13:31:54.000Z", "max_stars_repo_stars_event_max_datetime": "2019-06-26T17:48:54.000Z", "max_issues_repo_path": "custom_reporters/csv-output.js", "max_issues_repo_name": "Fishbowler/nightwatch-handy-bits", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "custom_reporters/csv-output.js", "max_forks_repo_name": "Fishbowler/nightwatch-handy-bits", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 6, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 37.9622641509, "max_line_length": 161, "alphanum_fraction": 0.6098409543}
/* 
Based on https://github.com/rarcega/nightwatch-custom-html-reporter
This requires that input is comma seperated already so is a bit of a sham, to be honest, but I've not thought of a solution to that (without modding Nightwatch)
*/

var fs = require('fs');
var path = require('path');

module.exports = new (function() {

  this.write = function(results, options, callback) {

    var defaultOptions = {failuresOnly: true}; //By default, ignore PASS & SKIP.

    var theseOptions = (options ? Object.assign({}, defaultOptions, options) : defaultOptions) //If some options provided, merge them over defaultOptions
    var reportFilename = options.filename_prefix + Date.now() + '.csv';
    var reportFilePath = path.join(options.output_folder, reportFilename);
    var csvOutput = '';

    var resultsKeys = Object.keys(results.modules);
    resultsKeys.forEach(function(resultsKey){
      var completedKeys = Object.keys(results.modules[resultsKey].completed);
      completedKeys.forEach(function(completedKey){
        var theseAssertions = results.modules[resultsKey].completed[completedKey].assertions;
        theseAssertions.forEach(function(assertion){
          if(assertion.message.trim() === ''){
            return;
          }
          if((!theseOptions.failuresOnly) || assertion.failure){ //If we aren't filtering, or it's got a failure
            var csvLine = '';
            if(assertion.message){
              csvLine += assertion.message;
            }
            if(assertion.failure){
              //csvLine += (csvLine.length > 0 ? ',' : '') + assertion.failure;
            }
            csvLine += (csvLine.length > 0 ? '\n' : '');
            csvOutput += csvLine;
          }
        })
      })
    })

    // write the html to a file
    fs.writeFile(reportFilePath, csvOutput, function(err) {
      if (err) throw err;
      console.log('Report generated: ' + reportFilePath);
      callback();
    });
    
    
  }
})();