UNPKG

5.95 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", "tslib", "fs", "path", "shell-quote", "glob", "@theintern/common", "./process", "../common/util"], factory);
8 }
9})(function (require, exports) {
10 "use strict";
11 Object.defineProperty(exports, "__esModule", { value: true });
12 exports.transpileSource = exports.mkdirp = exports.isErrnoException = exports.readSourceMap = exports.normalizePath = exports.loadText = exports.getConfig = exports.expandFiles = void 0;
13 var tslib_1 = require("tslib");
14 var fs_1 = require("fs");
15 var path_1 = require("path");
16 var shell_quote_1 = require("shell-quote");
17 var glob_1 = require("glob");
18 var common_1 = require("@theintern/common");
19 var process_1 = tslib_1.__importDefault(require("./process"));
20 var util_1 = require("../common/util");
21 function expandFiles(patterns) {
22 if (!patterns) {
23 patterns = [];
24 }
25 else if (!Array.isArray(patterns)) {
26 patterns = [patterns];
27 }
28 var excludes = [];
29 var includes = [];
30 var paths = [];
31 for (var _i = 0, patterns_1 = patterns; _i < patterns_1.length; _i++) {
32 var pattern = patterns_1[_i];
33 if (pattern[0] === '!') {
34 excludes.push(pattern.slice(1));
35 }
36 else {
37 if (glob_1.hasMagic(pattern)) {
38 includes.push(pattern);
39 }
40 else {
41 paths.push(pattern);
42 }
43 }
44 }
45 var allPaths = includes
46 .map(function (pattern) { return glob_1.sync(pattern, { ignore: excludes }); })
47 .reduce(function (allFiles, files) { return allFiles.concat(files); }, paths);
48 var uniquePaths = {};
49 allPaths.forEach(function (path) { return (uniquePaths[path] = true); });
50 return Object.keys(uniquePaths);
51 }
52 exports.expandFiles = expandFiles;
53 function getConfig(fileOrArgv, argv) {
54 var args = {};
55 var file = typeof fileOrArgv === 'string' ? fileOrArgv : undefined;
56 argv = Array.isArray(fileOrArgv) ? fileOrArgv : argv;
57 var userArgs = (argv || process_1.default.argv).slice(2);
58 if (process_1.default.env['INTERN_ARGS']) {
59 Object.assign(args, util_1.parseArgs(shell_quote_1.parse(process_1.default.env['INTERN_ARGS'] || '')));
60 }
61 if (userArgs.length > 0) {
62 Object.assign(args, util_1.parseArgs(userArgs));
63 }
64 if (file) {
65 args.config = file;
66 }
67 var load;
68 if (args.config) {
69 var _a = util_1.splitConfigPath(args.config, path_1.sep), configFile = _a.configFile, childConfig = _a.childConfig;
70 file = path_1.resolve(configFile || 'intern.json');
71 load = util_1.loadConfig(file, loadText, args, childConfig);
72 }
73 else {
74 file = path_1.resolve('intern.json');
75 load = util_1.loadConfig(file, loadText, args, undefined).catch(function (error) {
76 if (error.code === 'ENOENT') {
77 file = undefined;
78 return args;
79 }
80 throw error;
81 });
82 }
83 return load
84 .then(function (config) {
85 if (file) {
86 config.basePath = util_1.getBasePath(file, config.basePath, path_1.isAbsolute, path_1.sep);
87 }
88 return config;
89 })
90 .then(function (config) { return ({ config: config, file: file }); });
91 }
92 exports.getConfig = getConfig;
93 function loadText(path) {
94 return new common_1.Task(function (resolve, reject) {
95 fs_1.readFile(path, { encoding: 'utf8' }, function (error, data) {
96 if (error) {
97 reject(error);
98 }
99 else {
100 resolve(data);
101 }
102 });
103 });
104 }
105 exports.loadText = loadText;
106 function normalizePath(path) {
107 return path_1.normalize(path).replace(/\\/g, '/');
108 }
109 exports.normalizePath = normalizePath;
110 function readSourceMap(sourceFile, code) {
111 if (!code) {
112 code = fs_1.readFileSync(sourceFile, { encoding: 'utf8' });
113 }
114 var match;
115 var lastNewline = code.lastIndexOf('\n', code.length - 2);
116 var lastLine = code.slice(lastNewline + 1);
117 if ((match = sourceMapRegEx.exec(lastLine))) {
118 if (match[1]) {
119 return JSON.parse(Buffer.from(match[2], 'base64').toString('utf8'));
120 }
121 else {
122 var mapFile = path_1.join(path_1.dirname(sourceFile), match[2]);
123 return JSON.parse(fs_1.readFileSync(mapFile, { encoding: 'utf8' }));
124 }
125 }
126 }
127 exports.readSourceMap = readSourceMap;
128 function isErrnoException(value) {
129 return value.errno !== null;
130 }
131 exports.isErrnoException = isErrnoException;
132 function mkdirp(dir) {
133 var parent = path_1.dirname(dir);
134 if (parent && !fs_1.existsSync(parent)) {
135 mkdirp(parent);
136 }
137 if (!fs_1.existsSync(dir)) {
138 fs_1.mkdirSync(dir);
139 }
140 }
141 exports.mkdirp = mkdirp;
142 function transpileSource(filename, code) {
143 require.extensions[path_1.extname(filename)]({
144 _compile: function (source) {
145 code = source;
146 }
147 }, filename);
148 return code;
149 }
150 exports.transpileSource = transpileSource;
151 var sourceMapRegEx = /^(?:\/{2}[#@]{1,2}|\/\*)\s+sourceMappingURL\s*=\s*(data:(?:[^;]+;)+base64,)?(\S+)/;
152});
153//# sourceMappingURL=util.js.map
\No newline at end of file