<commit_before>
/**
 * Module dependencies.
 */

var fs           = require('fs')
  , path         = require('path')
  , glob         = require('glob')
  , parser       = require('./parser')
  , translations = require('./translations')
  , colors       = require('colors')
  , syntax       = require('./syntax')
  , _            = require('underscore')
  , merger       = require('./merger')
  , readline     = require('readline');

/**
 * Update
 *
 * @constructor
 */

function Update() {
  this.functionCallRegex = lcf.TRANSLATION_FUNCTION_CALL_REGEX;
  this.isWaitingUserInput = false;
  this.deletedKeys = [];
  this.addedKeys = [];
  this.migratedKeys = [];
  // readline interface
  this.rl;
  // file sources
  this.src = cf.src;
  // locales
  this.locales = cf.locales;
  // locales folder
  this.localesFolder = cf.localesFolder;
}

/**
 * Update keys
 *
 * @return {void}
 * @public
 */

Update.prototype.update = function() {
  var _this = this;
  var newTranslations = this._getSourceKeys()
  this._mergeTranslations(newTranslations, function(err, _newTranslations) {
    if(!err) {
      return _this.writeToFiles(_newTranslations);
    }
    console.error('Translation update failed');
  });
};

/**
 * Strip inner functions calls. All function calls that is not gt()
 * needs to be removed. Because they can cause updating error
 * when they are defined inside vars like below
 *
 *    Example:
 *
 *      gt('SOME_TRANSLATION_KEY', {
 *        prop1 : test() // Becomes ''
 *      });
 *
 * @param {string} content, file content
 * @private
 */

Update.prototype._stripInnerFunctionCalls = function(content) {
  return content.replace(lcf.TRANSLATION_INNER_FUNCTION_CALL_REGEX, function(m) {
    if(/gt\(/g.test(m)) {
      return m;
    } else {
      return '';
    }
  });
};

/**
 * Logs duplicate error message and output
 *
 * @param {string} key
 * @param {string} file
 */

Update.prototype.logDuplicateErrorMessage = function(key, file) {
  console.log('>>'.red + ' You have defined same translation key (' + key + ') with different vars.\n');
  console.log('In ' + file);
};

/**
 * Get translation function calls form source
 *
 * @return {struct} newTranslations
 * @private
 */

Update.prototype._getSourceKeys = function() {
  var _this = this, newTranslations = {};
  this.src.forEach(function(file) {
    var content = _this._stripInnerFunctionCalls(fs.readFileSync(file, 'utf8'));
    // Match all gt() calls
    var calls = content.match(_this.functionCallRegex);
    if(calls !== null) {
      calls.forEach(function(call) {
        var key  = parser.getKey(call)
          , vars = parser.getVars(call);
        if(!(key in newTranslations)) {
          newTranslations[key] = {};
          newTranslations[key].vars = vars;
          newTranslations[key].files = [file];
        }
        else {
          if(syntax.hasErrorDuplicate(newTranslations, key, vars)) {
            _this.logDuplicateErrorMessage(key, file);
          }<commit_msg>Removed log method for update object<commit_after>
/**
 * Module dependencies.
 */

var fs           = require('fs')
  , path         = require('path')
  , glob         = require('glob')
  , parser       = require('./parser')
  , translations = require('./translations')
  , colors       = require('colors')
  , syntax       = require('./syntax')
  , _            = require('underscore')
  , merger       = require('./merger')
  , readline     = require('readline');

/**
 * Update
 *
 * @constructor
 */

function Update() {
  this.functionCallRegex = lcf.TRANSLATION_FUNCTION_CALL_REGEX;
  this.isWaitingUserInput = false;
  this.deletedKeys = [];
  this.addedKeys = [];
  this.migratedKeys = [];
  // readline interface
  this.rl;
  // file sources
  this.src = cf.src;
  // locales
  this.locales = cf.locales;
  // locales folder
  this.localesFolder = cf.localesFolder;
}

/**
 * Update keys
 *
 * @return {void}
 * @public
 */

Update.prototype.update = function() {
  var _this = this;
  var newTranslations = this._getSourceKeys()
  this._mergeTranslations(newTranslations, function(err, _newTranslations) {
    if(!err) {
      return _this.writeToFiles(_newTranslations);
    }
    console.error('Translation update failed');
  });
};

/**
 * Strip inner functions calls. All function calls that is not gt()
 * needs to be removed. Because they can cause updating error
 * when they are defined inside vars like below
 *
 *    Example:
 *
 *      gt('SOME_TRANSLATION_KEY', {
 *        prop1 : test() // Becomes ''
 *      });
 *
 * @param {string} content, file content
 * @private
 */

Update.prototype._stripInnerFunctionCalls = function(content) {
  return content.replace(lcf.TRANSLATION_INNER_FUNCTION_CALL_REGEX, function(m) {
    if(/gt\(/g.test(m)) {
      return m;
    } else {
      return '';
    }
  });
};

/**
 * Get translation function calls form source
 *
 * @return {struct} newTranslations
 * @private
 */

Update.prototype._getSourceKeys = function() {
  var _this = this, newTranslations = {};
  this.src.forEach(function(file) {
    var content = _this._stripInnerFunctionCalls(fs.readFileSync(file, 'utf8'));
    // Match all gt() calls
    var calls = content.match(_this.functionCallRegex);
    if(calls !== null) {
      calls.forEach(function(call) {
        var key  = parser.getKey(call)
          , vars = parser.getVars(call);
        if(!(key in newTranslations)) {
          newTranslations[key] = {};
          newTranslations[key].vars = vars;
          newTranslations[key].files = [file];
        }
        else {
          if(syntax.hasErrorDuplicate(newTranslations, key, vars)) {
            throw new TypeError('You have defined a translation key ('
              + key + ') with different vars.\n In file:' + file);
          }
{'fraction_non_alphanumeric': 0.1413948646773074, 'fraction_numerical': 0.0006939625260235947, 'mean_word_length': 2.7581486310299868, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '5539116', 'n_tokens_mistral': 1791, 'n_tokens_neox': 1787, 'n_words': 386}