<commit_before>const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const webpack = require('webpack');
const modulePaths = require('./module-paths');
const StripesBuildError = require('./stripes-build-error');

function prefixKeys(obj, prefix) {
  const res = {};
  for (const key of Object.keys(obj)) {
    res[`${prefix}${key}`] = obj[key];
  }
  return res;
}

module.exports = class StripesTranslationPlugin {
  constructor(options) {
    // Include stripes-core et al because they have translations
    this.modules = {
      '@folio/stripes-core': {},
      '@folio/stripes-components': {},
      '@folio/stripes-smart-components': {},
    };
    Object.assign(this.modules, options.modules);
    this.languageFilter = options.config.languages || [];
  }

  apply(compiler) {
    // Used to help locate modules
    this.context = compiler.context;
    this.publicPath = compiler.options.output.publicPath;
    this.aliases = compiler.options.resolve.alias;

    // Limit the number of languages loaded by third-party libraries with the ContextReplacementPlugin
    if (this.languageFilter.length) {
      const filterRegex = new RegExp(`(${this.languageFilter.join('|')})`); // constructed regex will look something like /(en|es)/
      compiler.apply(new webpack.ContextReplacementPlugin(/react-intl[/\\]locale-data/, filterRegex));
      compiler.apply(new webpack.ContextReplacementPlugin(/moment[/\\]locale/, filterRegex));
    }

    // Gather all translations available in each module
    const allTranslations = this.gatherAllTranslations();
    const fileData = this.generateFileNames(allTranslations);
    this.allFiles = _.mapValues(fileData, data => data.browserPath); // stripes-config-plugin will grab "allFiles" for fetching in the browser

    // Emit merged translations to the output directory
    compiler.plugin('emit', (compilation, callback) => {
      Object.keys(allTranslations).forEach((language) => {
        const content = JSON.stringify(allTranslations[language]);
        compilation.assets[fileData[language].emitPath] = {
          source: () => content,
          size: () => content.length,
        };
      });
      callback();
    });
  }

  // Locate each module's translations directory (current) or package.json data (fallback)
  gatherAllTranslations() {
    const allTranslations = {};
    for (const mod of Object.keys(this.modules)) {
      const modPackageJsonPath = modulePaths.locateStripesModule(this.context, mod, this.aliases, 'package.json');
      if (!modPackageJsonPath) {
        console.log(`Unable to locate ${mod} while looking for translations.`);
        continue;
      }
      const modTranslationDir = modPackageJsonPath.replace('package.json', 'translations');

      if (fs.existsSync(modTranslationDir)) {
        _.merge(allTranslations, this.loadTranslationsDirectory(mod, modTranslationDir));
      } else {
        _.merge(allTranslations, this.loadTranslationsPackageJson(mod, modPackageJsonPath));
      }
    }
    return allTranslations;
  }
<commit_msg>refactoring to appease eslint<commit_after>const path = require('path');
const fs = require('fs');
const _ = require('lodash');
const webpack = require('webpack');
const modulePaths = require('./module-paths');

function prefixKeys(obj, prefix) {
  const res = {};
  for (const key of Object.keys(obj)) {
    res[`${prefix}${key}`] = obj[key];
  }
  return res;
}

module.exports = class StripesTranslationPlugin {
  constructor(options) {
    // Include stripes-core et al because they have translations
    this.modules = {
      '@folio/stripes-core': {},
      '@folio/stripes-components': {},
      '@folio/stripes-smart-components': {},
    };
    Object.assign(this.modules, options.modules);
    this.languageFilter = options.config.languages || [];
  }

  apply(compiler) {
    // Used to help locate modules
    this.context = compiler.context;
    this.publicPath = compiler.options.output.publicPath;
    this.aliases = compiler.options.resolve.alias;

    // Limit the number of languages loaded by third-party libraries with the ContextReplacementPlugin
    if (this.languageFilter.length) {
      const filterRegex = new RegExp(`(${this.languageFilter.join('|')})`); // constructed regex will look something like /(en|es)/
      compiler.apply(new webpack.ContextReplacementPlugin(/react-intl[/\\]locale-data/, filterRegex));
      compiler.apply(new webpack.ContextReplacementPlugin(/moment[/\\]locale/, filterRegex));
    }

    // Gather all translations available in each module
    const allTranslations = this.gatherAllTranslations();
    const fileData = this.generateFileNames(allTranslations);
    this.allFiles = _.mapValues(fileData, data => data.browserPath); // stripes-config-plugin will grab "allFiles" for fetching in the browser

    // Emit merged translations to the output directory
    compiler.plugin('emit', (compilation, callback) => {
      Object.keys(allTranslations).forEach((language) => {
        const content = JSON.stringify(allTranslations[language]);
        compilation.assets[fileData[language].emitPath] = {
          source: () => content,
          size: () => content.length,
        };
      });
      callback();
    });
  }

  // Locate each module's translations directory (current) or package.json data (fallback)
  gatherAllTranslations() {
    const allTranslations = {};
    for (const mod of Object.keys(this.modules)) {
      const modPackageJsonPath = modulePaths.locateStripesModule(this.context, mod, this.aliases, 'package.json');
      if (modPackageJsonPath) {
        const modTranslationDir = modPackageJsonPath.replace('package.json', 'translations');
        if (fs.existsSync(modTranslationDir)) {
          _.merge(allTranslations, this.loadTranslationsDirectory(mod, modTranslationDir));
        } else {
          _.merge(allTranslations, this.loadTranslationsPackageJson(mod, modPackageJsonPath));
        }
      } else {
        console.log(`Unable to locate ${mod} while looking for translations.`);
      }
    }
    return allTranslations;
  }

{'fraction_non_alphanumeric': 0.13154010168935543, 'fraction_numerical': 0.0, 'mean_word_length': 3.5541448842419716, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, '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': '3384923', 'n_tokens_mistral': 1717, 'n_tokens_neox': 1694, 'n_words': 380}