intern
Version:
Intern. A next-generation code testing stack for JavaScript.
153 lines • 5.95 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", "tslib", "fs", "path", "shell-quote", "glob", "@theintern/common", "./process", "../common/util"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.transpileSource = exports.mkdirp = exports.isErrnoException = exports.readSourceMap = exports.normalizePath = exports.loadText = exports.getConfig = exports.expandFiles = void 0;
var tslib_1 = require("tslib");
var fs_1 = require("fs");
var path_1 = require("path");
var shell_quote_1 = require("shell-quote");
var glob_1 = require("glob");
var common_1 = require("@theintern/common");
var process_1 = tslib_1.__importDefault(require("./process"));
var util_1 = require("../common/util");
function expandFiles(patterns) {
if (!patterns) {
patterns = [];
}
else if (!Array.isArray(patterns)) {
patterns = [patterns];
}
var excludes = [];
var includes = [];
var paths = [];
for (var _i = 0, patterns_1 = patterns; _i < patterns_1.length; _i++) {
var pattern = patterns_1[_i];
if (pattern[0] === '!') {
excludes.push(pattern.slice(1));
}
else {
if (glob_1.hasMagic(pattern)) {
includes.push(pattern);
}
else {
paths.push(pattern);
}
}
}
var allPaths = includes
.map(function (pattern) { return glob_1.sync(pattern, { ignore: excludes }); })
.reduce(function (allFiles, files) { return allFiles.concat(files); }, paths);
var uniquePaths = {};
allPaths.forEach(function (path) { return (uniquePaths[path] = true); });
return Object.keys(uniquePaths);
}
exports.expandFiles = expandFiles;
function getConfig(fileOrArgv, argv) {
var args = {};
var file = typeof fileOrArgv === 'string' ? fileOrArgv : undefined;
argv = Array.isArray(fileOrArgv) ? fileOrArgv : argv;
var userArgs = (argv || process_1.default.argv).slice(2);
if (process_1.default.env['INTERN_ARGS']) {
Object.assign(args, util_1.parseArgs(shell_quote_1.parse(process_1.default.env['INTERN_ARGS'] || '')));
}
if (userArgs.length > 0) {
Object.assign(args, util_1.parseArgs(userArgs));
}
if (file) {
args.config = file;
}
var load;
if (args.config) {
var _a = util_1.splitConfigPath(args.config, path_1.sep), configFile = _a.configFile, childConfig = _a.childConfig;
file = path_1.resolve(configFile || 'intern.json');
load = util_1.loadConfig(file, loadText, args, childConfig);
}
else {
file = path_1.resolve('intern.json');
load = util_1.loadConfig(file, loadText, args, undefined).catch(function (error) {
if (error.code === 'ENOENT') {
file = undefined;
return args;
}
throw error;
});
}
return load
.then(function (config) {
if (file) {
config.basePath = util_1.getBasePath(file, config.basePath, path_1.isAbsolute, path_1.sep);
}
return config;
})
.then(function (config) { return ({ config: config, file: file }); });
}
exports.getConfig = getConfig;
function loadText(path) {
return new common_1.Task(function (resolve, reject) {
fs_1.readFile(path, { encoding: 'utf8' }, function (error, data) {
if (error) {
reject(error);
}
else {
resolve(data);
}
});
});
}
exports.loadText = loadText;
function normalizePath(path) {
return path_1.normalize(path).replace(/\\/g, '/');
}
exports.normalizePath = normalizePath;
function readSourceMap(sourceFile, code) {
if (!code) {
code = fs_1.readFileSync(sourceFile, { encoding: 'utf8' });
}
var match;
var lastNewline = code.lastIndexOf('\n', code.length - 2);
var lastLine = code.slice(lastNewline + 1);
if ((match = sourceMapRegEx.exec(lastLine))) {
if (match[1]) {
return JSON.parse(Buffer.from(match[2], 'base64').toString('utf8'));
}
else {
var mapFile = path_1.join(path_1.dirname(sourceFile), match[2]);
return JSON.parse(fs_1.readFileSync(mapFile, { encoding: 'utf8' }));
}
}
}
exports.readSourceMap = readSourceMap;
function isErrnoException(value) {
return value.errno !== null;
}
exports.isErrnoException = isErrnoException;
function mkdirp(dir) {
var parent = path_1.dirname(dir);
if (parent && !fs_1.existsSync(parent)) {
mkdirp(parent);
}
if (!fs_1.existsSync(dir)) {
fs_1.mkdirSync(dir);
}
}
exports.mkdirp = mkdirp;
function transpileSource(filename, code) {
require.extensions[path_1.extname(filename)]({
_compile: function (source) {
code = source;
}
}, filename);
return code;
}
exports.transpileSource = transpileSource;
var sourceMapRegEx = /^(?:\/{2}[#@]{1,2}|\/\*)\s+sourceMappingURL\s*=\s*(data:(?:[^;]+;)+base64,)?(\S+)/;
});
//# sourceMappingURL=util.js.map