<commit_before>var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var Filter = require('broccoli-filter');
var Promise = require('rsvp').Promise;

function Fingerprint(inputTree, options) {
  if (!(this instanceof Fingerprint)) {
    return new Fingerprint(inputTree, options);
  }

  options = options || {};

  this.inputTree = inputTree;
  this.assetMap = options.assetMap || {};
  this.extensions = options.extensions || [];
  this.exclude = options.exclude || [];
  this.description = options.description;
  this.generateAssetMap = options.generateAssetMap;
  this.generateRailsManifest = options.generateRailsManifest;
  this.prepend = options.prepend;

  if (typeof options.customHash === 'function') {
    this.customHash = null;
    this.hashFn = options.customHash;
  } else {
    this.customHash = options.customHash;
    this.hashFn = md5Hash;
  }
}

Fingerprint.prototype = Object.create(Filter.prototype);
Fingerprint.prototype.constructor = Fingerprint;

Fingerprint.prototype.canProcessFile = function (relativePath) {
  for (var i = 0; i < this.exclude.length; i++) {
    if (relativePath.indexOf(this.exclude[i]) !== -1) {
      return false;
    }
  }

  return Filter.prototype.getDestFilePath.apply(this, arguments) != null;
};

Fingerprint.prototype.processFile = function (srcDir, destDir, relativePath) {
  this._srcDir = srcDir;

  var file = fs.readFileSync(srcDir + '/' + relativePath);
  var self = this;

  return Promise.resolve().then(function () {
    var outputPath = self.getDestFilePath(relativePath);
    fs.writeFileSync(destDir + '/' + outputPath, file);
  });
};

Fingerprint.prototype.getDestFilePath = function (relativePath) {
  if (Filter.prototype.getDestFilePath.apply(this, arguments)) {
    if (this.assetMap[relativePath]) {
      return this.assetMap[relativePath];
    }

    var tmpPath = path.join(this._srcDir, relativePath);
    var file = fs.readFileSync(tmpPath, { encoding: 'utf8' });
    var hash;

    if (this.customHash) {
      hash = this.customHash;
    } else {
      hash = this.hashFn(file);
    }

    var ext = path.extname(relativePath);
    var newPath = relativePath.replace(ext, '-' + hash + ext);
    this.assetMap[relativePath] = newPath;

    return newPath;
  }

  return null;
};

Fingerprint.prototype.writeAssetMap = function (destDir) {
  var toWrite = {
    assets: this.assetMap,
    prepend: this.prepend
  };

  if (!fs.existsSync(destDir + '/assets')) {
    fs.mkdirSync(destDir + '/assets');
  }

  fs.writeFileSync(destDir + '/assets/assetMap.json', JSON.stringify(toWrite));
};

Fingerprint.prototype.writeRailsManifest = function(destDir) {
    var assetRegex = /^assets\//,
        digestRegex = /-([0-9a-f]+)\.\w+$/,<commit_msg>Pass tmpPath to hashFn as second param<commit_after>var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var Filter = require('broccoli-filter');
var Promise = require('rsvp').Promise;

function Fingerprint(inputTree, options) {
  if (!(this instanceof Fingerprint)) {
    return new Fingerprint(inputTree, options);
  }

  options = options || {};

  this.inputTree = inputTree;
  this.assetMap = options.assetMap || {};
  this.extensions = options.extensions || [];
  this.exclude = options.exclude || [];
  this.description = options.description;
  this.generateAssetMap = options.generateAssetMap;
  this.generateRailsManifest = options.generateRailsManifest;
  this.prepend = options.prepend;

  if (typeof options.customHash === 'function') {
    this.customHash = null;
    this.hashFn = options.customHash;
  } else {
    this.customHash = options.customHash;
    this.hashFn = md5Hash;
  }
}

Fingerprint.prototype = Object.create(Filter.prototype);
Fingerprint.prototype.constructor = Fingerprint;

Fingerprint.prototype.canProcessFile = function (relativePath) {
  for (var i = 0; i < this.exclude.length; i++) {
    if (relativePath.indexOf(this.exclude[i]) !== -1) {
      return false;
    }
  }

  return Filter.prototype.getDestFilePath.apply(this, arguments) != null;
};

Fingerprint.prototype.processFile = function (srcDir, destDir, relativePath) {
  this._srcDir = srcDir;

  var file = fs.readFileSync(srcDir + '/' + relativePath);
  var self = this;

  return Promise.resolve().then(function () {
    var outputPath = self.getDestFilePath(relativePath);
    fs.writeFileSync(destDir + '/' + outputPath, file);
  });
};

Fingerprint.prototype.getDestFilePath = function (relativePath) {
  if (Filter.prototype.getDestFilePath.apply(this, arguments)) {
    if (this.assetMap[relativePath]) {
      return this.assetMap[relativePath];
    }

    var tmpPath = path.join(this._srcDir, relativePath);
    var file = fs.readFileSync(tmpPath, { encoding: 'utf8' });
    var hash;

    if (this.customHash) {
      hash = this.customHash;
    } else {
      hash = this.hashFn(file, tmpPath);
    }

    var ext = path.extname(relativePath);
    var newPath = relativePath.replace(ext, '-' + hash + ext);
    this.assetMap[relativePath] = newPath;

    return newPath;
  }

  return null;
};

Fingerprint.prototype.writeAssetMap = function (destDir) {
  var toWrite = {
    assets: this.assetMap,
    prepend: this.prepend
  };

  if (!fs.existsSync(destDir + '/assets')) {
    fs.mkdirSync(destDir + '/assets');
  }

  fs.writeFileSync(destDir + '/assets/assetMap.json', JSON.stringify(toWrite));
};

Fingerprint.prototype.writeRailsManifest = function(destDir) {
    var assetRegex = /^assets\//,
        digestRegex = /-([0-9a-f]+)\.\w+$/,
{'fraction_non_alphanumeric': 0.14422382671480144, 'fraction_numerical': 0.0021660649819494585, 'mean_word_length': 3.7932525951557095, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5479505', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1743, 'n_words': 342}