(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "path", "fs", "../common/util", "@dojo/core/async/Task", "glob", "@dojo/core/lang", "shell-quote", "@dojo/shim/global"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var path_1 = require("path");
    var fs_1 = require("fs");
    var util_1 = require("../common/util");
    var Task_1 = require("@dojo/core/async/Task");
    var glob_1 = require("glob");
    var lang_1 = require("@dojo/core/lang");
    var shell_quote_1 = require("shell-quote");
    var global_1 = require("@dojo/shim/global");
    var process = global_1.default.process;
    /**
     * Expand a list of glob patterns into a flat file list
     */
    function expandFiles(patterns) {
        if (!patterns) {
            patterns = [];
        }
        else if (!Array.isArray(patterns)) {
            patterns = [patterns];
        }
        return patterns.map(function (pattern) {
            if (glob_1.hasMagic(pattern)) {
                return glob_1.sync(pattern);
            }
            return [pattern];
        }).reduce(function (allFiles, files) {
            return allFiles.concat(files);
        }, []);
    }
    exports.expandFiles = expandFiles;
    /**
     * Get the user-supplied config data, which may include command line args and a config file.
     */
    function getConfig(configFile) {
        var args = {};
        if (process.env['INTERN_ARGS']) {
            lang_1.mixin(args, util_1.parseArgs(shell_quote_1.parse(process.env['INTERN_ARGS'])));
        }
        if (process.argv.length > 2) {
            lang_1.mixin(args, util_1.parseArgs(process.argv.slice(2)));
        }
        if (configFile) {
            args.config = configFile;
        }
        if (args.config) {
            // If a config parameter was provided, load it and mix in any other command line args.
            var _a = util_1.splitConfigPath(args.config), configFile_1 = _a.configFile, childConfig = _a.childConfig;
            return util_1.loadConfig(configFile_1 || 'intern.json', loadText, args, childConfig);
        }
        else {
            // If no config parameter was provided, try 'intern.json', or just resolve to the original args
            return util_1.loadConfig('intern.json', loadText, args).catch(function (error) {
                if (error.code === 'ENOENT') {
                    return args;
                }
                throw error;
            });
        }
    }
    exports.getConfig = getConfig;
    /**
     * Normalize a path (e.g., resolve '..')
     */
    function normalizePath(path) {
        return path_1.normalize(path).replace(/\\/g, '/');
    }
    exports.normalizePath = normalizePath;
    /**
     * Given a source filename, and optionally code, return the file's source map if one exists.
     */
    function readSourceMap(sourceFile, code) {
        if (!code) {
            code = fs_1.readFileSync(sourceFile, { encoding: 'utf8' });
        }
        var match;
        // sourceMappingUrl must be on last line of source file; search for last newline from code.length - 2 in case the
        // file ends with a newline
        var lastNewline = code.lastIndexOf('\n', code.length - 2);
        var lastLine = code.slice(lastNewline + 1);
        if ((match = sourceMapRegEx.exec(lastLine))) {
            if (match[1]) {
                return JSON.parse((new Buffer(match[2], 'base64').toString('utf8')));
            }
            else {
                // Treat map file path as relative to the source file
                var mapFile = path_1.join(path_1.dirname(sourceFile), match[2]);
                return JSON.parse(fs_1.readFileSync(mapFile, { encoding: 'utf8' }));
            }
        }
    }
    exports.readSourceMap = readSourceMap;
    /**
     * Loads a text resource.
     */
    function loadText(path) {
        return new Task_1.default(function (resolve, reject) {
            fs_1.readFile(path, { encoding: 'utf8' }, function (error, data) {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(data);
                }
            });
        });
    }
    // Regex for matching sourceMappingUrl comments
    var sourceMapRegEx = /^(?:\/{2}[#@]{1,2}|\/\*)\s+sourceMappingURL\s*=\s*(data:(?:[^;]+;)+base64,)?(\S+)/;
});
//# sourceMappingURL=util.js.map