<commit_before>/* @flow */

import { sync as globSync } from 'glob';
import path from 'path';
import appRootDir from 'app-root-dir';
import flowRemoveTypes from 'flow-remove-types';
import fs from 'fs';
import rimraf from 'rimraf';
import { exec } from '../utils';

const appRootPath = appRootDir.get();

function safeDelete(target, cb) {
  if (fs.existsSync(target)) {
    rimraf(target, (err) => {
      if (err) {
        console.log('Failed to delete:', target);
      }
      if (cb) {
        cb();
      }
    });
  }
}

const srcPath = path.resolve(appRootPath, 'src');
const flowConfigPath = path.resolve(appRootPath, '.flowconfig');
const packageJsonPath = path.resolve(appRootPath, 'package.json');
const flowToolsPath = path.resolve(appRootPath, 'tools/flow');
const flowTypesPath = path.resolve(appRootPath, 'src/shared/universal/types');
const flowScriptPath = path.resolve(appRootPath, 'tools/scripts/flow.js');

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

// Strip the flow types from our src files.
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);
  });

// Delete the types folder in the src
safeDelete(
  flowTypesPath,
  // Then do an eslint fix parse on the src files.
  () => exec(`eslint --fix ${srcPath}`),
);

// Remove the .flowconfig
safeDelete(flowConfigPath);

// Remove the flow tools<commit_msg>Updates flow:remove command to strip flow types from the tools.<commit_after>/* @flow */

import { sync as globSync } from 'glob';
import path from 'path';
import appRootDir from 'app-root-dir';
import flowRemoveTypes from 'flow-remove-types';
import fs from 'fs';
import rimraf from 'rimraf';
import { exec } from '../utils';

const appRootPath = appRootDir.get();

function safeDelete(target, cb) {
  if (fs.existsSync(target)) {
    rimraf(target, (err) => {
      if (err) {
        console.log('Failed to delete:', target);
      }
      if (cb) {
        cb();
      }
    });
  }
}

const srcPath = path.resolve(appRootPath, 'src');
const flowConfigPath = path.resolve(appRootPath, '.flowconfig');
const packageJsonPath = path.resolve(appRootPath, 'package.json');
const flowToolsPath = path.resolve(appRootPath, 'tools/flow');
const flowTypesPath = path.resolve(appRootPath, 'src/shared/universal/types');
const flowScriptPath = path.resolve(appRootPath, 'tools/scripts/flow.js');

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

// Strip the flow types from our src files.
globSync(`${path.resolve(appRootPath, 'src')}/**/*.js`)
  .concat(globSync(`${path.resolve(appRootPath, 'tools')}/**/*.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);
  });

// Delete the types folder in the src
safeDelete(
  flowTypesPath,
  // Then do an eslint fix parse on the src files.
  () => exec(`eslint --fix ${srcPath}`),
);

// Remove the .flowconfig
safeDelete(flowConfigPath);

// Remove the flow tools
{'fraction_non_alphanumeric': 0.16600790513833993, 'fraction_numerical': 0.0005270092226613965, 'mean_word_length': 3.3136363636363635, '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': '334222', 'n_tokens_mistral': 1216, 'n_tokens_neox': 1215, 'n_words': 334}