intern
Version: 
Intern. A next-generation code testing stack for JavaScript.
143 lines • 5.01 kB
JavaScript
(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", "@theintern/common", "../common/util"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    exports.parseUrl = exports.parseQuery = exports.normalizePath = exports.getDefaultBasePath = exports.getConfig = void 0;
    var common_1 = require("@theintern/common");
    var util_1 = require("../common/util");
    function getConfig(file) {
        var args = util_1.parseArgs(parseQuery());
        var configBase = getDefaultBasePath();
        if (file) {
            args.config = file;
        }
        var load;
        if (args.config) {
            var _a = util_1.splitConfigPath(args.config), configFile = _a.configFile, childConfig = _a.childConfig;
            file = resolvePath(configFile || 'intern.json', configBase);
            load = util_1.loadConfig(file, loadText, args, childConfig);
        }
        else {
            file = resolvePath('intern.json', configBase);
            load = util_1.loadConfig(file, loadText, args).catch(function (error) {
                if (error.message.indexOf('Request failed') === 0) {
                    file = undefined;
                    return args;
                }
                throw error;
            });
        }
        return load
            .then(function (config) {
            if (file) {
                config.basePath = util_1.getBasePath(file, config.basePath, function (path) { return path[0] === '/'; }, '/');
            }
            return config;
        })
            .then(function (config) { return ({ config: config, file: file }); });
    }
    exports.getConfig = getConfig;
    function getDefaultBasePath() {
        var match = /^(.*\/)node_modules\/intern\/?/.exec(common_1.global.location.pathname);
        if (match) {
            return match[1];
        }
        else {
            return '/';
        }
    }
    exports.getDefaultBasePath = getDefaultBasePath;
    function normalizePath(path) {
        var parts = path.replace(/\\/g, '/').split('/');
        var result = [];
        for (var i = 0; i < parts.length; ++i) {
            var part = parts[i];
            if (!part || part === '.') {
                if (i === 0 || i === parts.length - 1) {
                    result.push('');
                }
                continue;
            }
            if (part === '..') {
                if (result.length && result[result.length - 1] !== '..') {
                    result.pop();
                }
                else {
                    result.push(part);
                }
            }
            else {
                result.push(part);
            }
        }
        return result.join('/');
    }
    exports.normalizePath = normalizePath;
    function parseQuery(query) {
        query = query || common_1.global.location.search;
        var parsed = [];
        var params = new URLSearchParams(query);
        params.forEach(function (value, key) {
            if (new RegExp("\\b" + key + "=").test(query)) {
                parsed.push(key + "=" + value);
            }
            else {
                parsed.push(key);
            }
        });
        return parsed;
    }
    exports.parseQuery = parseQuery;
    function parseUrl(url) {
        if (url) {
            var match = /^(([^:\/?#]+):)?(\/\/(([^:\/?#]*)(:(\d+))?))?([^?#]*)(\?([^#]*))?(#(.*))?/.exec(url);
            if (match) {
                return {
                    protocol: match[2],
                    hostname: match[5],
                    port: match[7],
                    path: match[8],
                    query: match[10],
                    hash: match[12]
                };
            }
        }
    }
    exports.parseUrl = parseUrl;
    function loadText(path) {
        return common_1.request(path).then(function (response) {
            if (!response.ok) {
                throw new Error('Request failed: ' + response.status);
            }
            return response.text();
        });
    }
    function resolvePath(path, basePath) {
        if (path[0] === '/') {
            return path;
        }
        var pathParts = path.split('/');
        var basePathParts = basePath.split('/');
        if (basePathParts[basePathParts.length - 1] === '') {
            basePathParts.pop();
        }
        for (var _i = 0, pathParts_1 = pathParts; _i < pathParts_1.length; _i++) {
            var part = pathParts_1[_i];
            if (part === '..') {
                basePathParts.pop();
            }
            else if (part !== '.') {
                basePathParts.push(part);
            }
        }
        return basePathParts.join('/');
    }
});
//# sourceMappingURL=util.js.map