index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.Builder = void 0;
  4. const path_1 = require("path");
  5. const api_builder_1 = require("./api-builder");
  6. var pm = null;
  7. /* c8 ignore next 6 */
  8. try {
  9. require.resolve("picomatch");
  10. pm = require("picomatch");
  11. }
  12. catch (_e) {
  13. // do nothing
  14. }
  15. class Builder {
  16. globCache = {};
  17. options = {
  18. maxDepth: Infinity,
  19. suppressErrors: true,
  20. pathSeparator: path_1.sep,
  21. filters: [],
  22. };
  23. globFunction;
  24. constructor(options) {
  25. this.options = { ...this.options, ...options };
  26. this.globFunction = this.options.globFunction;
  27. }
  28. group() {
  29. this.options.group = true;
  30. return this;
  31. }
  32. withPathSeparator(separator) {
  33. this.options.pathSeparator = separator;
  34. return this;
  35. }
  36. withBasePath() {
  37. this.options.includeBasePath = true;
  38. return this;
  39. }
  40. withRelativePaths() {
  41. this.options.relativePaths = true;
  42. return this;
  43. }
  44. withDirs() {
  45. this.options.includeDirs = true;
  46. return this;
  47. }
  48. withMaxDepth(depth) {
  49. this.options.maxDepth = depth;
  50. return this;
  51. }
  52. withMaxFiles(limit) {
  53. this.options.maxFiles = limit;
  54. return this;
  55. }
  56. withFullPaths() {
  57. this.options.resolvePaths = true;
  58. this.options.includeBasePath = true;
  59. return this;
  60. }
  61. withErrors() {
  62. this.options.suppressErrors = false;
  63. return this;
  64. }
  65. withSymlinks({ resolvePaths = true } = {}) {
  66. this.options.resolveSymlinks = true;
  67. this.options.useRealPaths = resolvePaths;
  68. return this.withFullPaths();
  69. }
  70. withAbortSignal(signal) {
  71. this.options.signal = signal;
  72. return this;
  73. }
  74. normalize() {
  75. this.options.normalizePath = true;
  76. return this;
  77. }
  78. filter(predicate) {
  79. this.options.filters.push(predicate);
  80. return this;
  81. }
  82. onlyDirs() {
  83. this.options.excludeFiles = true;
  84. this.options.includeDirs = true;
  85. return this;
  86. }
  87. exclude(predicate) {
  88. this.options.exclude = predicate;
  89. return this;
  90. }
  91. onlyCounts() {
  92. this.options.onlyCounts = true;
  93. return this;
  94. }
  95. crawl(root) {
  96. return new api_builder_1.APIBuilder(root || ".", this.options);
  97. }
  98. withGlobFunction(fn) {
  99. // cast this since we don't have the new type params yet
  100. this.globFunction = fn;
  101. return this;
  102. }
  103. /**
  104. * @deprecated Pass options using the constructor instead:
  105. * ```ts
  106. * new fdir(options).crawl("/path/to/root");
  107. * ```
  108. * This method will be removed in v7.0
  109. */
  110. /* c8 ignore next 4 */
  111. crawlWithOptions(root, options) {
  112. this.options = { ...this.options, ...options };
  113. return new api_builder_1.APIBuilder(root || ".", this.options);
  114. }
  115. glob(...patterns) {
  116. if (this.globFunction) {
  117. return this.globWithOptions(patterns);
  118. }
  119. return this.globWithOptions(patterns, ...[{ dot: true }]);
  120. }
  121. globWithOptions(patterns, ...options) {
  122. const globFn = (this.globFunction || pm);
  123. /* c8 ignore next 5 */
  124. if (!globFn) {
  125. throw new Error('Please specify a glob function to use glob matching.');
  126. }
  127. var isMatch = this.globCache[patterns.join("\0")];
  128. if (!isMatch) {
  129. isMatch = globFn(patterns, ...options);
  130. this.globCache[patterns.join("\0")] = isMatch;
  131. }
  132. this.options.filters.push((path) => isMatch(path));
  133. return this;
  134. }
  135. }
  136. exports.Builder = Builder;