<commit_before>/* eslint-disable no-console */
/* eslint-disable no-unused-vars */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2019
 */

const fs = require('fs');

const localizationFiles = [];
const nextLine = /{nextLine}/g;
const selectedLanguage = process.argv[3];
const newFileFolder = './src/localization/translations/';
const mainLanguage = 'gb';
const message =
  '\nMore information about this script here: https://github.com/openmsupply/mobile/wiki/Scripts\n';

// Reads all JSON files in localization files and save them in localizationFiles array
const filesFolder = './src/localization/';
fs.readdirSync(`./${filesFolder}`).forEach(file => {
  if (file.split('.').pop() === 'json') {
    localizationFiles.push({
      fileContent: require(`../${filesFolder}${file}`),
      fileName: file.replace('.json', ''),
    });
  }
});

// Exports all strings from the selectedLanguage to CSV
// csvData keeps all the content in csv format
const exportTranslation = () => {
  let csvData = '';
  localizationFiles.forEach(({ fileName, fileContent }) => {
    const selectedLanguageContent = fileContent[selectedLanguage];
    const mainLanguageContent = fileContent[mainLanguage];

    Object.entries(mainLanguageContent).forEach(([key, value]) => {
      value = value.replace(/\n/gi, '{nextLine}');
      if (selectedLanguageContent[key]) {
        selectedLanguageContent[key] = selectedLanguageContent[key].replace(/\n/gi, '{nextLine}');
      } else selectedLanguageContent[key] = '';
      csvData = `${csvData} ${fileName},${key},"${value}","${selectedLanguageContent[key]}"\n`;
    });
  });
  fs.writeFile(`${newFileFolder}${selectedLanguage}.csv`, csvData, 'utf8', err => {
    if (err) {
      console.log('Error - file either not saved or corrupted file saved.');
    } else {
      console.log(`\nFile ${newFileFolder}${selectedLanguage}.csv saved.${message}`);
    }
  });
};

// Imports all data from TSV file and save it in its corresponding localization file
const importTranslation = () => {
  const csvObject = {};
  const file = fs.readFileSync(`${newFileFolder}${selectedLanguage}.tsv`, 'utf8');

  // Creates one string per line available in the csv file
  const csvRows = file.toString().split('\n');

  // Reads row by row in the TSV file
  csvRows.forEach(csvRow => {
    const [fileName, translationField, generalValue, selectedValue] = csvRow.split('\t');
    const trimmedFileName = fileName.trim();
    if (!csvObject[trimmedFileName]) csvObject[trimmedFileName] = {};
    if (!selectedValue) return;
    const trimmedSelectedValue = selectedValue.replace(nextLine, '\n').trim();
    if (trimmedSelectedValue) csvObject[trimmedFileName][translationField] = trimmedSelectedValue;
  });

  localizationFiles.forEach(({ fileName, fileContent }) => {
    const newObject = {};
    Object.entries(fileContent[mainLanguage]).forEach(([key]) => {
      if (csvObject[fileName][key]) {
        newObject[key] = csvObject[fileName][key];
      } else if (fileContent[selectedLanguage] && fileContent[selectedLanguage][key]) {
        newObject[key] = fileContent[selectedLanguage][key];
      }
    });
    fileContent[selectedLanguage] = newObject;
    const fileContentStringify = JSON.stringify(fileContent, null, 2);

    // Updates each file based on changes
    fs.writeFile(`${filesFolder}${fileName}.json`, `${fileContentStringify}\r\n`, err => {
      if (err) throw err;
      console.log(`The file ${filesFolder}${fileName}.json has been saved`);
    });
  });
  console.log(`${message}`);
};<commit_msg>Destructuring in localizationFiles.forEach<commit_after>/* eslint-disable no-console */
/* eslint-disable no-unused-vars */
/* eslint-disable import/no-dynamic-require */
/* eslint-disable global-require */
/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2019
 */

const fs = require('fs');

