| 1 | "use strict";
|
|---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
|---|
| 3 | exports.isAbsolute = exports.partitionAbsoluteAndRelative = exports.removeDuplicateSlashes = exports.matchAny = exports.convertPatternsToRe = exports.makeRe = exports.getPatternParts = exports.expandBraceExpansion = exports.expandPatternsWithBraceExpansion = exports.isAffectDepthOfReadingPattern = exports.endsWithSlashGlobStar = exports.hasGlobStar = exports.getBaseDirectory = exports.isPatternRelatedToParentDirectory = exports.getPatternsOutsideCurrentDirectory = exports.getPatternsInsideCurrentDirectory = exports.getPositivePatterns = exports.getNegativePatterns = exports.isPositivePattern = exports.isNegativePattern = exports.convertToNegativePattern = exports.convertToPositivePattern = exports.isDynamicPattern = exports.isStaticPattern = void 0;
|
|---|
| 4 | const path = require("path");
|
|---|
| 5 | const globParent = require("glob-parent");
|
|---|
| 6 | const micromatch = require("micromatch");
|
|---|
| 7 | const GLOBSTAR = '**';
|
|---|
| 8 | const ESCAPE_SYMBOL = '\\';
|
|---|
| 9 | const COMMON_GLOB_SYMBOLS_RE = /[*?]|^!/;
|
|---|
| 10 | const REGEX_CHARACTER_CLASS_SYMBOLS_RE = /\[[^[]*]/;
|
|---|
| 11 | const REGEX_GROUP_SYMBOLS_RE = /(?:^|[^!*+?@])\([^(]*\|[^|]*\)/;
|
|---|
| 12 | const GLOB_EXTENSION_SYMBOLS_RE = /[!*+?@]\([^(]*\)/;
|
|---|
| 13 | const BRACE_EXPANSION_SEPARATORS_RE = /,|\.\./;
|
|---|
| 14 | /**
|
|---|
| 15 | * Matches a sequence of two or more consecutive slashes, excluding the first two slashes at the beginning of the string.
|
|---|
| 16 | * The latter is due to the presence of the device path at the beginning of the UNC path.
|
|---|
| 17 | */
|
|---|
| 18 | const DOUBLE_SLASH_RE = /(?!^)\/{2,}/g;
|
|---|
| 19 | function isStaticPattern(pattern, options = {}) {
|
|---|
| 20 | return !isDynamicPattern(pattern, options);
|
|---|
| 21 | }
|
|---|
| 22 | exports.isStaticPattern = isStaticPattern;
|
|---|
| 23 | function isDynamicPattern(pattern, options = {}) {
|
|---|
| 24 | /**
|
|---|
| 25 | * A special case with an empty string is necessary for matching patterns that start with a forward slash.
|
|---|
| 26 | * An empty string cannot be a dynamic pattern.
|
|---|
| 27 | * For example, the pattern `/lib/*` will be spread into parts: '', 'lib', '*'.
|
|---|
| 28 | */
|
|---|
| 29 | if (pattern === '') {
|
|---|
| 30 | return false;
|
|---|
| 31 | }
|
|---|
| 32 | /**
|
|---|
| 33 | * When the `caseSensitiveMatch` option is disabled, all patterns must be marked as dynamic, because we cannot check
|
|---|
| 34 | * filepath directly (without read directory).
|
|---|
| 35 | */
|
|---|
| 36 | if (options.caseSensitiveMatch === false || pattern.includes(ESCAPE_SYMBOL)) {
|
|---|
| 37 | return true;
|
|---|
| 38 | }
|
|---|
| 39 | if (COMMON_GLOB_SYMBOLS_RE.test(pattern) || REGEX_CHARACTER_CLASS_SYMBOLS_RE.test(pattern) || REGEX_GROUP_SYMBOLS_RE.test(pattern)) {
|
|---|
| 40 | return true;
|
|---|
| 41 | }
|
|---|
| 42 | if (options.extglob !== false && GLOB_EXTENSION_SYMBOLS_RE.test(pattern)) {
|
|---|
| 43 | return true;
|
|---|
| 44 | }
|
|---|
| 45 | if (options.braceExpansion !== false && hasBraceExpansion(pattern)) {
|
|---|
| 46 | return true;
|
|---|
| 47 | }
|
|---|
| 48 | return false;
|
|---|
| 49 | }
|
|---|
| 50 | exports.isDynamicPattern = isDynamicPattern;
|
|---|
| 51 | function hasBraceExpansion(pattern) {
|
|---|
| 52 | const openingBraceIndex = pattern.indexOf('{');
|
|---|
| 53 | if (openingBraceIndex === -1) {
|
|---|
| 54 | return false;
|
|---|
| 55 | }
|
|---|
| 56 | const closingBraceIndex = pattern.indexOf('}', openingBraceIndex + 1);
|
|---|
| 57 | if (closingBraceIndex === -1) {
|
|---|
| 58 | return false;
|
|---|
| 59 | }
|
|---|
| 60 | const braceContent = pattern.slice(openingBraceIndex, closingBraceIndex);
|
|---|
| 61 | return BRACE_EXPANSION_SEPARATORS_RE.test(braceContent);
|
|---|
| 62 | }
|
|---|
| 63 | function convertToPositivePattern(pattern) {
|
|---|
| 64 | return isNegativePattern(pattern) ? pattern.slice(1) : pattern;
|
|---|
| 65 | }
|
|---|
| 66 | exports.convertToPositivePattern = convertToPositivePattern;
|
|---|
| 67 | function convertToNegativePattern(pattern) {
|
|---|
| 68 | return '!' + pattern;
|
|---|
| 69 | }
|
|---|
| 70 | exports.convertToNegativePattern = convertToNegativePattern;
|
|---|
| 71 | function isNegativePattern(pattern) {
|
|---|
| 72 | return pattern.startsWith('!') && pattern[1] !== '(';
|
|---|
| 73 | }
|
|---|
| 74 | exports.isNegativePattern = isNegativePattern;
|
|---|
| 75 | function isPositivePattern(pattern) {
|
|---|
| 76 | return !isNegativePattern(pattern);
|
|---|
| 77 | }
|
|---|
| 78 | exports.isPositivePattern = isPositivePattern;
|
|---|
| 79 | function getNegativePatterns(patterns) {
|
|---|
| 80 | return patterns.filter(isNegativePattern);
|
|---|
| 81 | }
|
|---|
| 82 | exports.getNegativePatterns = getNegativePatterns;
|
|---|
| 83 | function getPositivePatterns(patterns) {
|
|---|
| 84 | return patterns.filter(isPositivePattern);
|
|---|
| 85 | }
|
|---|
| 86 | exports.getPositivePatterns = getPositivePatterns;
|
|---|
| 87 | /**
|
|---|
| 88 | * Returns patterns that can be applied inside the current directory.
|
|---|
| 89 | *
|
|---|
| 90 | * @example
|
|---|
| 91 | * // ['./*', '*', 'a/*']
|
|---|
| 92 | * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
|
|---|
| 93 | */
|
|---|
| 94 | function getPatternsInsideCurrentDirectory(patterns) {
|
|---|
| 95 | return patterns.filter((pattern) => !isPatternRelatedToParentDirectory(pattern));
|
|---|
| 96 | }
|
|---|
| 97 | exports.getPatternsInsideCurrentDirectory = getPatternsInsideCurrentDirectory;
|
|---|
| 98 | /**
|
|---|
| 99 | * Returns patterns to be expanded relative to (outside) the current directory.
|
|---|
| 100 | *
|
|---|
| 101 | * @example
|
|---|
| 102 | * // ['../*', './../*']
|
|---|
| 103 | * getPatternsInsideCurrentDirectory(['./*', '*', 'a/*', '../*', './../*'])
|
|---|
| 104 | */
|
|---|
| 105 | function getPatternsOutsideCurrentDirectory(patterns) {
|
|---|
| 106 | return patterns.filter(isPatternRelatedToParentDirectory);
|
|---|
| 107 | }
|
|---|
| 108 | exports.getPatternsOutsideCurrentDirectory = getPatternsOutsideCurrentDirectory;
|
|---|
| 109 | function isPatternRelatedToParentDirectory(pattern) {
|
|---|
| 110 | return pattern.startsWith('..') || pattern.startsWith('./..');
|
|---|
| 111 | }
|
|---|
| 112 | exports.isPatternRelatedToParentDirectory = isPatternRelatedToParentDirectory;
|
|---|
| 113 | function getBaseDirectory(pattern) {
|
|---|
| 114 | return globParent(pattern, { flipBackslashes: false });
|
|---|
| 115 | }
|
|---|
| 116 | exports.getBaseDirectory = getBaseDirectory;
|
|---|
| 117 | function hasGlobStar(pattern) {
|
|---|
| 118 | return pattern.includes(GLOBSTAR);
|
|---|
| 119 | }
|
|---|
| 120 | exports.hasGlobStar = hasGlobStar;
|
|---|
| 121 | function endsWithSlashGlobStar(pattern) {
|
|---|
| 122 | return pattern.endsWith('/' + GLOBSTAR);
|
|---|
| 123 | }
|
|---|
| 124 | exports.endsWithSlashGlobStar = endsWithSlashGlobStar;
|
|---|
| 125 | function isAffectDepthOfReadingPattern(pattern) {
|
|---|
| 126 | const basename = path.basename(pattern);
|
|---|
| 127 | return endsWithSlashGlobStar(pattern) || isStaticPattern(basename);
|
|---|
| 128 | }
|
|---|
| 129 | exports.isAffectDepthOfReadingPattern = isAffectDepthOfReadingPattern;
|
|---|
| 130 | function expandPatternsWithBraceExpansion(patterns) {
|
|---|
| 131 | return patterns.reduce((collection, pattern) => {
|
|---|
| 132 | return collection.concat(expandBraceExpansion(pattern));
|
|---|
| 133 | }, []);
|
|---|
| 134 | }
|
|---|
| 135 | exports.expandPatternsWithBraceExpansion = expandPatternsWithBraceExpansion;
|
|---|
| 136 | function expandBraceExpansion(pattern) {
|
|---|
| 137 | const patterns = micromatch.braces(pattern, { expand: true, nodupes: true, keepEscaping: true });
|
|---|
| 138 | /**
|
|---|
| 139 | * Sort the patterns by length so that the same depth patterns are processed side by side.
|
|---|
| 140 | * `a/{b,}/{c,}/*` – `['a///*', 'a/b//*', 'a//c/*', 'a/b/c/*']`
|
|---|
| 141 | */
|
|---|
| 142 | patterns.sort((a, b) => a.length - b.length);
|
|---|
| 143 | /**
|
|---|
| 144 | * Micromatch can return an empty string in the case of patterns like `{a,}`.
|
|---|
| 145 | */
|
|---|
| 146 | return patterns.filter((pattern) => pattern !== '');
|
|---|
| 147 | }
|
|---|
| 148 | exports.expandBraceExpansion = expandBraceExpansion;
|
|---|
| 149 | function getPatternParts(pattern, options) {
|
|---|
| 150 | let { parts } = micromatch.scan(pattern, Object.assign(Object.assign({}, options), { parts: true }));
|
|---|
| 151 | /**
|
|---|
| 152 | * The scan method returns an empty array in some cases.
|
|---|
| 153 | * See micromatch/picomatch#58 for more details.
|
|---|
| 154 | */
|
|---|
| 155 | if (parts.length === 0) {
|
|---|
| 156 | parts = [pattern];
|
|---|
| 157 | }
|
|---|
| 158 | /**
|
|---|
| 159 | * The scan method does not return an empty part for the pattern with a forward slash.
|
|---|
| 160 | * This is another part of micromatch/picomatch#58.
|
|---|
| 161 | */
|
|---|
| 162 | if (parts[0].startsWith('/')) {
|
|---|
| 163 | parts[0] = parts[0].slice(1);
|
|---|
| 164 | parts.unshift('');
|
|---|
| 165 | }
|
|---|
| 166 | return parts;
|
|---|
| 167 | }
|
|---|
| 168 | exports.getPatternParts = getPatternParts;
|
|---|
| 169 | function makeRe(pattern, options) {
|
|---|
| 170 | return micromatch.makeRe(pattern, options);
|
|---|
| 171 | }
|
|---|
| 172 | exports.makeRe = makeRe;
|
|---|
| 173 | function convertPatternsToRe(patterns, options) {
|
|---|
| 174 | return patterns.map((pattern) => makeRe(pattern, options));
|
|---|
| 175 | }
|
|---|
| 176 | exports.convertPatternsToRe = convertPatternsToRe;
|
|---|
| 177 | function matchAny(entry, patternsRe) {
|
|---|
| 178 | return patternsRe.some((patternRe) => patternRe.test(entry));
|
|---|
| 179 | }
|
|---|
| 180 | exports.matchAny = matchAny;
|
|---|
| 181 | /**
|
|---|
| 182 | * This package only works with forward slashes as a path separator.
|
|---|
| 183 | * Because of this, we cannot use the standard `path.normalize` method, because on Windows platform it will use of backslashes.
|
|---|
| 184 | */
|
|---|
| 185 | function removeDuplicateSlashes(pattern) {
|
|---|
| 186 | return pattern.replace(DOUBLE_SLASH_RE, '/');
|
|---|
| 187 | }
|
|---|
| 188 | exports.removeDuplicateSlashes = removeDuplicateSlashes;
|
|---|
| 189 | function partitionAbsoluteAndRelative(patterns) {
|
|---|
| 190 | const absolute = [];
|
|---|
| 191 | const relative = [];
|
|---|
| 192 | for (const pattern of patterns) {
|
|---|
| 193 | if (isAbsolute(pattern)) {
|
|---|
| 194 | absolute.push(pattern);
|
|---|
| 195 | }
|
|---|
| 196 | else {
|
|---|
| 197 | relative.push(pattern);
|
|---|
| 198 | }
|
|---|
| 199 | }
|
|---|
| 200 | return [absolute, relative];
|
|---|
| 201 | }
|
|---|
| 202 | exports.partitionAbsoluteAndRelative = partitionAbsoluteAndRelative;
|
|---|
| 203 | function isAbsolute(pattern) {
|
|---|
| 204 | return path.isAbsolute(pattern);
|
|---|
| 205 | }
|
|---|
| 206 | exports.isAbsolute = isAbsolute;
|
|---|