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AC;QAC5B,IAAI,MAAM,IAAI,CAAC,eAAU,CAAC,MAAM,CAAC,EAAE;YACjC,MAAM,CAAC,MAAM,CAAC,CAAC;SAChB;QACD,IAAI,CAAC,eAAU,CAAC,GAAG,CAAC,EAAE;YACpB,cAAS,CAAC,GAAG,CAAC,CAAC;SAChB;IACH,CAAC;IARD,wBAQC;IAED,SAAgB,eAAe,CAAC,QAAgB,EAAE,IAAY;QAC5D,OAAO,CAAC,UAAU,CAAC,cAAO,CAAC,QAAQ,CAAC,CAAC,CACnC;YACE,QAAQ,EAAR,UAAS,MAAc;gBACrB,IAAI,GAAG,MAAM,CAAC;YAChB,CAAC;SACK,EACR,QAAQ,CACT,CAAC;QAEF,OAAO,IAAI,CAAC;IACd,CAAC;IAXD,0CAWC;IAGD,IAAM,cAAc,GAAG,mFAAmF,CAAC","sourcesContent":["import { readFile, readFileSync, mkdirSync, existsSync } from 'fs';\nimport {\n dirname,\n extname,\n isAbsolute,\n join,\n normalize,\n resolve,\n sep\n} from 'path';\nimport { parse } from 'shell-quote';\nimport { RawSourceMap } from 'source-map';\nimport { sync as glob, hasMagic } from 'glob';\n\nimport { Task, CancellablePromise } from '@theintern/common';\n\nimport process from './process';\nimport {\n getBasePath,\n loadConfig,\n parseArgs,\n splitConfigPath\n} from '../common/util';\n/**\n * Expand a list of glob patterns into a flat file list. Patterns may be simple\n * file paths or glob patterns. Patterns starting with '!' denote exclusions.\n * Note that exclusion rules will not apply to simple paths.\n */\nexport function expandFiles(patterns?: string[] | string) {\n if (!patterns) {\n patterns = [];\n } else if (!Array.isArray(patterns)) {\n patterns = [patterns];\n }\n\n const excludes: string[] = [];\n const includes: string[] = [];\n const paths: string[] = [];\n\n for (let pattern of patterns) {\n if (pattern[0] === '!') {\n excludes.push(pattern.slice(1));\n } else {\n if (hasMagic(pattern)) {\n includes.push(pattern);\n } else {\n paths.push(pattern);\n }\n }\n }\n\n const allPaths = includes\n .map(pattern => glob(pattern, { ignore: excludes }))\n .reduce((allFiles, files) => allFiles.concat(files), paths);\n const uniquePaths: { [name: string]: boolean } = {};\n allPaths.forEach(path => (uniquePaths[path] = true));\n\n return Object.keys(uniquePaths);\n}\n\n/**\n * Get the user-supplied config data, which may include command line args and a\n * config file.\n *\n * @param file A config file\n * @param argv An array of command line arguments. This should follow the same\n * format as process.argv (where user args start at index 2).\n */\nexport function getConfig(\n file?: string\n): CancellablePromise<{ config: any; file?: string }>;\nexport function getConfig(\n argv?: string[]\n): CancellablePromise<{ config: any; file?: string }>;\nexport function getConfig(\n file: string,\n argv?: string[]\n): CancellablePromise<{ config: any; file?: string }>;\nexport function getConfig(\n fileOrArgv?: string | string[],\n argv?: string[]\n): CancellablePromise<{ config: any; file?: string }> {\n let args: { [key: string]: any } = {};\n let file = typeof fileOrArgv === 'string' ? fileOrArgv : undefined;\n argv = Array.isArray(fileOrArgv) ? fileOrArgv : argv;\n const userArgs = (argv || process.argv).slice(2);\n\n if (process.env['INTERN_ARGS']) {\n Object.assign(\n args,\n parseArgs(parse(process.env['INTERN_ARGS'] || '') as string[])\n );\n }\n\n if (userArgs.length > 0) {\n Object.assign(args, parseArgs(userArgs));\n }\n\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 and mix in any other\n // command line args.\n const { configFile, childConfig } = splitConfigPath(args.config, sep);\n file = resolve(configFile || 'intern.json');\n load = loadConfig(file, loadText, args, childConfig);\n } else {\n // If no config parameter was provided, try 'intern.json', or just\n // resolve to the original args\n file = resolve('intern.json');\n load = loadConfig(file, loadText, args, undefined).catch(\n (error: NodeJS.ErrnoException) => {\n if (error.code === 'ENOENT') {\n file = undefined;\n return args;\n }\n throw error;\n }\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(file, config.basePath, isAbsolute, sep);\n }\n return config;\n })\n .then(config => ({ config, file }));\n}\n\n/**\n * Loads a text resource.\n */\nexport function loadText(path: string): CancellablePromise<string> {\n return new Task<string>((resolve, reject) => {\n readFile(path, { encoding: 'utf8' }, (error, data) => {\n if (error) {\n reject(error);\n } else {\n resolve(data);\n }\n });\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 return normalize(path).replace(/\\\\/g, '/');\n}\n\n/**\n * Given a source filename, and optionally code, return the file's source map if\n * one exists.\n */\nexport function readSourceMap(\n sourceFile: string,\n code?: string\n): RawSourceMap | undefined {\n if (!code) {\n code = readFileSync(sourceFile, { encoding: 'utf8' });\n }\n\n let match: RegExpMatchArray | null;\n\n // sourceMappingUrl must be on last line of source file; search for last\n // newline from code.length - 2 in case the file ends with a newline\n const lastNewline = code.lastIndexOf('\\n', code.length - 2);\n const lastLine = code.slice(lastNewline + 1);\n\n if ((match = sourceMapRegEx.exec(lastLine))) {\n if (match[1]) {\n return JSON.parse(Buffer.from(match[2], 'base64').toString('utf8'));\n } else {\n // Treat map file path as relative to the source file\n const mapFile = join(dirname(sourceFile), match[2]);\n return JSON.parse(readFileSync(mapFile, { encoding: 'utf8' }));\n }\n }\n}\n\n/**\n * Indicate whether a value is an ErrnoException\n */\nexport function isErrnoException(value: any): value is NodeJS.ErrnoException {\n return value.errno !== null;\n}\n\n/**\n * Recursively create directories\n */\nexport function mkdirp(dir: string) {\n const parent = dirname(dir);\n if (parent && !existsSync(parent)) {\n mkdirp(parent);\n }\n if (!existsSync(dir)) {\n mkdirSync(dir);\n }\n}\n\nexport function transpileSource(filename: string, code: string) {\n require.extensions[extname(filename)](\n {\n _compile(source: string) {\n code = source;\n }\n } as any,\n filename\n );\n\n return code;\n}\n\n// Regex for matching sourceMappingUrl comments\nconst sourceMapRegEx = /^(?:\\/{2}[#@]{1,2}|\\/\\*)\\s+sourceMappingURL\\s*=\\s*(data:(?:[^;]+;)+base64,)?(\\S+)/;\n"]} |