1 | /**
|
2 | * @fileoverview An inherited `glob.GlobSync` to support .gitignore patterns.
|
3 | * @author Kael Zhang
|
4 | */
|
5 | ;
|
6 |
|
7 | //------------------------------------------------------------------------------
|
8 | // Requirements
|
9 | //------------------------------------------------------------------------------
|
10 |
|
11 | const Sync = require("glob").GlobSync,
|
12 | util = require("util");
|
13 |
|
14 | //------------------------------------------------------------------------------
|
15 | // Private
|
16 | //------------------------------------------------------------------------------
|
17 |
|
18 | const IGNORE = Symbol("ignore");
|
19 |
|
20 | /**
|
21 | * Subclass of `glob.GlobSync`
|
22 | * @param {string} pattern Pattern to be matched.
|
23 | * @param {Object} options `options` for `glob`
|
24 | * @param {function()} shouldIgnore Method to check whether a directory should be ignored.
|
25 | * @constructor
|
26 | */
|
27 | function GlobSync(pattern, options, shouldIgnore) {
|
28 |
|
29 | /**
|
30 | * We don't put this thing to argument `options` to avoid
|
31 | * further problems, such as `options` validation.
|
32 | *
|
33 | * Use `Symbol` as much as possible to avoid confliction.
|
34 | */
|
35 | this[IGNORE] = shouldIgnore;
|
36 |
|
37 | Sync.call(this, pattern, options);
|
38 | }
|
39 |
|
40 | util.inherits(GlobSync, Sync);
|
41 |
|
42 | /* eslint no-underscore-dangle: ["error", { "allow": ["_readdir", "_mark"] }] */
|
43 |
|
44 | GlobSync.prototype._readdir = function(abs, inGlobStar) {
|
45 |
|
46 | /**
|
47 | * `options.nodir` makes `options.mark` as `true`.
|
48 | * Mark `abs` first
|
49 | * to make sure `"node_modules"` will be ignored immediately with ignore pattern `"node_modules/"`.
|
50 | *
|
51 | * There is a built-in cache about marked `File.Stat` in `glob`, so that we could not worry about the extra invocation of `this._mark()`
|
52 | */
|
53 | const marked = this._mark(abs);
|
54 |
|
55 | if (this[IGNORE](marked)) {
|
56 | return null;
|
57 | }
|
58 |
|
59 | return Sync.prototype._readdir.call(this, abs, inGlobStar);
|
60 | };
|
61 |
|
62 |
|
63 | module.exports = GlobSync;
|