<commit_before>const globSync = require('glob').sync;
const path = require('path');
const appRootPath = require('app-root-path').toString();
const flowRemoveTypes = require('flow-remove-types');
const fs = require('fs');
const rimraf = require('rimraf');
const { exec } = require('../utils');

const srcPath = path.resolve(appRootPath, 'src');

const isJsFile = file => path.extname(file) === '.js';

const stripTypes = () => {
  globSync(`${path.resolve(appRootPath, 'src')}/**/*.js`)
    .filter(isJsFile)
    .forEach((file) => {
      console.log(`Removing types from "${file}`);
      const input = fs.readFileSync(file, 'utf8');
      const output = flowRemoveTypes(input);
      fs.writeFileSync(file, output);
    });

  // Now do an eslint fix parse
  exec(`eslint --fix ${srcPath}`);
};

if (fs.existsSync('src/shared/universal/types')) {
  rimraf('src/shared/universal/types', (err) => {
    if (err) {
      console.error('Could not remove "src/shared/universal/types" directory.');
    } else {
      stripTypes();
    }
  });
} else {
  stripTypes();
}<commit_msg>Improves the flow remove types script.<commit_after>const globSync = require('glob').sync;
const path = require('path');
const appRootPath = require('app-root-path').toString();
const flowRemoveTypes = require('flow-remove-types');
const fs = require('fs');
const rimraf = require('rimraf');
const { exec } = require('../utils');

const srcPath = path.resolve(appRootPath, 'src');

const isJsFile = file => path.extname(file) === '.js';

const stripTypes = () => {
  globSync(`${path.resolve(appRootPath, 'src')}/**/*.js`)
    .filter(isJsFile)
    .forEach((file) => {
      console.log(`Removing types from "${file}`);
      const input = fs.readFileSync(file, 'utf8');
      const output =
        // Remove flow annotations
        flowRemoveTypes(input)
        // Remove the empty flow tags.
        .replace(/\/\*\s+\*\/\n/g, '')
        // Remove any blank lines at top of file.
        .replace(/^\n+/, '')
        // Remove any multiple blank lines in files.
        .replace(/\n\n\n/g, '\n\n');
      fs.writeFileSync(file, output);
    });

  // Now do an eslint fix parse
  exec(`eslint --fix ${srcPath}`);
};

if (fs.existsSync('src/shared/universal/types')) {
  rimraf('src/shared/universal/types', (err) => {
    if (err) {
      console.error('Could not remove "src/shared/universal/types" directory.');
    } else {
      stripTypes();
    }
  });
} else {
  stripTypes();
}
{'fraction_non_alphanumeric': 0.1931174089068826, 'fraction_numerical': 0.0008097165991902834, 'mean_word_length': 3.260344827586207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '3839936', 'n_tokens_mistral': 791, 'n_tokens_neox': 787, 'n_words': 178}