<commit_before>#!/usr/bin/env node

var args = require('minimist')(process.argv.slice(2), {
  boolean: ['d', 'dry-run', 'h', 'help'],
  default: {
    'log-file': 'tmp-debug.log'
  },
  string: ['i', 'ignore']
});
var chalk = require('chalk');
var cwd = process.cwd();
var exit = process.exit;
var fs = require('fs');
var getFnParams = require('get-fn-params');
var path = require('path');
var resolve = path.resolve;
var exitCodes = {
  NO_JS_FILES_FOUND: 5,
  NO_DIR_ARG: 2,
  UNKOWN: 4,
};

var dirs = args._.map(toAbsolute(cwd));
var logFile = args['log-file'];

var directoriesToProcess = [];
var files = [];

if (args.h || args.help) {
  help(0);
}

if (!dirs.length) {
  error('No directory was specified!');
  help(exitCodes.NO_DIR_ARG);
}

dirs.forEach(handleArgDir);

if (!files || !files.length) {
  error('No .js files found!');
  exit(exitCodes.NO_JS_FILES_FOUND);
}

if (args.d || args['dry-run']) {
  console.log(files);
  console.log(files.length + ' file[s] were found.');
  console.log('Dry run detected.');
  exit(0);
}

files.forEach(instrument);

function error(msg) {
  console.error('\n' + chalk.red(msg));
}

function handleArgDir(dir) {
  var dirStat = fs.statSync(dir);

  if (!dirStat.isDirectory()) {
    error(dir + ' was not a directory!\n\nYou must provide directories.');
    exit(1);
  }

  directoriesToProcess.push(fs.readdirSync(dir).map(toAbsolute(dir)));

  while (directoriesToProcess.length) {
    handleDirectory(directoriesToProcess.shift());
  }
}

function handleDirectory(directory) {
  directory.forEach(function(node) {
    var stat = fs.statSync(node);
    if (stat.isFile() && /\.js$/.test(node)) {
      files.push(node);
    }
  });
}

function help(code) {
  require('./help')();
  exit(code);
}

function instrument(file) {
  var contents = fs.readFileSync(file, {encoding: 'utf8'});

  contents = contents
    .replace(
        /(function(?! __tmpDebug)(?:(?!\{)[\s\S])+\{)(?!__tmpDebug)/gm,
        function (match, fn) {
          return fn +<commit_msg>Fixing instrumentation regex.<commit_after>#!/usr/bin/env node

var args = require('minimist')(process.argv.slice(2), {
  boolean: ['d', 'dry-run', 'h', 'help'],
  default: {
    'log-file': 'tmp-debug.log'
  },
  string: ['i', 'ignore']
});
var chalk = require('chalk');
var cwd = process.cwd();
var exit = process.exit;
var fs = require('fs');
var getFnParams = require('get-fn-params');
var path = require('path');
var resolve = path.resolve;
var exitCodes = {
  NO_JS_FILES_FOUND: 5,
  NO_DIR_ARG: 2,
  UNKOWN: 4,
};

var dirs = args._.map(toAbsolute(cwd));
var logFile = args['log-file'];

var directoriesToProcess = [];
var files = [];

if (args.h || args.help) {
  help(0);
}

if (!dirs.length) {
  error('No directory was specified!');
  help(exitCodes.NO_DIR_ARG);
}

dirs.forEach(handleArgDir);

if (!files || !files.length) {
  error('No .js files found!');
  exit(exitCodes.NO_JS_FILES_FOUND);
}

if (args.d || args['dry-run']) {
  console.log(files);
  console.log(files.length + ' file[s] were found.');
  console.log('Dry run detected.');
  exit(0);
}

files.forEach(instrument);

function error(msg) {
  console.error('\n' + chalk.red(msg));
}

function handleArgDir(dir) {
  var dirStat = fs.statSync(dir);

  if (!dirStat.isDirectory()) {
    error(dir + ' was not a directory!\n\nYou must provide directories.');
    exit(1);
  }

  directoriesToProcess.push(fs.readdirSync(dir).map(toAbsolute(dir)));

  while (directoriesToProcess.length) {
    handleDirectory(directoriesToProcess.shift());
  }
}

function handleDirectory(directory) {
  directory.forEach(function(node) {
    var stat = fs.statSync(node);
    if (stat.isFile() && /\.js$/.test(node)) {
      files.push(node);
    }
  });
}

function help(code) {
  require('./help')();
  exit(code);
}

function instrument(file) {
  var contents = fs.readFileSync(file, {encoding: 'utf8'});

  contents = contents
    .replace(
        /(function\s*([a-z][a-z0-9$_]*)?\s*\([^)]*\)\s*\{)(?!__tmpDebug)/gim,
        function (match, fn) {
          return fn +
{'fraction_non_alphanumeric': 0.19767441860465115, 'fraction_numerical': 0.004453240969816922, 'mean_word_length': 3.8946731234866827, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7172460', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1481, 'n_words': 287}