UNPKG

5.01 kBJavaScriptView Raw
1(function (factory) {
2 if (typeof module === "object" && typeof module.exports === "object") {
3 var v = factory(require, exports);
4 if (v !== undefined) module.exports = v;
5 }
6 else if (typeof define === "function" && define.amd) {
7 define(["require", "exports", "@theintern/common", "../common/util"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 exports.parseUrl = exports.parseQuery = exports.normalizePath = exports.getDefaultBasePath = exports.getConfig = void 0;
13 var common_1 = require("@theintern/common");
14 var util_1 = require("../common/util");
15 function getConfig(file) {
16 var args = util_1.parseArgs(parseQuery());
17 var configBase = getDefaultBasePath();
18 if (file) {
19 args.config = file;
20 }
21 var load;
22 if (args.config) {
23 var _a = util_1.splitConfigPath(args.config), configFile = _a.configFile, childConfig = _a.childConfig;
24 file = resolvePath(configFile || 'intern.json', configBase);
25 load = util_1.loadConfig(file, loadText, args, childConfig);
26 }
27 else {
28 file = resolvePath('intern.json', configBase);
29 load = util_1.loadConfig(file, loadText, args).catch(function (error) {
30 if (error.message.indexOf('Request failed') === 0) {
31 file = undefined;
32 return args;
33 }
34 throw error;
35 });
36 }
37 return load
38 .then(function (config) {
39 if (file) {
40 config.basePath = util_1.getBasePath(file, config.basePath, function (path) { return path[0] === '/'; }, '/');
41 }
42 return config;
43 })
44 .then(function (config) { return ({ config: config, file: file }); });
45 }
46 exports.getConfig = getConfig;
47 function getDefaultBasePath() {
48 var match = /^(.*\/)node_modules\/intern\/?/.exec(common_1.global.location.pathname);
49 if (match) {
50 return match[1];
51 }
52 else {
53 return '/';
54 }
55 }
56 exports.getDefaultBasePath = getDefaultBasePath;
57 function normalizePath(path) {
58 var parts = path.replace(/\\/g, '/').split('/');
59 var result = [];
60 for (var i = 0; i < parts.length; ++i) {
61 var part = parts[i];
62 if (!part || part === '.') {
63 if (i === 0 || i === parts.length - 1) {
64 result.push('');
65 }
66 continue;
67 }
68 if (part === '..') {
69 if (result.length && result[result.length - 1] !== '..') {
70 result.pop();
71 }
72 else {
73 result.push(part);
74 }
75 }
76 else {
77 result.push(part);
78 }
79 }
80 return result.join('/');
81 }
82 exports.normalizePath = normalizePath;
83 function parseQuery(query) {
84 query = query || common_1.global.location.search;
85 var parsed = [];
86 var params = new URLSearchParams(query);
87 params.forEach(function (value, key) {
88 if (new RegExp("\\b" + key + "=").test(query)) {
89 parsed.push(key + "=" + value);
90 }
91 else {
92 parsed.push(key);
93 }
94 });
95 return parsed;
96 }
97 exports.parseQuery = parseQuery;
98 function parseUrl(url) {
99 if (url) {
100 var match = /^(([^:\/?#]+):)?(\/\/(([^:\/?#]*)(:(\d+))?))?([^?#]*)(\?([^#]*))?(#(.*))?/.exec(url);
101 if (match) {
102 return {
103 protocol: match[2],
104 hostname: match[5],
105 port: match[7],
106 path: match[8],
107 query: match[10],
108 hash: match[12]
109 };
110 }
111 }
112 }
113 exports.parseUrl = parseUrl;
114 function loadText(path) {
115 return common_1.request(path).then(function (response) {
116 if (!response.ok) {
117 throw new Error('Request failed: ' + response.status);
118 }
119 return response.text();
120 });
121 }
122 function resolvePath(path, basePath) {
123 if (path[0] === '/') {
124 return path;
125 }
126 var pathParts = path.split('/');
127 var basePathParts = basePath.split('/');
128 if (basePathParts[basePathParts.length - 1] === '') {
129 basePathParts.pop();
130 }
131 for (var _i = 0, pathParts_1 = pathParts; _i < pathParts_1.length; _i++) {
132 var part = pathParts_1[_i];
133 if (part === '..') {
134 basePathParts.pop();
135 }
136 else if (part !== '.') {
137 basePathParts.push(part);
138 }
139 }
140 return basePathParts.join('/');
141 }
142});
143//# sourceMappingURL=util.js.map
\No newline at end of file