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

'use strict';

const fs = require('fs');
const program = require('commander');

const CommandLineEditor = require('../lib/CommandLineEditor');
const Importer = require('../lib/Importer');
const packageJson = require('../package.json');

/**
 * @param {String} str
 */
function stdoutWrite(str) {
  process.stdout.write(`${str}\n`);
}

/**
 * Grab lines from stdin or directly from the file.
 * @param {String} pathToFile
 * @param {Function} callback
 */
function getLines(pathToFile, callback) {
  if (process.stdin.isTTY) {
    fs.readFile(pathToFile, 'utf-8', (err, fileContent) => {
      if (err) throw err;
      callback(fileContent.split('\n'));
    });
    return;
  }
  const lines = [];
  process.stdin.resume();
  process.stdin.setEncoding('utf-8');
  process.stdin.on('data', data => lines.push(...data.split('\n')));
  process.stdin.on('end', () => callback(lines));
}

/**
 * Run a command/method on an importer instance
 * @param {Function} executor
 * @param {String} pathToFile
 * @param {Object} options
 * @param {Boolean} options.overwrite
 */
function runCommand(executor, pathToFile, options) {
  const overwrite = options.overwrite;

  getLines(pathToFile, (lines) => {
    const editor = new CommandLineEditor(lines);
    const importer = new Importer(editor, pathToFile);
    executor(importer);
    if (overwrite) {
      fs.writeFile(pathToFile, editor.currentFileContent(), (err) => {
        if (err) throw err;
      });
    } else {
      stdoutWrite(JSON.stringify(editor.toJSON()));
    }
  });
}

program.version(packageJson.version);

// Some options that are shared by multiple commands
const sharedOptions = {
  overwrite: [
    '--overwrite',
    'overwrite the file with the result after importing',
  ],
};

program.command('word <word> <pathToFile>')
  .option(...sharedOptions.overwrite)
  .action((word, pathToFile, options) => {
    runCommand(importer => importer.import(word), pathToFile, options);
  });

program.command('fix <pathToFile>')
  .option(...sharedOptions.overwrite)
  .action((pathToFile, options) => {
    runCommand(importer => importer.fixImports(), pathToFile, options);
  });

program.command('rewrite <pathToFile>')
  .option(...sharedOptions.overwrite)
  .action((pathToFile, options) => {
    runCommand(importer => importer.rewriteImports(), pathToFile, options);
  });

program.command('add <imports> <pathToFile>')
  .option(...sharedOptions.overwrite)
  .action((imports, pathToFile, options) => {
    runCommand(importer => importer.addImports(JSON.parse(imports)),
               pathToFile, options);
  });

program.command('goto <word> <pathToFile>')
  .action((word, pathToFile) => {
    getLines(pathToFile, (lines) => {
      const editor = new CommandLineEditor(lines, program);
      stdoutWrite(new Importer(editor, pathToFile).goto(word));
    });
  });

program.on('--help', () => {<commit_msg>Fix binary after refactoring Importer<commit_after>#!/usr/bin/env node

'use strict';

const fs = require('fs');
const program = require('commander');

const Importer = require('../lib/Importer');
const packageJson = require('../package.json');

/**
 * @param {String} str
 */
function stdoutWrite(str) {
  process.stdout.write(`${str}\n`);
}

/**
 * Grab lines from stdin or directly from the file.
 * @param {String} pathToFile
 * @param {Function} callback
 */
function getLines(pathToFile, callback) {
  if (process.stdin.isTTY) {
    fs.readFile(pathToFile, 'utf-8', (err, fileContent) => {
      if (err) throw err;
      callback(fileContent.split('\n'));
    });
    return;
  }
  const lines = [];
  process.stdin.resume();
  process.stdin.setEncoding('utf-8');
  process.stdin.on('data', data => lines.push(...data.split('\n')));
  process.stdin.on('end', () => callback(lines));
}

/**
 * Run a command/method on an importer instance
 * @param {Function} executor
 * @param {String} pathToFile
 * @param {Object} options
 * @param {Boolean} options.overwrite
 */
function runCommand(executor, pathToFile, options) {
  const overwrite = options.overwrite;

  getLines(pathToFile, (lines) => {
    const importer = new Importer(lines, pathToFile);
    const result = executor(importer);
    if (overwrite) {
      fs.writeFile(pathToFile, result.fileContent, (err) => {
        if (err) throw err;
      });
    } else {
      stdoutWrite(JSON.stringify(result));
    }
  });
}

program.version(packageJson.version);

// Some options that are shared by multiple commands
const sharedOptions = {
  overwrite: [
    '--overwrite',
    'overwrite the file with the result after importing',
  ],
};

program.command('word <word> <pathToFile>')
  .option(...sharedOptions.overwrite)
  .action((word, pathToFile, options) => {
    runCommand(importer => importer.import(word), pathToFile, options);
  });

program.command('fix <pathToFile>')
  .option(...sharedOptions.overwrite)
  .action((pathToFile, options) => {
    runCommand(importer => importer.fixImports(), pathToFile, options);
  });

program.command('rewrite <pathToFile>')
  .option(...sharedOptions.overwrite)
  .action((pathToFile, options) => {
    runCommand(importer => importer.rewriteImports(), pathToFile, options);
  });

program.command('add <imports> <pathToFile>')
  .option(...sharedOptions.overwrite)
  .action((imports, pathToFile, options) => {
    runCommand(importer => importer.addImports(JSON.parse(imports)),
               pathToFile, options);
  });

program.command('goto <word> <pathToFile>')
  .action((word, pathToFile) => {
    getLines(pathToFile, (lines) => {
      stdoutWrite(JSON.stringify(new Importer(lines, pathToFile).goto(word)));
    });
  });

program.on('--help', () => {
{'fraction_non_alphanumeric': 0.17249253469172668, 'fraction_numerical': 0.0007026172492534692, 'mean_word_length': 3.9816272965879267, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 51, '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': '6461423', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1837, 'n_words': 374}