"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 lang_1 = require("@dojo/core/lang");
var Task_1 = require("@dojo/core/async/Task");
var Promise_1 = require("@dojo/shim/Promise");
var glob = require("glob");
function expandFiles(patterns) {
    if (!Array.isArray(patterns)) {
        patterns = [patterns];
    }
    return Promise_1.default.all(patterns.map(function (pattern) {
        return new Promise_1.default(function (resolve, reject) {
            glob(pattern, function (error, files) {
                if (error) {
                    reject(error);
                }
                else {
                    resolve(files);
                }
            });
        });
    })).then(function (fileSets) {
        return fileSets.reduce(function (allFiles, files) {
            return allFiles.concat(files);
        }, []);
    });
}
exports.expandFiles = expandFiles;
function getConfig() {
    var args = util_1.parseArgs(process.argv.slice(2));
    if (args.config) {
        return loadConfig(args.config).then(function (config) { return lang_1.deepMixin(config, args); });
    }
    else {
        return loadConfig('intern.json').then(function (config) { return lang_1.deepMixin(config, args); }, function (error) {
            if (error.code === 'ENOENT') {
                return args;
            }
            throw error;
        });
    }
}
exports.getConfig = getConfig;
function loadJson(resource) {
    return loadText(resource).then(function (data) { return util_1.parseJSON(data); });
}
exports.loadJson = loadJson;
function loadScript(script) {
    if (!Array.isArray(script)) {
        script = [script];
    }
    script.forEach(function (script) {
        script = path_1.resolve(script);
        delete require.cache[script];
        require(script);
    });
    return Task_1.default.resolve();
}
exports.loadScript = loadScript;
function normalizePath(path) {
    return path_1.normalize(path).replace(/\\/g, '/');
}
exports.normalizePath = normalizePath;
function projectRequire(mod) {
    require(path_1.resolve(mod));
}
exports.projectRequire = projectRequire;
function reportUncaughtErrors(executor) {
    process.on('unhandledRejection', function (reason, promise) {
        console.warn('Unhandled rejection:', promise);
        executor.emit('error', reason);
    });
    process.on('uncaughtException', function (reason) {
        console.warn('Unhandled error');
        executor.emit('error', reason);
    });
}
exports.reportUncaughtErrors = reportUncaughtErrors;
function loadConfig(configPath) {
    return loadJson(configPath).then(function (config) {
        if (config.extends) {
            var parts = configPath.split('/');
            var extensionPath = parts.slice(0, parts.length - 1).concat(config.extends).join('/');
            return loadConfig(extensionPath).then(function (extension) {
                return lang_1.deepMixin(extension, config);
            });
        }
        else {
            return config;
        }
    });
}
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);
            }
        });
    });
}
//# sourceMappingURL=util.js.map