@backtrace/javascript-cli
Version:
Backtrace CLI for working with Javascript files.
109 lines • 5.56 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.file2Or1FromTuple = exports.findTuples = exports.buildIncludeExclude = exports.includesFindResult = exports.find = void 0;
const sourcemap_tools_1 = require("@backtrace/sourcemap-tools");
const fs_1 = __importDefault(require("fs"));
const glob_1 = require("glob");
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
/**
* Returns files found in directories matching `regex`. If path is a file, it is returned if it matches `regex`.
* @param regex Regular expression pattern to match.
* @param paths Paths to search in.
* @returns Result with file paths.
*/
async function find(paths) {
const finder = new sourcemap_tools_1.FileFinder();
const results = new Map();
for (const globPath of paths) {
const globResults = await (0, glob_1.glob)(globPath);
for (const findPath of globResults) {
const stat = await fs_1.default.promises.stat(findPath);
if (!stat.isDirectory()) {
const fullPath = path_1.default.resolve(findPath);
if (!results.has(fullPath)) {
results.set(fullPath, {
path: fullPath,
findPath,
direct: true,
});
}
continue;
}
const findResults = await finder.find(findPath, { recursive: true });
for (const result of findResults) {
const fullPath = path_1.default.resolve(result);
if (!results.has(fullPath)) {
results.set(fullPath, {
path: fullPath,
findPath,
direct: false,
});
}
}
}
}
return [...results.values()];
}
exports.find = find;
function includesFindResult(includes) {
const pathSet = new Set(includes.map((i) => i.path));
return function _includesFindResult(result) {
return pathSet.has(result.path);
};
}
exports.includesFindResult = includesFindResult;
async function buildIncludeExclude(includePaths, excludePaths, logger) {
const resolvedIncludePaths = includePaths ? await find(includePaths) : undefined;
const isIncluded = resolvedIncludePaths
? (result) => (0, sourcemap_tools_1.pipe)(result, includesFindResult(resolvedIncludePaths), logger((t) => (t ? `result included: ${result.path}` : `result not included: ${result.path}`)))
: undefined;
const resolvedExcludePaths = excludePaths ? await find(excludePaths) : undefined;
const isExcluded = resolvedExcludePaths
? (result) => (0, sourcemap_tools_1.pipe)(result, includesFindResult(resolvedExcludePaths), logger((t) => (t ? `result excluded: ${result.path}` : `result not excluded: ${result.path}`)))
: undefined;
return { isIncluded, isExcluded };
}
exports.buildIncludeExclude = buildIncludeExclude;
async function findTuples(paths) {
function findLongest(char, str) {
return (0, sourcemap_tools_1.pipe)([...str.matchAll(new RegExp(`${char}+`, 'g'))], (0, sourcemap_tools_1.flatMap)((a) => a), (a) => a.sort((a, b) => b.length - a.length), (a) => a[0]);
}
function splitByLongest(char) {
return async function _splitByLongest(str) {
const longest = await findLongest(char, str);
if (!longest) {
return [str];
}
return str.split(longest);
};
}
function verifyTupleLength(path) {
return function _verifyTupleLength(paths) {
return paths.length > 2
? (0, sourcemap_tools_1.Err)(`${path}: only two paths are allowed in a tuple`)
: (0, sourcemap_tools_1.Ok)(paths);
};
}
function verifyTuple(path) {
return async function _verifyTuple([path1, path2]) {
return path2
? await (0, sourcemap_tools_1.pipe)([path1, path2], (0, sourcemap_tools_1.mapAsync)(sourcemap_tools_1.statFile), sourcemap_tools_1.R.flatMap, sourcemap_tools_1.R.map((0, sourcemap_tools_1.flow)((0, sourcemap_tools_1.map)((r) => r.isFile() ? (0, sourcemap_tools_1.Ok)(path2) : (0, sourcemap_tools_1.Err)(`${path}: both paths of tuple must point to files`)), sourcemap_tools_1.R.flatMap)), sourcemap_tools_1.R.map(() => [path1, path2]))
: (0, sourcemap_tools_1.Ok)([path1, path2]);
};
}
function isWindows() {
return os_1.default.platform() === 'win32';
}
function processPath(path) {
return (0, sourcemap_tools_1.pipe)(path, splitByLongest(isWindows() ? '::' : ':'), verifyTupleLength(path), sourcemap_tools_1.R.map(verifyTuple(path)), sourcemap_tools_1.R.map(async ([path1, path2]) => ({ result: await find([path1]), path2 })), sourcemap_tools_1.R.map(({ result, path2 }) => result.map((file1) => ({ file1, file2: path2 }))));
}
return (0, sourcemap_tools_1.pipe)(paths, (0, sourcemap_tools_1.mapAsync)(processPath), sourcemap_tools_1.R.flatMap, sourcemap_tools_1.R.map((0, sourcemap_tools_1.flatMap)((x) => x)));
}
exports.findTuples = findTuples;
const file2Or1FromTuple = ({ file1, file2 }) => file2 ? { direct: true, findPath: file2, path: file2 } : file1;
exports.file2Or1FromTuple = file2Or1FromTuple;
//# sourceMappingURL=find.js.map
;