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", {jsFilesOnly: true, fromSourceFileDir: true}, (args, opts, done) ->
    if !opts.config then return done new Error("Could not find configuration for aliasify")
    aliases = opts.config.aliases
    verbose = opts.config.verbose
    configDir = opts.configData?.configDir or opts.config.configDir or process.cwd()

    result = null

    file = args[0]
    if file? and aliases?
        replacement = getReplacement(file, aliases)
        if replacement?
            if replacement.relative?
                replacement = replacement.relative

            else if /^\./.test(replacement)
                # Resolve the new file relative to the configuration file.
                replacement = path.resolve configDir, replacement
                fileDir = path.dirname opts.file
                replacement = "./#{path.relative fileDir, replacement}"

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

            result = "require('#{replacement.replace(/\\/gi,"/")}')"

    done null, result

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", {jsFilesOnly: true, fromSourceFileDir: true}, (args, opts, done) ->
    if !opts.config then return done new Error("Could not find configuration for aliasify")
    aliases = opts.config.aliases
    verbose = opts.config.verbose
    configDir = opts.configData?.configDir or opts.config.configDir or process.cwd()

    result = null

    file = args[0]
    if file? and aliases?
        replacement = getReplacement(file, aliases)
        if replacement?
            if replacement.relative?
                replacement = replacement.relative

            else if /^\./.test(replacement)
                # Resolve the new file relative to the configuration file.
                replacement = path.resolve configDir, replacement
                fileDir = path.dirname opts.file
                replacement = "./#{path.relative fileDir, replacement}"

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

            if /^[a-zA-Z]:\\/.test(replacement)
                replacement = replacement.replace(/\\/gi, "\\\\")
            else
                replacement = replacement.replace(/\\/gi, "/")

            result = "require('#{replacement.replace(/\\/gi,"/")}')"

    done null, result

Fix replacement paths that are absolute paths on Windows
Fix replacement paths that are absolute paths on Windows

There is a bug where absolute paths have their backslashes replaced as forward slashes.  For relative paths, this is not a problem.  However, especially with joeybaker/remapify, it is possible to have absolute paths passed into aliasify, and getting the backslashes replaced there is problematic.

This fixes the issue by testing for an absolute path on Windows, and then replacing backslashes with double backslashes, which is necessary since the filename is in quotes.  All other paths have their backslashes replaced by forward slashes as currently happens today.