const localizationFiles = [];
const nextLine = /{nextLine}/g;
const selectedLanguage = process.argv[3];
const newFileFolder = './src/localization/translations/';
const mainLanguage = 'gb';
const message =
  '\nMore information about this script here: https://github.com/openmsupply/mobile/wiki/Scripts\n';

// Reads all JSON files in localization files and save them in localizationFiles array
const filesFolder = './src/localization/';
fs.readdirSync(`./${filesFolder}`).forEach(file => {
  if (file.split('.').pop() === 'json') {
    localizationFiles.push({
      fileContent: require(`../${filesFolder}${file}`),
      fileName: file.replace('.json', ''),
    });
  }
});

// Exports all strings from the selectedLanguage to CSV
// csvData keeps all the content in csv format
const exportTranslation = () => {
  let csvData = '';
  localizationFiles.forEach(({ fileName, fileContent }) => {
    const {
      [mainLanguage]: mainLanguageContent,
      [selectedLanguage]: selectedLanguageContent,
    } = fileContent;
    Object.entries(mainLanguageContent).forEach(([key, value]) => {
      value = value.replace(/\n/gi, '{nextLine}');
      if (selectedLanguageContent[key]) {
        selectedLanguageContent[key] = selectedLanguageContent[key].replace(/\n/gi, '{nextLine}');
      } else selectedLanguageContent[key] = '';
      csvData = `${csvData} ${fileName},${key},"${value}","${selectedLanguageContent[key]}"\n`;
    });
  });
  fs.writeFile(`${newFileFolder}${selectedLanguage}.csv`, csvData, 'utf8', err => {
    if (err) {
      console.log('Error - file either not saved or corrupted file saved.');
    } else {
      console.log(`\nFile ${newFileFolder}${selectedLanguage}.csv saved.${message}`);
    }
  });
};

// Imports all data from TSV file and save it in its corresponding localization file
const importTranslation = () => {
  const csvObject = {};
  const file = fs.readFileSync(`${newFileFolder}${selectedLanguage}.tsv`, 'utf8');

  // Creates one string per line available in the csv file
  const csvRows = file.toString().split('\n');

  // Reads row by row in the TSV file
  csvRows.forEach(csvRow => {
    const [fileName, translationField, generalValue, selectedValue] = csvRow.split('\t');
    const trimmedFileName = fileName.trim();
    if (!csvObject[trimmedFileName]) csvObject[trimmedFileName] = {};
    if (!selectedValue) return;
    const trimmedSelectedValue = selectedValue.replace(nextLine, '\n').trim();
    if (trimmedSelectedValue) csvObject[trimmedFileName][translationField] = trimmedSelectedValue;
  });

  localizationFiles.forEach(({ fileName, fileContent }) => {
    const {
      [mainLanguage]: mainLanguageContent,
      [selectedLanguage]: selectedLanguageContent,
    } = fileContent;
    const newObject = {};
    Object.entries(mainLanguageContent).forEach(([key]) => {
      if (csvObject[fileName][key]) {
        newObject[key] = csvObject[fileName][key];
      } else if (selectedLanguageContent && selectedLanguageContent[key]) {
        newObject[key] = selectedLanguageContent[key];
      }
    });
    fileContent[selectedLanguage] = newObject;
    const fileContentStringify = JSON.stringify(fileContent, null, 2);

    // Updates each file based on changes
    fs.writeFile(`${filesFolder}${fileName}.json`, `${fileContentStringify}\r\n`, err => {
      if (err) throw err;
      console.log(`The file ${filesFolder}${fileName}.json has been saved`);
    });
  });
  console.log(`${message}`);
};
{'fraction_non_alphanumeric': 0.15955147257497973, 'fraction_numerical': 0.0021615779519048904, 'mean_word_length': 4.116102280580511, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 2, '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': '783763', 'n_tokens_mistral': 2159, 'n_tokens_neox': 2134, 'n_words': 488}