glob.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.globSync = globSync;
  4. const pathModule = require("node:path");
  5. const glob_to_regex_js_1 = require("glob-to-regex.js");
  6. const util_1 = require("./util");
  7. const { join, relative, resolve } = pathModule.posix;
  8. /**
  9. * Check if a path matches a glob pattern
  10. */
  11. function matchesPattern(path, pattern) {
  12. const regex = (0, glob_to_regex_js_1.toRegex)(pattern);
  13. return regex.test(path);
  14. }
  15. /**
  16. * Check if a path should be excluded based on exclude patterns
  17. */
  18. function isExcluded(path, exclude) {
  19. if (!exclude)
  20. return false;
  21. if (typeof exclude === 'function') {
  22. return exclude(path);
  23. }
  24. const patterns = Array.isArray(exclude) ? exclude : [exclude];
  25. return patterns.some(pattern => matchesPattern(path, pattern));
  26. }
  27. /**
  28. * Walk directory tree and collect matching paths
  29. */
  30. function walkDirectory(fs, dir, patterns, options, currentDepth = 0) {
  31. const results = [];
  32. const maxDepth = options.maxdepth ?? Infinity;
  33. const baseCwd = options.cwd ? (0, util_1.pathToFilename)(options.cwd) : process.cwd();
  34. if (currentDepth > maxDepth) {
  35. return results;
  36. }
  37. try {
  38. const entries = fs.readdirSync(dir, { withFileTypes: true });
  39. for (const entry of entries) {
  40. const fullPath = join(dir, entry.name.toString());
  41. const relativePath = relative(baseCwd, fullPath);
  42. // Skip if excluded
  43. if (isExcluded(relativePath, options.exclude)) {
  44. continue;
  45. }
  46. // Check if this path matches any pattern
  47. const matches = patterns.some(pattern => matchesPattern(relativePath, pattern));
  48. if (matches) {
  49. results.push(relativePath);
  50. }
  51. // Recurse into directories
  52. if (entry.isDirectory() && currentDepth < maxDepth) {
  53. const subResults = walkDirectory(fs, fullPath, patterns, options, currentDepth + 1);
  54. results.push(...subResults);
  55. }
  56. }
  57. }
  58. catch (err) {
  59. // Skip directories we can't read
  60. }
  61. return results;
  62. }
  63. /**
  64. * Main glob implementation
  65. */
  66. function globSync(fs, pattern, options = {}) {
  67. const cwd = options.cwd ? (0, util_1.pathToFilename)(options.cwd) : process.cwd();
  68. const resolvedCwd = resolve(cwd);
  69. const globOptions = {
  70. cwd: resolvedCwd,
  71. exclude: options.exclude,
  72. maxdepth: options.maxdepth,
  73. withFileTypes: options.withFileTypes || false,
  74. };
  75. let results = [];
  76. // Handle absolute patterns
  77. if (pathModule.posix.isAbsolute(pattern)) {
  78. const dir = pathModule.posix.dirname(pattern);
  79. const basename = pathModule.posix.basename(pattern);
  80. const dirResults = walkDirectory(fs, dir, [basename], { ...globOptions, cwd: dir });
  81. results.push(...dirResults.map(r => pathModule.posix.resolve(dir, r)));
  82. }
  83. else {
  84. // Handle relative patterns
  85. const dirResults = walkDirectory(fs, resolvedCwd, [pattern], globOptions);
  86. results.push(...dirResults);
  87. }
  88. // Remove duplicates and sort
  89. results = [...new Set(results)].sort();
  90. return results;
  91. }
  92. //# sourceMappingURL=glob.js.map