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5B,OAAO,gBAAO,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,UAAA,QAAQ;YAChC,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;gBAChB,MAAM,IAAI,KAAK,CAAC,kBAAkB,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC;aACvD;YACD,OAAO,QAAQ,CAAC,IAAI,EAAE,CAAC;QACzB,CAAC,CAAC,CAAC;IACL,CAAC;IAKD,SAAS,WAAW,CAAC,IAAY,EAAE,QAAgB;QACjD,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE;YACnB,OAAO,IAAI,CAAC;SACb;QAED,IAAM,SAAS,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAClC,IAAM,aAAa,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QAE1C,IAAI,aAAa,CAAC,aAAa,CAAC,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;YAClD,aAAa,CAAC,GAAG,EAAE,CAAC;SACrB;QAED,KAAmB,UAAS,EAAT,uBAAS,EAAT,uBAAS,EAAT,IAAS,EAAE;YAAzB,IAAM,IAAI,kBAAA;YACb,IAAI,IAAI,KAAK,IAAI,EAAE;gBACjB,aAAa,CAAC,GAAG,EAAE,CAAC;aACrB;iBAAM,IAAI,IAAI,KAAK,GAAG,EAAE;gBACvB,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aAC1B;SACF;QAED,OAAO,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IACjC,CAAC","sourcesContent":["import { request, CancellablePromise, global } from '@theintern/common';\n\nimport {\n getBasePath,\n loadConfig,\n parseArgs,\n splitConfigPath\n} from '../common/util';\n\n/**\n * Resolve the user-supplied config data, which may include query args and a\n * config file.\n */\nexport function getConfig(file?: string) {\n const args = parseArgs(parseQuery());\n const configBase = getDefaultBasePath();\n if (file) {\n args.config = file;\n }\n\n let load: CancellablePromise<{ [key: string]: any }>;\n\n if (args.config) {\n // If a config parameter was provided, load it, mix in any other query\n // params, then initialize the executor with that\n const { configFile, childConfig } = splitConfigPath(args.config);\n file = resolvePath(configFile || 'intern.json', configBase);\n load = loadConfig(file, loadText, args, childConfig);\n } else {\n // If no config parameter was provided, try 'intern.json'. If that file\n // doesn't exist, just return the args\n file = resolvePath('intern.json', configBase);\n load = loadConfig(file, loadText, args).catch(error => {\n if (error.message.indexOf('Request failed') === 0) {\n // The file wasn't found, clear the file name\n file = undefined;\n return args;\n }\n throw error;\n });\n }\n\n return load\n .then(config => {\n // If a basePath wasn't set in the config or via a query arg, and we\n // have a config file path, use that.\n if (file) {\n config.basePath = getBasePath(\n file,\n config.basePath,\n path => path[0] === '/',\n '/'\n );\n }\n return config;\n })\n .then(config => ({ config, file }));\n}\n\n/**\n * Return a base path based on the current location pathname\n */\nexport function getDefaultBasePath() {\n const match = /^(.*\\/)node_modules\\/intern\\/?/.exec(global.location.pathname);\n if (match) {\n // If the current location contains `node_modules/intern`,\n // assume the base path is the parent of\n // `node_modules/intern`\n return match[1];\n } else {\n return '/';\n }\n}\n\n// TODO: Remove in the next version\n/**\n * Normalize a path (e.g., resolve '..')\n */\nexport function normalizePath(path: string) {\n const parts = path.replace(/\\\\/g, '/').split('/');\n let result: string[] = [];\n for (let i = 0; i < parts.length; ++i) {\n let part = parts[i];\n\n if (!part || part === '.') {\n if (i === 0 || i === parts.length - 1) {\n result.push('');\n }\n\n continue;\n }\n\n if (part === '..') {\n if (result.length && result[result.length - 1] !== '..') {\n result.pop();\n } else {\n result.push(part);\n }\n } else {\n result.push(part);\n }\n }\n\n return result.join('/');\n}\n\n/**\n * Parse a query string and return a set of decoded name=value pairs\n */\nexport function parseQuery(query?: string) {\n query = query || global.location.search;\n\n const parsed: string[] = [];\n const params = new URLSearchParams(query);\n params.forEach((value, key) => {\n // If the param in the raw query string is bare, push a bare key,\n // otherwise, push key=value.\n if (new RegExp(`\\\\b${key}=`).test(query!)) {\n parsed.push(`${key}=${value}`);\n } else {\n parsed.push(key);\n }\n });\n\n return parsed;\n}\n\n/**\n * Parse a URL\n */\nexport type Url = {\n protocol: string;\n hostname: string;\n port: string;\n path: string;\n query: string;\n hash: string;\n};\nexport function parseUrl(url: string): Url | undefined {\n if (url) {\n const match = /^(([^:\\/?#]+):)?(\\/\\/(([^:\\/?#]*)(:(\\d+))?))?([^?#]*)(\\?([^#]*))?(#(.*))?/.exec(\n url\n );\n if (match) {\n return {\n protocol: match[2],\n hostname: match[5],\n port: match[7],\n path: match[8],\n query: match[10],\n hash: match[12]\n };\n }\n }\n}\n\n/**\n * Load a text resource\n */\nfunction loadText(path: string): CancellablePromise<any> {\n return request(path).then(response => {\n if (!response.ok) {\n throw new Error('Request failed: ' + response.status);\n }\n return response.text();\n });\n}\n\n/**\n * Resolve a path against a base path\n */\nfunction resolvePath(path: string, basePath: string) {\n if (path[0] === '/') {\n return path;\n }\n\n const pathParts = path.split('/');\n const basePathParts = basePath.split('/');\n\n if (basePathParts[basePathParts.length - 1] === '') {\n basePathParts.pop();\n }\n\n for (const part of pathParts) {\n if (part === '..') {\n basePathParts.pop();\n } else if (part !== '.') {\n basePathParts.push(part);\n }\n }\n\n return basePathParts.join('/');\n}\n"]} |