<commit_before>path = require 'path'
transformTools = require 'browserify-transform-tools'

module.exports = transformTools.makeRequireTransform "aliasify", (args, opts, cb) ->
    aliases = opts.config?.aliases
    verbose = opts.config?.verbose
    if (args.length > 0) and aliases? and aliases[args[0]]?
        replacement = aliases[args[0]]

        if replacement.length > 0 and replacement[0] is '.'
            # Resolve the new file relative to the file doing the requiring.
            replacement = path.resolve opts.configData.configDir, replacement
            fileDir = path.dirname opts.file
            replacement = "./#{path.relative fileDir, replacement}"

        if verbose
            console.log "aliasify - #{opts.file}: replacing #{args[0]} with #{replacement}"

        cb null, "require('#{replacement}')"
    else
        cb()<commit_msg>Support for aliases that are folders of packages<commit_after>path = require 'path'
transformTools = require 'browserify-transform-tools'

getReplacement = (file, aliases)->
    if aliases[file]
        return aliases[file]
    else
        fileParts = /^([^\/]*)(\/.*)$/.exec(file)
        pkg = aliases[fileParts?[1]]
        if pkg?
            return pkg+fileParts[2]
    return null

module.exports = transformTools.makeRequireTransform "aliasify", (args, opts, cb) ->
    aliases = opts.config?.aliases
    verbose = opts.config?.verbose
    file = args[0]
    if file? and aliases?
        replacement = getReplacement(file, aliases)
        if replacement?
            if /^\./.test(replacement)
                # Resolve the new file relative to the file doing the requiring.
                replacement = path.resolve opts.configData.configDir, replacement
                fileDir = path.dirname opts.file
                replacement = "./#{path.relative fileDir, replacement}"

            if verbose
                console.log "aliasify - #{opts.file}: replacing #{args[0]} with #{replacement}"

            cb null, "require('#{replacement}')"
        else
            cb()
    else
        cb()
{'fraction_non_alphanumeric': 0.11759036144578314, 'fraction_numerical': 0.004819277108433735, 'mean_word_length': 2.414473684210526, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, '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': '1734995', 'n_tokens_mistral': 573, 'n_tokens_neox': 560, 'n_words': 158}