<commit_before># grunt-coffee-build
# https://github.com/tarruda/grunt-coffee-build
#
# Copyright (c) 2013 <NAME>
# Licensed under the MIT license.

fs = require('fs')
path = require('path')
handlebars = require('handlebars')
browserify = require('browserify')
parseScope = require('lexical-scope')

{compile} = require 'coffee-script'
{SourceMapConsumer, SourceMapGenerator} = require 'source-map'
UglifyJS = require 'uglify-js'

NAME = 'coffee_build'
DESC =
  'Compiles Coffeescript files, optionally merging and generating source maps.'

# Cache of compiledfiles, keeps track of the last modified date
# so we can avoid processing it again.
buildCache = browserifyCache = timestampCache = null
TIMESTAMP_CACHE = '.timestamp_cache~'
BUILD_CACHE = '.build_cache~'


mtime = (fp) -> fs.statSync(fp).mtime.getTime()


process.on('exit', ->
  # save caches to disk to speed up future builds
  if timestampCache
    fs.writeFileSync(TIMESTAMP_CACHE, JSON.stringify(timestampCache))
  if buildCache
    fs.writeFileSync(BUILD_CACHE, JSON.stringify(buildCache))
  # don't save browserify build to disk since it can get complicated
  # in situations where only dependencies versions change
)


process.on('SIGINT', process.exit)
process.on('SIGTERM', process.exit)


buildToDirectory = (grunt, options, src) ->
  cwd = options.src_base
  outDir = options.dest

  if not timestampCache
    if grunt.file.exists(TIMESTAMP_CACHE)
      timestampCache = grunt.file.readJSON(TIMESTAMP_CACHE)
    else
      timestampCache = {}

  if not grunt.file.exists(outDir)
    grunt.file.mkdir(outDir)

  src.forEach (file) ->
    if file.indexOf(cwd) != 0
      file = path.join(cwd, file)
    if not grunt.file.exists(file)
      grunt.log.warn('Source file "' + file + '" not found.')
      return
    outFile = path.join(outDir, path.relative(cwd, file))
    entry = timestampCache[file]
    mt = mtime(file)
    if /\.js/.test(outFile)
      if not grunt.file.exists(outFile) or mt != entry?.mtime
        # plain js, just copy to the output dir
        grunt.file.copy(file, outFile)
        grunt.log.ok("Copied #{file} to #{outFile}")
        timestampCache[file] = mtime: mt
      return
    outFile = outFile.replace(/\.coffee$/, '.js')
    fileOutDir = path.dirname(outFile)
    if not grunt.file.exists(outFile) or mt != entry?.mtime
      src = grunt.file.read(file)
      try
        compiled = compile(src, {
          sourceMap: options.sourceMap
          bare: false
        })
      catch e
        grunt.log.error("#{e.message}(file: #{file}, line: #{e.location.last_line + 1}, column: #{e.location.last_column})")
        throw e
      grunt.log.ok("Compiled #{file}")
      if options.sourceMap
        {js: compiled, v3SourceMap} = compiled
        v3SourceMap = JSON.parse(v3SourceMap)
        v3SourceMap.sourceRoot = path.relative(fileOutDir, cwd)
        v3SourceMap.file = path.basename(outFile)
        v3SourceMap.sources[0] = path.relative(cwd, file)
        v3SourceMap = JSON.stringify(v3SourceMap)
        compiled += "\n\n//@ sourceMappingURL=#{path.basename(outFile)}.map"
        grunt.file.write("#{outFile}.map", v3SourceMap)
        grunt.log.ok("File #{outFile}.map was created")
      timestampCache[file] = mtime: mt
      grunt.file.write(outFile, compiled)
      grunt.log.ok("File #{outFile} was created")

