UNPKG

2.01 kBJavaScriptView Raw
1const APIBuilder = require("./apiBuilder");
2var pm = require("picomatch");
3var globCache = {};
4
5function Builder() {
6 this.maxDepth = Infinity;
7 this.suppressErrors = true;
8 this.filters = [];
9}
10
11Builder.prototype.crawl = function(path) {
12 return new APIBuilder(path, this);
13};
14
15Builder.prototype.crawlWithOptions = function(path, options) {
16 if (!options.maxDepth) options.maxDepth = Infinity;
17 options.groupVar = options.group;
18 options.onlyCountsVar = options.onlyCounts;
19 options.excludeFn = options.exclude;
20 options.filters = options.filters || [];
21 return new APIBuilder(path, options);
22};
23
24Builder.prototype.withBasePath = function() {
25 this.includeBasePath = true;
26 return this;
27};
28
29Builder.prototype.withDirs = function() {
30 this.includeDirs = true;
31 return this;
32};
33
34Builder.prototype.withMaxDepth = function(depth) {
35 this.maxDepth = depth;
36 return this;
37};
38
39Builder.prototype.withFullPaths = function() {
40 this.resolvePaths = true;
41 this.includeBasePath = true;
42 return this;
43};
44
45Builder.prototype.withErrors = function() {
46 this.suppressErrors = false;
47 return this;
48};
49
50Builder.prototype.group = function() {
51 this.groupVar = true;
52 return this;
53};
54
55Builder.prototype.normalize = function() {
56 this.normalizePath = true;
57 return this;
58};
59
60Builder.prototype.filter = function(filterFn) {
61 this.filters.push(filterFn);
62 return this;
63};
64
65Builder.prototype.glob = function(...patterns) {
66 /* istanbul ignore next */
67 if (!pm) {
68 throw new Error(
69 `Please install picomatch: "npm i picomatch" to use glob matching.`
70 );
71 }
72 var isMatch = globCache[patterns.join()];
73 if (!isMatch) {
74 isMatch = pm(patterns, { dot: true });
75 globCache[patterns.join()] = isMatch;
76 }
77 this.filters.push((path) => isMatch(path));
78 return this;
79};
80
81Builder.prototype.exclude = function(excludeFn) {
82 this.excludeFn = excludeFn;
83 return this;
84};
85
86Builder.prototype.onlyCounts = function() {
87 this.onlyCountsVar = true;
88 return this;
89};
90
91module.exports = Builder;