2019-03-02 14:56:19 -05:00
|
|
|
// This file is ported from globrex@0.1.2
|
|
|
|
// MIT License
|
|
|
|
// Copyright (c) 2018 Terkel Gjervig Nielsen
|
2020-05-31 13:48:32 -04:00
|
|
|
/** This module is browser compatible. */
|
|
|
|
|
|
|
|
import { isWindows as isWin } from "./_constants.ts";
|
2019-03-02 14:56:19 -05:00
|
|
|
|
2020-02-06 17:25:39 -05:00
|
|
|
const SEP = isWin ? `(?:\\\\|\\/)` : `\\/`;
|
2019-03-02 14:56:19 -05:00
|
|
|
const SEP_ESC = isWin ? `\\\\` : `/`;
|
2019-09-28 09:33:17 -04:00
|
|
|
const SEP_RAW = isWin ? `\\` : `/`;
|
2020-02-06 17:25:39 -05:00
|
|
|
const GLOBSTAR = `(?:(?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`;
|
|
|
|
const WILDCARD = `(?:[^${SEP_ESC}/]*)`;
|
2019-09-28 09:33:17 -04:00
|
|
|
const GLOBSTAR_SEGMENT = `((?:[^${SEP_ESC}/]*(?:${SEP_ESC}|\/|$))*)`;
|
2020-02-06 17:25:39 -05:00
|
|
|
const WILDCARD_SEGMENT = `(?:[^${SEP_ESC}/]*)`;
|
2019-03-02 14:56:19 -05:00
|
|
|
|
2019-10-02 13:59:27 -04:00
|
|
|
export interface GlobrexOptions {
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Allow ExtGlob features.
|
|
|
|
* @default false */
|
2019-10-02 13:59:27 -04:00
|
|
|
extended?: boolean;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Support globstar.
|
2020-05-14 00:38:42 -04:00
|
|
|
* @remarks When globstar is `true`, '/foo/**' is equivalent
|
2020-04-28 17:26:31 -04:00
|
|
|
* to '/foo/*' when globstar is `false`.
|
|
|
|
* Having globstar set to `true` is the same usage as
|
|
|
|
* using wildcards in bash.
|
|
|
|
* @default false */
|
2019-10-02 13:59:27 -04:00
|
|
|
globstar?: boolean;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Be laissez-faire about mutiple slashes.
|
|
|
|
* @default true */
|
2019-10-02 13:59:27 -04:00
|
|
|
strict?: boolean;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Parse as filepath for extra path related features.
|
|
|
|
* @default false */
|
2019-10-02 13:59:27 -04:00
|
|
|
filepath?: boolean;
|
2020-04-28 17:26:31 -04:00
|
|
|
/** Flag to use in the generated RegExp. */
|
2019-10-02 13:59:27 -04:00
|
|
|
flags?: string;
|
|
|
|
}
|
|
|
|
|
2019-03-12 01:51:51 -04:00
|
|
|
export interface GlobrexResult {
|
|
|
|
regex: RegExp;
|
|
|
|
path?: {
|
2019-09-28 09:33:17 -04:00
|
|
|
regex: RegExp;
|
2019-03-12 01:51:51 -04:00
|
|
|
segments: RegExp[];
|
|
|
|
globstar?: RegExp;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-03-02 14:56:19 -05:00
|
|
|
/**
|
|
|
|
* Convert any glob pattern to a JavaScript Regexp object
|
2019-03-14 10:24:54 -04:00
|
|
|
* @param glob Glob pattern to convert
|
|
|
|
* @param opts Configuration object
|
|
|
|
* @returns Converted object with string, segments and RegExp object
|
2019-03-02 14:56:19 -05:00
|
|
|
*/
|
|
|
|
export function globrex(
|
|
|
|
glob: string,
|
|
|
|
{
|
|
|
|
extended = false,
|
|
|
|
globstar = false,
|
|
|
|
strict = false,
|
|
|
|
filepath = false,
|
2020-03-28 13:03:49 -04:00
|
|
|
flags = "",
|
2020-07-14 15:24:17 -04:00
|
|
|
}: GlobrexOptions = {},
|
2019-03-12 01:51:51 -04:00
|
|
|
): GlobrexResult {
|
2020-02-06 17:25:39 -05:00
|
|
|
const sepPattern = new RegExp(`^${SEP}${strict ? "" : "+"}$`);
|
2019-03-02 14:56:19 -05:00
|
|
|
let regex = "";
|
|
|
|
let segment = "";
|
2019-09-28 09:33:17 -04:00
|
|
|
let pathRegexStr = "";
|
|
|
|
const pathSegments = [];
|
2019-03-02 14:56:19 -05:00
|
|
|
|
|
|
|
// If we are doing extended matching, this boolean is true when we are inside
|
|
|
|
// a group (eg {*.html,*.js}), and false otherwise.
|
|
|
|
let inGroup = false;
|
|
|
|
let inRange = false;
|
|
|
|
|
|
|
|
// extglob stack. Keep track of scope
|
|
|
|
const ext = [];
|
|
|
|
|
|
|
|
interface AddOptions {
|
|
|
|
split?: boolean;
|
|
|
|
last?: boolean;
|
|
|
|
only?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to build string and segments
|
|
|
|
function add(
|
2019-05-30 08:59:30 -04:00
|
|
|
str: string,
|
2020-07-14 15:24:17 -04:00
|
|
|
options: AddOptions = { split: false, last: false, only: "" },
|
2019-03-12 01:51:51 -04:00
|
|
|
): void {
|
2019-03-02 14:56:19 -05:00
|
|
|
const { split, last, only } = options;
|
|
|
|
if (only !== "path") regex += str;
|
|
|
|
if (filepath && only !== "regex") {
|
2020-02-06 17:25:39 -05:00
|
|
|
pathRegexStr += str.match(sepPattern) ? SEP : str;
|
2019-03-02 14:56:19 -05:00
|
|
|
if (split) {
|
|
|
|
if (last) segment += str;
|
|
|
|
if (segment !== "") {
|
2019-06-19 00:22:01 -04:00
|
|
|
// change it 'includes'
|
|
|
|
if (!flags.includes("g")) segment = `^${segment}$`;
|
2019-09-28 09:33:17 -04:00
|
|
|
pathSegments.push(new RegExp(segment, flags));
|
2019-03-02 14:56:19 -05:00
|
|
|
}
|
|
|
|
segment = "";
|
|
|
|
} else {
|
|
|
|
segment += str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let c, n;
|
|
|
|
for (let i = 0; i < glob.length; i++) {
|
|
|
|
c = glob[i];
|
|
|
|
n = glob[i + 1];
|
|
|
|
|
|
|
|
if (["\\", "$", "^", ".", "="].includes(c)) {
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-02-06 17:25:39 -05:00
|
|
|
if (c.match(sepPattern)) {
|
|
|
|
add(SEP, { split: true });
|
|
|
|
if (n != null && n.match(sepPattern) && !strict) regex += "?";
|
2019-03-02 14:56:19 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "(") {
|
|
|
|
if (ext.length) {
|
2020-02-06 17:25:39 -05:00
|
|
|
add(`${c}?:`);
|
2019-03-02 14:56:19 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === ")") {
|
|
|
|
if (ext.length) {
|
|
|
|
add(c);
|
2019-10-05 12:02:34 -04:00
|
|
|
const type: string | undefined = ext.pop();
|
2019-03-02 14:56:19 -05:00
|
|
|
if (type === "@") {
|
|
|
|
add("{1}");
|
|
|
|
} else if (type === "!") {
|
2020-02-06 17:25:39 -05:00
|
|
|
add(WILDCARD);
|
2019-03-02 14:56:19 -05:00
|
|
|
} else {
|
2019-05-30 08:59:30 -04:00
|
|
|
add(type as string);
|
2019-03-02 14:56:19 -05:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "|") {
|
|
|
|
if (ext.length) {
|
|
|
|
add(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "+") {
|
|
|
|
if (n === "(" && extended) {
|
|
|
|
ext.push(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "@" && extended) {
|
|
|
|
if (n === "(") {
|
|
|
|
ext.push(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "!") {
|
|
|
|
if (extended) {
|
|
|
|
if (inRange) {
|
|
|
|
add("^");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (n === "(") {
|
|
|
|
ext.push(c);
|
|
|
|
add("(?!");
|
|
|
|
i++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "?") {
|
|
|
|
if (extended) {
|
|
|
|
if (n === "(") {
|
|
|
|
ext.push(c);
|
|
|
|
} else {
|
|
|
|
add(".");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "[") {
|
|
|
|
if (inRange && n === ":") {
|
|
|
|
i++; // skip [
|
|
|
|
let value = "";
|
|
|
|
while (glob[++i] !== ":") value += glob[i];
|
2020-02-06 17:25:39 -05:00
|
|
|
if (value === "alnum") add("(?:\\w|\\d)");
|
2019-03-02 14:56:19 -05:00
|
|
|
else if (value === "space") add("\\s");
|
|
|
|
else if (value === "digit") add("\\d");
|
|
|
|
i++; // skip last ]
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (extended) {
|
|
|
|
inRange = true;
|
|
|
|
add(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "]") {
|
|
|
|
if (extended) {
|
|
|
|
inRange = false;
|
|
|
|
add(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "{") {
|
|
|
|
if (extended) {
|
|
|
|
inGroup = true;
|
2020-02-06 17:25:39 -05:00
|
|
|
add("(?:");
|
2019-03-02 14:56:19 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "}") {
|
|
|
|
if (extended) {
|
|
|
|
inGroup = false;
|
|
|
|
add(")");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === ",") {
|
|
|
|
if (inGroup) {
|
|
|
|
add("|");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
add(`\\${c}`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c === "*") {
|
|
|
|
if (n === "(" && extended) {
|
|
|
|
ext.push(c);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Move over all consecutive "*"'s.
|
|
|
|
// Also store the previous and next characters
|
2019-10-05 12:02:34 -04:00
|
|
|
const prevChar = glob[i - 1];
|
2019-03-02 14:56:19 -05:00
|
|
|
let starCount = 1;
|
|
|
|
while (glob[i + 1] === "*") {
|
|
|
|
starCount++;
|
|
|
|
i++;
|
|
|
|
}
|
2019-10-05 12:02:34 -04:00
|
|
|
const nextChar = glob[i + 1];
|
2019-03-02 14:56:19 -05:00
|
|
|
if (!globstar) {
|
|
|
|
// globstar is disabled, so treat any number of "*" as one
|
|
|
|
add(".*");
|
|
|
|
} else {
|
|
|
|
// globstar is enabled, so determine if this is a globstar segment
|
2020-07-14 15:24:17 -04:00
|
|
|
const isGlobstar = starCount > 1 && // multiple "*"'s
|
2019-06-19 00:22:01 -04:00
|
|
|
// from the start of the segment
|
2019-09-28 09:33:17 -04:00
|
|
|
[SEP_RAW, "/", undefined].includes(prevChar) &&
|
2019-06-19 00:22:01 -04:00
|
|
|
// to the end of the segment
|
2019-09-28 09:33:17 -04:00
|
|
|
[SEP_RAW, "/", undefined].includes(nextChar);
|
2019-03-02 14:56:19 -05:00
|
|
|
if (isGlobstar) {
|
|
|
|
// it's a globstar, so match zero or more path segments
|
|
|
|
add(GLOBSTAR, { only: "regex" });
|
|
|
|
add(GLOBSTAR_SEGMENT, { only: "path", last: true, split: true });
|
|
|
|
i++; // move over the "/"
|
|
|
|
} else {
|
|
|
|
// it's not a globstar, so only match one path segment
|
|
|
|
add(WILDCARD, { only: "regex" });
|
|
|
|
add(WILDCARD_SEGMENT, { only: "path" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
add(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
// When regexp 'g' flag is specified don't
|
|
|
|
// constrain the regular expression with ^ & $
|
|
|
|
if (!flags.includes("g")) {
|
|
|
|
regex = `^${regex}$`;
|
|
|
|
segment = `^${segment}$`;
|
2019-09-28 09:33:17 -04:00
|
|
|
if (filepath) pathRegexStr = `^${pathRegexStr}$`;
|
2019-03-02 14:56:19 -05:00
|
|
|
}
|
|
|
|
|
2019-03-12 01:51:51 -04:00
|
|
|
const result: GlobrexResult = { regex: new RegExp(regex, flags) };
|
2019-03-02 14:56:19 -05:00
|
|
|
|
|
|
|
// Push the last segment
|
|
|
|
if (filepath) {
|
2019-09-28 09:33:17 -04:00
|
|
|
pathSegments.push(new RegExp(segment, flags));
|
|
|
|
result.path = {
|
|
|
|
regex: new RegExp(pathRegexStr, flags),
|
|
|
|
segments: pathSegments,
|
|
|
|
globstar: new RegExp(
|
|
|
|
!flags.includes("g") ? `^${GLOBSTAR_SEGMENT}$` : GLOBSTAR_SEGMENT,
|
2020-07-14 15:24:17 -04:00
|
|
|
flags,
|
2020-03-28 13:03:49 -04:00
|
|
|
),
|
2019-09-28 09:33:17 -04:00
|
|
|
};
|
2019-03-02 14:56:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|