<commit_before>/**
 * Created by bryangill on 8/9/16.
 */

var fs = require('fs');
var fse = require('fs-extra');
var lbl = require('line-by-line');
var S = require('string');

module.exports = {

  getDirectoryContentNames: function (path) {

    "use strict";

    var promise = new Promise(function (resolve, reject) {
      try {
        fs.readdir(path, function (err, items) {
          resolve(items);
        });
      } catch (error) {
        sails.log.error(error);
        reject(error);
      }
    });

    return promise;

  },

  fileParse: function (lr) {

    //todo: each file will create a new object

    var promise = new Promise(function (resolve, reject) {

      var score = '';

      lr.on('error', function (err) {
        sails.log.error(err);
        // 'err' contains error object
      });

      lr.on('line', function (line) {

        if (S(line).contains('WIN:')) {
          score = line;
          sails.log.info(score);
          resolve(score);
        }
        // 'line' contains the current line without the trailing newline character.
      });

      lr.on('end', function () {
        // sails.log.info('end of readrows');
        // All lines are read, file is closed now.
      });

    });

    return promise;

  },

  getScores: function (files) {
    "use strict";
    var self = this;

    var promise = new Promise(function (resolve, reject) {

      try {

        for (var file of files) {

          var lr = new lbl('input/' + file);

          try {
            self.fileParse(lr).then(function (data) {
              resolve(data);
            });
          } catch (err) {
            sails.log.error('error parsing scores: ' + err);
            reject(err);
          }
        }
      } catch (err) {
        sails.log.error('error parsing scores: ' + err);
        reject(err);
      }

    });

    return promise;

  },

  moveDirectoryContent: function (from, to) {

    "use strict";

    try {
      sails.log.info("moving directory content")
      fse.copySync(from, to);
      sails.log.info("completed moving directory content")
      this.removeDirectoryContent(from);
    } catch (err) {
      sails.log.error('error moving directory content: ' + err);
    }
  },

  removeDirectoryContent: function (from) {
    "use strict";

    sails.log.info("beginning to remove contents from directory...");
    try {
      sails.log.info("removing contents from directory...");
      fse.emptyDirSync(from);
      sails.log.info("completed removing contents from directory");
    } catch (err) {
      sails.log.error('error removing contents from directory: ' + err);
    }
  },

  readRows: function (files) {
    "use strict";
    var self = this;

    var promise = new Promise(function (resolve, reject) {

      try {
        self.getScores(files).then(function (data) {
          resolve(data);
        });

      } catch (err) {
        sails.log.error('error reading rows from directory: ' + err);
        reject(err);
      }

    });

    return promise;

  }

<commit_msg>Correctly passing back line items out of the files.<commit_after>/**
 * Created by bryangill on 8/9/16.
 */

var fs = require('fs');
var fse = require('fs-extra');
var lbl = require('line-by-line');
var S = require('string');
var score = [];

module.exports = {

  getDirectoryContentNames: function (path) {

    "use strict";

    var promise = new Promise(function (resolve, reject) {
      try {
        fs.readdir(path, function (err, items) {
          resolve(items);
        });
      } catch (error) {
        sails.log.error(error);
        reject(error);
      }
    });

    return promise;

  },

  parseFile: function (file) {

    //todo: each file will create a new object


    var promise = new Promise(function (resolve, reject) {


      var lr = new lbl('input/' + file);

      lr.on('error', function (err) {
        sails.log.error(err);
        // 'err' contains error object
      });

      lr.on('line', function (line) {

        if (S(line).contains('WIN:')) {
          score.push(line);
          // sails.log.info(score);
          resolve(score);
        }
        // 'line' contains the current line without the trailing newline character.
      });

      lr.on('end', function () {
        // sails.log.info('end of readrows');
        // All lines are read, file is closed now.
      });

    });

    return promise;

  },

  getScores: function (files) {
    "use strict";
    var self = this;

    var promise = new Promise(function (resolve, reject) {

      try {

        for (var file of files) {

          try {
            resolve(self.parseFile(file));
          } catch (err) {
            sails.log.error('error parsing scores: ' + err);
            reject(err);
          }
        }
      } catch (err) {
        sails.log.error('error parsing scores: ' + err);
        reject(err);
      }

    });

    return promise;

  },

  moveDirectoryContent: function (from, to) {

    "use strict";

    try {
      sails.log.info("moving directory content")
      fse.copySync(from, to);
      sails.log.info("completed moving directory content")
      this.removeDirectoryContent(from);
    } catch (err) {
      sails.log.error('error moving directory content: ' + err);
    }
  },

  removeDirectoryContent: function (from) {
    "use strict";

    sails.log.info("beginning to remove contents from directory...");
    try {
      sails.log.info("removing contents from directory...");
      fse.emptyDirSync(from);
      sails.log.info("completed removing contents from directory");
    } catch (err) {
      sails.log.error('error removing contents from directory: ' + err);
    }
  },

  readRows: function (files) {
    "use strict";
    var self = this;

    var promise = new Promise(function (resolve, reject) {

      try {
        resolve(self.getScores(files));

      } catch (err) {
        sails.log.error('error reading rows from directory: ' + err);
        reject(err);
      }

    });

    return promise;

  }


{'fraction_non_alphanumeric': 0.1404643449419569, 'fraction_numerical': 0.0013266998341625207, 'mean_word_length': 2.0291310899045705, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '2202845', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1769, 'n_words': 465}