<commit_before>require('source-map-support').install()


path = require 'path'
fs = require 'fs'
fsu = require 'fs-util'

Coffeescript = require './../compilers/coffeescript'
Jade = require './../compilers/jade'
Stylus = require './../compilers/stylus'

{log,debug,warn,error} = require './../utils/log-util'


module.exports = class File

  @EXTENSIONS = [Coffeescript.EXT, Jade.EXT, Stylus.EXT]
  @COMPILERS = [Coffeescript, Jade, Stylus]

  id: null
  name: null

  absolute_path: null
  relative_path: null

  destination_path: null
  destination_folder: null

  dependencies: null
  baseclasses: null

  constructor:( @polvo, @cli, @config, @tentacle, @tree, @src_dir, @absolute_path )->
    @compiler = @_resolve_compiler()
    do @refresh
    do @compile_to_str

  refresh:->
    @raw = fs.readFileSync @absolute_path, "utf-8"

    # source paths
    @relative_path = @absolute_path.replace @src_dir, ''
    @relative_dir = path.dirname @relative_path
    @name = path.basename @relative_path

    # normalizing source paths
    @relative_path = @relative_path.replace /^\//m, ''
    @relative_dir = @relative_dir.replace /^\//m, ''
    @relative_dir = '' if @relative_dir is '.'

    @id = @compiler.strip_ext @relative_path

    # destination paths
    @out = {}
    @out.absolute_src_path = path.join @config.destination, @relative_path

    if @compiler.translate_map_ext?
      # console.log '--------' + @out.absolute_path
      # console.log 'js path - ' + 
      @out.absolute_map_path = @compiler.translate_map_ext @out.absolute_src_path
      # console.log 'map path - ' + @out.absolute_map_path

    # changing extension for absolute path
    @out.absolute_path = @compiler.translate_ext @out.absolute_src_path

    # computing other paths
    @out.absolute_dir = path.dirname @out.absolute_path
    @out.relative_path = @out.absolute_path.replace @config.destination, ''
    @out.relative_path = @out.relative_path.replace /^\//m, ''


  compile_to_str:( after_compile, exclude_anonymous_requires )->
    @compiler.compile @, ( js, map, src )=>
      js = @inject_dependencies js, exclude_anonymous_requires
      if exclude_anonymous_requires
        js = @exclude_anonymous_requires js
      after_compile?(js, map, src)

  compile_to_disk:->
    # datetime for CLI notifications
    now = ("#{new Date}".match /[0-9]{2}\:[0-9]{2}\:[0-9]{2}/)[0]

    # get compiled file
    @compile_to_str (js, src_map, src)=>
      # create container folder if it doesnt exist yet
      unless fs.existsSync @out.absolute_dir
        fsu.mkdir_p @out.absolute_dir

      # write compile file inside of it
      fs.writeFileSync @out.absolute_path, js

      # if source maps have been generated
      if src_map? and @out.absolute_map_path?
        fs.writeFileSync @out.absolute_map_path, src_map

      # if source is given, put it side by side with the js in order to
      # provide easy source-mapping
      if src?
        fs.writeFileSync @out.absolute_src_path, src

      # notify user through cli
      msg = '✓ Compiled'.bold
      log "[#{now}] #{msg} #{@out.relative_path}".green

  extract_dependencies:( js_code )->
    @dependencies = []
    @baseclasses = []

    require_reg = /([^\s]+)?(?:\s*=\s*)?(?:require\s*\()(?:'|")(.+)(?:'|")/g

    while matched = require_reg.exec js_code

        # computes dep name and path
        dep = 
          name: matched[1]
          id: matched[2]
          vendor: matched[1] is undefined or (matched[2] of @config.vendors.javascript)
          incompatible: matched[2] in @config.vendors.javascript.incompatible

        # and add it to the dependencies array
        if dep.is_vendor is true or dep.name is undefined
          @dependencies.push dep
        else
          @dependencies.splice @dependencies_diff_head++, 0, dep

  inject_dependencies:( js_code, exclude_anonymous_requires = false )->

    @extract_dependencies js_code

    if @dependencies.length
      paths = []
      for dep in @dependencies
        if exclude_anonymous_requires and dep.incompatible
          continue
        else
          paths.push dep.id
      paths = ", '#{paths.join "', '"}'"
    else
      paths = ''
    
    search = "define(['require', 'exports', 'module']"<commit_msg>Fixing indentation.<commit_after>require('source-map-support').install()


path = require 'path'
fs = require 'fs'
fsu = require 'fs-util'

Coffeescript = require './../compilers/coffeescript'
Jade = require './../compilers/jade'
Stylus = require './../compilers/stylus'

{log,debug,warn,error} = require './../utils/log-util'


module.exports = class File

  @EXTENSIONS = [Coffeescript.EXT, Jade.EXT, Stylus.EXT]
  @COMPILERS = [Coffeescript, Jade, Stylus]

  id: null
  name: null

  absolute_path: null
  relative_path: null

  destination_path: null
  destination_folder: null

  dependencies: null
  baseclasses: null

  constructor:( @polvo, @cli, @config, @tentacle, @tree, @src_dir, @absolute_path )->
    @compiler = @_resolve_compiler()
    do @refresh
    do @compile_to_str

  refresh:->
    @raw = fs.readFileSync @absolute_path, "utf-8"

    # source paths
    @relative_path = @absolute_path.replace @src_dir, ''
    @relative_dir = path.dirname @relative_path
    @name = path.basename @relative_path

    # normalizing source paths
    @relative_path = @relative_path.replace /^\//m, ''
    @relative_dir = @relative_dir.replace /^\//m, ''
    @relative_dir = '' if @relative_dir is '.'

    @id = @compiler.strip_ext @relative_path

    # destination paths
    @out = {}
    @out.absolute_src_path = path.join @config.destination, @relative_path

    if @compiler.translate_map_ext?
      # console.log '--------' + @out.absolute_path
      # console.log 'js path - ' + 
      @out.absolute_map_path = @compiler.translate_map_ext @out.absolute_src_path
      # console.log 'map path - ' + @out.absolute_map_path

    # changing extension for absolute path
    @out.absolute_path = @compiler.translate_ext @out.absolute_src_path

    # computing other paths
    @out.absolute_dir = path.dirname @out.absolute_path
    @out.relative_path = @out.absolute_path.replace @config.destination, ''
    @out.relative_path = @out.relative_path.replace /^\//m, ''


  compile_to_str:( after_compile, exclude_anonymous_requires )->
    @compiler.compile @, ( js, map, src )=>
      js = @inject_dependencies js, exclude_anonymous_requires
      if exclude_anonymous_requires
        js = @exclude_anonymous_requires js
      after_compile?(js, map, src)

  compile_to_disk:->
    # datetime for CLI notifications
    now = ("#{new Date}".match /[0-9]{2}\:[0-9]{2}\:[0-9]{2}/)[0]

    # get compiled file
    @compile_to_str (js, src_map, src)=>
      # create container folder if it doesnt exist yet
      unless fs.existsSync @out.absolute_dir
        fsu.mkdir_p @out.absolute_dir

      # write compile file inside of it
      fs.writeFileSync @out.absolute_path, js

      # if source maps have been generated
      if src_map? and @out.absolute_map_path?
        fs.writeFileSync @out.absolute_map_path, src_map

      # if source is given, put it side by side with the js in order to
      # provide easy source-mapping
      if src?
        fs.writeFileSync @out.absolute_src_path, src

      # notify user through cli
      msg = '✓ Compiled'.bold
      log "[#{now}] #{msg} #{@out.relative_path}".green

  extract_dependencies:( js_code )->
    @dependencies = []
    @baseclasses = []

    require_reg = /([^\s]+)?(?:\s*=\s*)?(?:require\s*\()(?:'|")(.+)(?:'|")/g

    while (matched = require_reg.exec js_code)

      # computes dep name and path
      dep = 
        name: matched[1]
        id: matched[2]
        vendor: matched[1] is undefined or (matched[2] of @config.vendors.javascript)
        incompatible: matched[2] in @config.vendors.javascript.incompatible

      # and add it to the dependencies array
      if dep.is_vendor is true or dep.name is undefined
        @dependencies.push dep
      else
        @dependencies.splice @dependencies_diff_head++, 0, dep

  inject_dependencies:( js_code, exclude_anonymous_requires = false )->

    @extract_dependencies js_code

    if @dependencies.length
      paths = []
      for dep in @dependencies
        if exclude_anonymous_requires and dep.incompatible
          continue
        else
          paths.push dep.id
      paths = ", '#{paths.join "', '"}'"
    else
      paths = ''
    
    search = "define(['require', 'exports', 'module']"
{'fraction_non_alphanumeric': 0.1284869976359338, 'fraction_numerical': 0.004018912529550827, 'mean_word_length': 2.962997658079625, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, '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': '1432312', 'n_tokens_mistral': 2780, 'n_tokens_neox': 2655, 'n_words': 646}