# Function adapted from the helper function with same name  thein traceur
# compiler source code.
#
# It generates an ugly identifier for a given pathname relative to
# the current file being processed, taking into consideration a base dir. eg:
# > generateNameForUrl('./b/c') # if the filename is 'a'
# '$$__a_b_c'
# > generateNameForUrl('../d') # if the filename is 'a/b/c'
# '$$__a_b_d'
#
# This assumes you won't name your variables using this prefix
generateNameForUrl = (grunt, url, from, cwd = '.', prefix = '$$__$$__') ->
  try
    cwd = path.resolve(cwd)
    from = path.resolve(path.dirname(from))
    url = path.resolve(path.join(from, url))
    if grunt.file.isDir(url)
      # its possible to require directories that have an index.coffee file
      url = path.join(url, 'index')
    ext = /\.(coffee|js)$/
    if ext.test(url)
      url = url.replace(ext, '')
  catch e
    grunt.log.warn(e)
    return null
  id = "$#{prefix + url.replace(cwd, '').replace(/[^\d\w$]/g, '_')}"
  return {id: id, url: path.relative(cwd, url)}


# Replace all the require calls by the generated identifier that represents
# the corresponding module. This will also fill the 'deps' array so the 
# main routine can concatenate the files in proper order. Require calls to
# non relative paths will be included in the requires array to be declared
# in the umd wrapper
replaceRequires = (grunt, js, fn, fp, cwd, deps, requires) ->
  displayNode = (node) -> js.slice(node.start.pos, node.end.endpos)
  transformer = new UglifyJS.TreeTransformer (node, descend) ->
    if (not (node instanceof UglifyJS.AST_Call) or
      node.expression.name != 'require' or
      (node.args.length != 1 or
       not /^\.(?:\.)?\//.test(node.args[0].value) and
       grunt.log.writeln(
         "Absolute 'require' call in file #{fn}: '#{displayNode(node)}'")) or<commit_msg>Restore old module name prefix<commit_after># grunt-coffee-build
# https://github.com/tarruda/grunt-coffee-build
#
# Copyright (c) 2013 Th<NAME>
# Licensed under the MIT license.

fs = require('fs')
path = require('path')
handlebars = require('handlebars')
browserify = require('browserify')
parseScope = require('lexical-scope')

{compile} = require 'coffee-script'
{SourceMapConsumer, SourceMapGenerator} = require 'source-map'
UglifyJS = require 'uglify-js'

NAME = 'coffee_build'
DESC =
  'Compiles Coffeescript files, optionally merging and generating source maps.'

# Cache of compiledfiles, keeps track of the last modified date
# so we can avoid processing it again.
buildCache = browserifyCache = timestampCache = null
TIMESTAMP_CACHE = '.timestamp_cache~'
BUILD_CACHE = '.build_cache~'


mtime = (fp) -> fs.statSync(fp).mtime.getTime()


process.on('exit', ->
  # save caches to disk to speed up future builds
  if timestampCache
    fs.writeFileSync(TIMESTAMP_CACHE, JSON.stringify(timestampCache))
  if buildCache
    fs.writeFileSync(BUILD_CACHE, JSON.stringify(buildCache))
  # don't save browserify build to disk since it can get complicated
  # in situations where only dependencies versions change
)


process.on('SIGINT', process.exit)
process.on('SIGTERM', process.exit)


buildToDirectory = (grunt, options, src) ->
  cwd = options.src_base
  outDir = options.dest

  if not timestampCache
    if grunt.file.exists(TIMESTAMP_CACHE)
      timestampCache = grunt.file.readJSON(TIMESTAMP_CACHE)
    else
      timestampCache = {}

  if not grunt.file.exists(outDir)
    grunt.file.mkdir(outDir)

  src.forEach (file) ->
    if file.indexOf(cwd) != 0
      file = path.join(cwd, file)
    if not grunt.file.exists(file)
      grunt.log.warn('Source file "' + file + '" not found.')
      return
    outFile = path.join(outDir, path.relative(cwd, file))
    entry = timestampCache[file]
    mt = mtime(file)
    if /\.js/.test(outFile)
      if not grunt.file.exists(outFile) or mt != entry?.mtime
        # plain js, just copy to the output dir
        grunt.file.copy(file, outFile)
        grunt.log.ok("Copied #{file} to #{outFile}")
        timestampCache[file] = mtime: mt
      return
    outFile = outFile.replace(/\.coffee$/, '.js')
    fileOutDir = path.dirname(outFile)
    if not grunt.file.exists(outFile) or mt != entry?.mtime
      src = grunt.file.read(file)
      try
        compiled = compile(src, {
          sourceMap: options.sourceMap
          bare: false
        })
      catch e
        grunt.log.error("#{e.message}(file: #{file}, line: #{e.location.last_line + 1}, column: #{e.location.last_column})")
        throw e
      grunt.log.ok("Compiled #{file}")
      if options.sourceMap
        {js: compiled, v3SourceMap} = compiled
        v3SourceMap = JSON.parse(v3SourceMap)
        v3SourceMap.sourceRoot = path.relative(fileOutDir, cwd)
        v3SourceMap.file = path.basename(outFile)
        v3SourceMap.sources[0] = path.relative(cwd, file)
        v3SourceMap = JSON.stringify(v3SourceMap)
        compiled += "\n\n//@ sourceMappingURL=#{path.basename(outFile)}.map"
        grunt.file.write("#{outFile}.map", v3SourceMap)
        grunt.log.ok("File #{outFile}.map was created")
      timestampCache[file] = mtime: mt
      grunt.file.write(outFile, compiled)
      grunt.log.ok("File #{outFile} was created")

# Function adapted from the helper function with same name  thein traceur
# compiler source code.
#
# It generates an ugly identifier for a given pathname relative to
# the current file being processed, taking into consideration a base dir. eg:
# > generateNameForUrl('./b/c') # if the filename is 'a'
# '$$__a_b_c'
# > generateNameForUrl('../d') # if the filename is 'a/b/c'
# '$$__a_b_d'
#
# This assumes you won't name your variables using this prefix
generateNameForUrl = (grunt, url, from, cwd = '.', prefix = '$__') ->
  try
    cwd = path.resolve(cwd)
    from = path.resolve(path.dirname(from))
    url = path.resolve(path.join(from, url))
    if grunt.file.isDir(url)
      # its possible to require directories that have an index.coffee file
      url = path.join(url, 'index')
    ext = /\.(coffee|js)$/
    if ext.test(url)
      url = url.replace(ext, '')
  catch e
    grunt.log.warn(e)
    return null
  id = "$#{prefix + url.replace(cwd, '').replace(/[^\d\w$]/g, '_')}"
  return {id: id, url: path.relative(cwd, url)}


# Replace all the require calls by the generated identifier that represents
# the corresponding module. This will also fill the 'deps' array so the 
# main routine can concatenate the files in proper order. Require calls to
# non relative paths will be included in the requires array to be declared
# in the umd wrapper
replaceRequires = (grunt, js, fn, fp, cwd, deps, requires) ->
  displayNode = (node) -> js.slice(node.start.pos, node.end.endpos)
  transformer = new UglifyJS.TreeTransformer (node, descend) ->
    if (not (node instanceof UglifyJS.AST_Call) or
      node.expression.name != 'require' or
      (node.args.length != 1 or
       not /^\.(?:\.)?\//.test(node.args[0].value) and
       grunt.log.writeln(
         "Absolute 'require' call in file #{fn}: '#{displayNode(node)}'")) or
{'fraction_non_alphanumeric': 0.1313102119460501, 'fraction_numerical': 0.003468208092485549, 'mean_word_length': 3.359932801343973, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 25, 'https://': 2, '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': '7627807', 'n_tokens_mistral': 3398, 'n_tokens_neox': 3341, 'n_words': 917}