<commit_before>'use strict'

const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const assign = require('lodash.assign')
const makeTemplate = require('./make-template.js')
const defaultDirectives = require('./default-directives.js')

module.exports = function (directory, settings) {
  settings = settings || {}
  directory = path.resolve(process.cwd(), directory) + '/'
  settings.cacheDirectory = settings.cacheDirectory || directory + 'compiled/' + (new Date()).getTime() + '/'

  var directives = assign({}, defaultDirectives, settings.directives || {})
  var promises = {}

  return function load (name) {
    if (!promises[name]) {
      promises[name] = new Promise(function (resolve, reject) {
        fs.readFile(directory + name, { encoding: 'utf-8' }, function (err, result) {
          if (err) throw err

          makeTemplate(result, load, directives, function (err, result) {
            if (err) throw err

            mkdirp(path.dirname(settings.cacheDirectory + name + '.js'), function (err) {
              if (err) throw err

              fs.writeFile(settings.cacheDirectory + name + '.js', result, function (err) {
                if (err) throw err

                resolve(settings.cacheDirectory + name + '.js')
              })
            })
          })
        })
      })
    }

    return promises[name].then(function (path) {
      var Template = require(path)

      return Promise.resolve(function (content) {
        return (new Template()).render(content)
      })
    })
  }
}
<commit_msg>Delete require cache rather than create endless directories<commit_after>'use strict'

const fs = require('fs')
const path = require('path')
const mkdirp = require('mkdirp')
const assign = require('lodash.assign')
const makeTemplate = require('./make-template.js')
const defaultDirectives = require('./default-directives.js')

module.exports = function (directory, settings) {
  settings = settings || {}
  directory = path.resolve(process.cwd(), directory) + '/'
  settings.cacheDirectory = settings.cacheDirectory || directory + 'compiled/'

  var directives = assign({}, defaultDirectives, settings.directives || {})
  var promises = {}

  return function load (name) {
    if (!promises[name]) {
      promises[name] = new Promise(function (resolve, reject) {
        fs.readFile(directory + name, { encoding: 'utf-8' }, function (err, result) {
          if (err) throw err

          makeTemplate(result, load, directives, function (err, result) {
            if (err) throw err

            mkdirp(path.dirname(settings.cacheDirectory + name + '.js'), function (err) {
              if (err) throw err

              fs.writeFile(settings.cacheDirectory + name + '.js', result, function (err) {
                if (err) throw err

                resolve(settings.cacheDirectory + name + '.js')
              })
            })
          })
        })
      })
    }

    return promises[name].then(function (path) {
      delete require.cache[path]

      var Template = require(path)

      return Promise.resolve(function (content) {
        return (new Template()).render(content)
      })
    })
  }
}

{'fraction_non_alphanumeric': 0.1388101983002833, 'fraction_numerical': 0.0006295247088448222, 'mean_word_length': 2.6570771001150746, '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': '7339060', 'n_tokens_mistral': 878, 'n_tokens_neox': 847, 'n_words': 223}