{"size": 1379, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "simonguest/1pif-to-macpass", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "index.js", "max_issues_repo_name": "simonguest/1pif-to-macpass", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "index.js", "max_forks_repo_name": "simonguest/1pif-to-macpass", "max_forks_repo_forks_event_min_datetime": "2019-03-08T21:16:18.000Z", "max_forks_repo_forks_event_max_datetime": "2019-03-08T21:16:18.000Z"}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 28.1428571429, "max_line_length": 82, "alphanum_fraction": 0.6555474982}
/* jshint node: true */

'use strict';

var _           = require('lodash');
var fs          = require('fs');
var path        = require('path');
var util        = require('util');
var events      = require('events');
var jsonStream  = require('./lib/json-stream');
var cheerio     = require('cheerio');
var dataHandler = require('./lib/data-handler');
var endHandler  = require('./lib/end-handler');

var defaults = {
  output: process.stdout
};

var OnePassConverter = function OnePassConverter(input, options) {

  this.input        = input;
  this.options      = _.assign(defaults, options);
  this.inputStream  = fs.createReadStream(this.input);
  this.outputStream = _.isString(this.options.output) ?
    fs.createWriteStream(this.options.output) : this.options.output;
  this.entries      = [];

  events.EventEmitter.call(this);
};

util.inherits(OnePassConverter, events.EventEmitter);

OnePassConverter.prototype.start = function() {
  this.emit('start', this.input, this.options);

  fs.readFile(path.join(__dirname, 'xml-starter.xml'), _.bind(function(err, xml) {
    if (err) throw err;

    this.xml        = cheerio.load(xml.toString(), { xmlMode: true });
    this.jsonStream = jsonStream(this.inputStream)
      .on('data', _.bind(dataHandler, this))
      .on('end', _.bind(endHandler, this));
  }, this));

  return true;
};

module.exports = OnePassConverter;
