<commit_before>/*
 *
 * Diff to HTML CLI (cli.js)
 * Author: rtfpessoa
 *
 */

(function() {
  var os = require('os');
  var path = require('path');

  var diff2Html = require('diff2html').Diff2Html;

  var log = require('./logger.js').Logger;
  var http = require('./http-utils.js').HttpUtils;
  var utils = require('./utils.js').Utils;

  var open = require('open');
  var ncp = require('copy-paste');

  function Diff2HtmlInterface() {
  }

  /*
   * Input
   */

  Diff2HtmlInterface.prototype.getInput = function getInput(inputType, inputArgs, callback) {
    var that = this;
    switch (inputType) {
      case 'file':
        utils.readFile(inputArgs[0], callback);
        break;

      case 'stdin':
        utils.readStdin(callback);
        break;

      default:
        that._runGitDiff(inputArgs, callback);
    }
  };

  Diff2HtmlInterface.prototype._runGitDiff = function(gitArgsArr, callback) {
    var gitArgs;
    if (gitArgsArr.length && gitArgsArr[0]) {
      gitArgs = gitArgsArr.map(function(arg) {
        return '"' + arg + '"'; // wrap parameters
      }).join(' ');
    } else {
      gitArgs = '-M HEAD';
    }

    var diffCommand = 'git diff ' + gitArgs;
    return callback(null, utils.runCmd(diffCommand));
  };

  /*
   * Output
   */

  Diff2HtmlInterface.prototype.getOutput = function(baseConfig, input, callback) {
    var that = this;
    var config = baseConfig;
    config.wordByWord = (baseConfig.diff === 'word');
    config.charByChar = (baseConfig.diff === 'char');

    var jsonContent = diff2Html.getJsonFromDiff(input, config);

    if (baseConfig.format === 'html') {
      config.inputFormat = 'json';

      if (baseConfig.style === 'side') {
        config.outputFormat = 'side-by-side';
      } else {
        config.outputFormat = 'line-by-line';
      }

      if (baseConfig.summary === 'hidden') {
        config.showFiles = false;
      } else {
        config.showFiles = true;
        config.showFilesOpen = baseConfig.summary === 'open';
      }

      config.synchronisedScroll = (baseConfig.synchronisedScroll === 'enabled');

      var htmlContent = diff2Html.getPrettyHtml(jsonContent, config);
      return callback(null, that._prepareHTML(htmlContent, config.showFilesOpen));
    } else if (baseConfig.format === 'json') {
      return callback(null, JSON.stringify(jsonContent));
    }

    return callback(new Error('Wrong output format `' + baseConfig.format + '`!'));
  };

  Diff2HtmlInterface.prototype._prepareHTML = function(content, showFilesOpen) {
    var templatePath = path.resolve(__dirname, '..', 'dist', 'template.html');
    var template = utils.readFileSync(templatePath);

    var cssFilePath = path.resolve(__dirname, '..', 'node_modules', 'diff2html', 'dist', 'diff2html.min.css');
    var cssContent = utils.readFileSync(cssFilePath);

    var jsUiFilePath = path.resolve(__dirname, '..', 'node_modules', 'diff2html', 'dist', 'diff2html-ui.min.js');
    var jsUiContent = utils.readFileSync(jsUiFilePath);

    return template
      .replace('<!--diff2html-css-->', '<style>\n' + cssContent + '\n</style>')
      .replace('<!--diff2html-js-ui-->', '<script>\n' + jsUiContent + '\n</script>')
      .replace('//diff2html-fileListCloseable', 'diff2htmlUi.fileListCloseable("#diff", ' + showFilesOpen + ');')
      .replace('<!--diff2html-diff-->', content);
  };

  /*
   * Output destination
   */

  Diff2HtmlInterface.prototype.preview = function(content, format) {
    var filename = 'diff.' + format;
    var filePath = path.resolve(os.tmpdir(), filename);
    utils.writeFile(filePath, content);
    open(filePath);
  };

  Diff2HtmlInterface.prototype.postToDiffy = function(diff, postType, callback) {
    var jsonParams = {udiff: diff};

    http.post('http://diffy.org/api/new', jsonParams, function(err, response) {
      if (err) {
        log.error(err);
        return;<commit_msg>Fix usage as a local dependency<commit_after>/*
 *
 * Diff to HTML CLI (cli.js)
 * Author: rtfpessoa
 *
 */

(function() {
  var os = require('os');
  var path = require('path');

  var diff2Html = require('diff2html').Diff2Html;

  var log = require('./logger.js').Logger;
  var http = require('./http-utils.js').HttpUtils;
  var utils = require('./utils.js').Utils;

  var open = require('open');
  var ncp = require('copy-paste');

  function Diff2HtmlInterface() {
  }

  /*
   * Input
   */

  Diff2HtmlInterface.prototype.getInput = function getInput(inputType, inputArgs, callback) {
    var that = this;
    switch (inputType) {
      case 'file':
        utils.readFile(inputArgs[0], callback);
        break;

      case 'stdin':
        utils.readStdin(callback);
        break;

      default:
        that._runGitDiff(inputArgs, callback);
    }
  };

  Diff2HtmlInterface.prototype._runGitDiff = function(gitArgsArr, callback) {
    var gitArgs;
    if (gitArgsArr.length && gitArgsArr[0]) {
      gitArgs = gitArgsArr.map(function(arg) {
        return '"' + arg + '"'; // wrap parameters
      }).join(' ');
    } else {
      gitArgs = '-M HEAD';
    }

    var diffCommand = 'git diff ' + gitArgs;
    return callback(null, utils.runCmd(diffCommand));
  };

  /*
   * Output
   */

  Diff2HtmlInterface.prototype.getOutput = function(baseConfig, input, callback) {
    var that = this;
    var config = baseConfig;
    config.wordByWord = (baseConfig.diff === 'word');
    config.charByChar = (baseConfig.diff === 'char');

    var jsonContent = diff2Html.getJsonFromDiff(input, config);

    if (baseConfig.format === 'html') {
      config.inputFormat = 'json';

      if (baseConfig.style === 'side') {
        config.outputFormat = 'side-by-side';
      } else {
        config.outputFormat = 'line-by-line';
      }

      if (baseConfig.summary === 'hidden') {
        config.showFiles = false;
      } else {
        config.showFiles = true;
        config.showFilesOpen = baseConfig.summary === 'open';
      }

      config.synchronisedScroll = (baseConfig.synchronisedScroll === 'enabled');

      var htmlContent = diff2Html.getPrettyHtml(jsonContent, config);
      return callback(null, that._prepareHTML(htmlContent, config.showFilesOpen));
    } else if (baseConfig.format === 'json') {
      return callback(null, JSON.stringify(jsonContent));
    }

    return callback(new Error('Wrong output format `' + baseConfig.format + '`!'));
  };

  Diff2HtmlInterface.prototype._prepareHTML = function(content, showFilesOpen) {
    var templatePath = path.resolve(__dirname, '..', 'dist', 'template.html');
    var template = utils.readFileSync(templatePath);

    var diff2htmlPath = path.join(path.dirname(require.resolve('diff2html')), '..');

    var cssFilePath = path.resolve(diff2htmlPath, 'dist', 'diff2html.min.css');
    var cssContent = utils.readFileSync(cssFilePath);

    var jsUiFilePath = path.resolve(diff2htmlPath, 'dist', 'diff2html-ui.min.js');
    var jsUiContent = utils.readFileSync(jsUiFilePath);

    return template
      .replace('<!--diff2html-css-->', '<style>\n' + cssContent + '\n</style>')
      .replace('<!--diff2html-js-ui-->', '<script>\n' + jsUiContent + '\n</script>')
      .replace('//diff2html-fileListCloseable', 'diff2htmlUi.fileListCloseable("#diff", ' + showFilesOpen + ');')
      .replace('<!--diff2html-diff-->', content);
  };

  /*
   * Output destination
   */

  Diff2HtmlInterface.prototype.preview = function(content, format) {
    var filename = 'diff.' + format;
    var filePath = path.resolve(os.tmpdir(), filename);
    utils.writeFile(filePath, content);
    open(filePath);
  };

  Diff2HtmlInterface.prototype.postToDiffy = function(diff, postType, callback) {
    var jsonParams = {udiff: diff};

    http.post('http://diffy.org/api/new', jsonParams, function(err, response) {
      if (err) {
        log.error(err);
        return;
{'fraction_non_alphanumeric': 0.1596208045093518, 'fraction_numerical': 0.006149116064565719, 'mean_word_length': 3.111111111111111, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '7164110', 'n_tokens_mistral': 2463, 'n_tokens_neox': 2406, 'n_words': 467}