index.mjs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // src/index.ts
  2. import path, { posix } from "path";
  3. import { fdir } from "fdir";
  4. import picomatch2 from "picomatch";
  5. // src/utils.ts
  6. import picomatch from "picomatch";
  7. var ESCAPED_WIN32_BACKSLASHES = /\\(?![()[\]{}!+@])/g;
  8. function convertPosixPathToPattern(path2) {
  9. return escapePosixPath(path2);
  10. }
  11. function convertWin32PathToPattern(path2) {
  12. return escapeWin32Path(path2).replace(ESCAPED_WIN32_BACKSLASHES, "/");
  13. }
  14. var convertPathToPattern = process.platform === "win32" ? convertWin32PathToPattern : convertPosixPathToPattern;
  15. var POSIX_UNESCAPED_GLOB_SYMBOLS = /(?<!\\)([()[\]{}*?|]|^!|[!+@](?=\()|\\(?![()[\]{}!*+?@|]))/g;
  16. var WIN32_UNESCAPED_GLOB_SYMBOLS = /(?<!\\)([()[\]{}]|^!|[!+@](?=\())/g;
  17. var escapePosixPath = (path2) => path2.replace(POSIX_UNESCAPED_GLOB_SYMBOLS, "\\$&");
  18. var escapeWin32Path = (path2) => path2.replace(WIN32_UNESCAPED_GLOB_SYMBOLS, "\\$&");
  19. var escapePath = process.platform === "win32" ? escapeWin32Path : escapePosixPath;
  20. function isDynamicPattern(pattern, options) {
  21. if ((options == null ? void 0 : options.caseSensitiveMatch) === false) {
  22. return true;
  23. }
  24. const scan = picomatch.scan(pattern);
  25. return scan.isGlob || scan.negated;
  26. }
  27. // src/index.ts
  28. function normalizePattern(pattern, expandDirectories, cwd, properties, isIgnore) {
  29. var _a;
  30. let result = pattern;
  31. if (pattern.endsWith("/")) {
  32. result = pattern.slice(0, -1);
  33. }
  34. if (!result.endsWith("*") && expandDirectories) {
  35. result += "/**";
  36. }
  37. if (path.isAbsolute(result.replace(/\\(?=[()[\]{}!*+?@|])/g, ""))) {
  38. result = posix.relative(cwd, result);
  39. } else {
  40. result = posix.normalize(result);
  41. }
  42. const parentDirectoryMatch = /^(\/?\.\.)+/.exec(result);
  43. if (parentDirectoryMatch == null ? void 0 : parentDirectoryMatch[0]) {
  44. const potentialRoot = posix.join(cwd, parentDirectoryMatch[0]);
  45. if (properties.root.length > potentialRoot.length) {
  46. properties.root = potentialRoot;
  47. properties.depthOffset = -(parentDirectoryMatch[0].length + 1) / 3;
  48. }
  49. } else if (!isIgnore && properties.depthOffset >= 0) {
  50. const current = result.split("/");
  51. (_a = properties.commonPath) != null ? _a : properties.commonPath = current;
  52. const newCommonPath = [];
  53. for (let i = 0; i < Math.min(properties.commonPath.length, current.length); i++) {
  54. const part = current[i];
  55. if (part === "**" && !current[i + 1]) {
  56. newCommonPath.pop();
  57. break;
  58. }
  59. if (part !== properties.commonPath[i] || isDynamicPattern(part) || i === current.length - 1) {
  60. break;
  61. }
  62. newCommonPath.push(part);
  63. }
  64. properties.depthOffset = newCommonPath.length;
  65. properties.commonPath = newCommonPath;
  66. properties.root = newCommonPath.length > 0 ? `${cwd}/${newCommonPath.join("/")}` : cwd;
  67. }
  68. return result;
  69. }
  70. function processPatterns({ patterns, ignore = [], expandDirectories = true }, cwd, properties) {
  71. if (typeof patterns === "string") {
  72. patterns = [patterns];
  73. } else if (!patterns) {
  74. patterns = ["**/*"];
  75. }
  76. if (typeof ignore === "string") {
  77. ignore = [ignore];
  78. }
  79. const matchPatterns = [];
  80. const ignorePatterns = [];
  81. for (const pattern of ignore) {
  82. if (!pattern.startsWith("!") || pattern[1] === "(") {
  83. const newPattern = normalizePattern(pattern, expandDirectories, cwd, properties, true);
  84. ignorePatterns.push(newPattern);
  85. }
  86. }
  87. for (const pattern of patterns) {
  88. if (!pattern.startsWith("!") || pattern[1] === "(") {
  89. const newPattern = normalizePattern(pattern, expandDirectories, cwd, properties, false);
  90. matchPatterns.push(newPattern);
  91. } else if (pattern[1] !== "!" || pattern[2] === "(") {
  92. const newPattern = normalizePattern(pattern.slice(1), expandDirectories, cwd, properties, true);
  93. ignorePatterns.push(newPattern);
  94. }
  95. }
  96. return { match: matchPatterns, ignore: ignorePatterns };
  97. }
  98. function getRelativePath(path2, cwd, root) {
  99. return posix.relative(cwd, `${root}/${path2}`);
  100. }
  101. function processPath(path2, cwd, root, isDirectory, absolute) {
  102. const relativePath = absolute ? path2.slice(root.length + 1) || "." : path2;
  103. if (root === cwd) {
  104. return isDirectory && relativePath !== "." ? relativePath.slice(0, -1) : relativePath;
  105. }
  106. return getRelativePath(relativePath, cwd, root);
  107. }
  108. function crawl(options, cwd, sync) {
  109. const properties = {
  110. root: cwd,
  111. commonPath: null,
  112. depthOffset: 0
  113. };
  114. const processed = processPatterns(options, cwd, properties);
  115. const matcher = picomatch2(processed.match, {
  116. dot: options.dot,
  117. nocase: options.caseSensitiveMatch === false,
  118. ignore: processed.ignore
  119. });
  120. const exclude = picomatch2(processed.ignore, {
  121. dot: options.dot,
  122. nocase: options.caseSensitiveMatch === false
  123. });
  124. const fdirOptions = {
  125. // use relative paths in the matcher
  126. filters: [(p, isDirectory) => matcher(processPath(p, cwd, properties.root, isDirectory, options.absolute))],
  127. exclude: (_, p) => exclude(processPath(p, cwd, properties.root, true, true)),
  128. pathSeparator: "/",
  129. relativePaths: true,
  130. resolveSymlinks: true
  131. };
  132. if (options.deep) {
  133. fdirOptions.maxDepth = Math.round(options.deep - properties.depthOffset);
  134. }
  135. if (options.absolute) {
  136. fdirOptions.relativePaths = false;
  137. fdirOptions.resolvePaths = true;
  138. fdirOptions.includeBasePath = true;
  139. }
  140. if (options.followSymbolicLinks === false) {
  141. fdirOptions.resolveSymlinks = false;
  142. fdirOptions.excludeSymlinks = true;
  143. }
  144. if (options.onlyDirectories) {
  145. fdirOptions.excludeFiles = true;
  146. fdirOptions.includeDirs = true;
  147. } else if (options.onlyFiles === false) {
  148. fdirOptions.includeDirs = true;
  149. }
  150. properties.root = properties.root.replace(/\\/g, "");
  151. const api = new fdir(fdirOptions).crawl(properties.root);
  152. if (cwd === properties.root || options.absolute) {
  153. return sync ? api.sync() : api.withPromise();
  154. }
  155. return sync ? api.sync().map((p) => getRelativePath(p, cwd, properties.root) + (!p || p.endsWith("/") ? "/" : "")) : api.withPromise().then((paths) => paths.map((p) => getRelativePath(p, cwd, properties.root) + (!p || p.endsWith("/") ? "/" : "")));
  156. }
  157. async function glob(patternsOrOptions, options) {
  158. if (patternsOrOptions && (options == null ? void 0 : options.patterns)) {
  159. throw new Error("Cannot pass patterns as both an argument and an option");
  160. }
  161. const opts = Array.isArray(patternsOrOptions) || typeof patternsOrOptions === "string" ? { ...options, patterns: patternsOrOptions } : patternsOrOptions;
  162. const cwd = opts.cwd ? path.resolve(opts.cwd).replace(/\\/g, "/") : process.cwd().replace(/\\/g, "/");
  163. return crawl(opts, cwd, false);
  164. }
  165. function globSync(patternsOrOptions, options) {
  166. if (patternsOrOptions && (options == null ? void 0 : options.patterns)) {
  167. throw new Error("Cannot pass patterns as both an argument and an option");
  168. }
  169. const opts = Array.isArray(patternsOrOptions) || typeof patternsOrOptions === "string" ? { ...options, patterns: patternsOrOptions } : patternsOrOptions;
  170. const cwd = opts.cwd ? path.resolve(opts.cwd).replace(/\\/g, "/") : process.cwd().replace(/\\/g, "/");
  171. return crawl(opts, cwd, true);
  172. }
  173. export {
  174. convertPathToPattern,
  175. escapePath,
  176. glob,
  177. globSync,
  178. isDynamicPattern
  179. };