glob.js 3.3 KB

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