Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
glob.js248 linesDownload Raw Back to commonjs
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.Glob = void 0;
4const minimatch_1 = require("minimatch");
5const node_url_1 = require("node:url");
6const path_scurry_1 = require("path-scurry");
7const pattern_js_1 = require("./pattern.js");
8const walker_js_1 = require("./walker.js");
9// if no process global, just call it linux.
10// so we default to case-sensitive, / separators
11const defaultPlatform = (typeof process === 'object' &&
12    process &&
13    typeof process.platform === 'string') ?
14    process.platform
15    : 'linux';
16/**
17 * An object that can perform glob pattern traversals.
18 */
19class Glob {
20    absolute;
21    cwd;
22    root;
23    dot;
24    dotRelative;
25    follow;
26    ignore;
27    magicalBraces;
28    mark;
29    matchBase;
30    maxDepth;
31    nobrace;
32    nocase;
33    nodir;
34    noext;
35    noglobstar;
36    pattern;
37    platform;
38    realpath;
39    scurry;
40    stat;
41    signal;
42    windowsPathsNoEscape;
43    withFileTypes;
44    includeChildMatches;
45    /**
46     * The options provided to the constructor.
47     */
48    opts;
49    /**
50     * An array of parsed immutable {@link Pattern} objects.
51     */
52    patterns;
53    /**
54     * All options are stored as properties on the `Glob` object.
55     *
56     * See {@link GlobOptions} for full options descriptions.
57     *
58     * Note that a previous `Glob` object can be passed as the
59     * `GlobOptions` to another `Glob` instantiation to re-use settings
60     * and caches with a new pattern.
61     *
62     * Traversal functions can be called multiple times to run the walk
63     * again.
64     */
65    constructor(pattern, opts) {
66        /* c8 ignore start */
67        if (!opts)
68            throw new TypeError('glob options required');
69        /* c8 ignore stop */
70        this.withFileTypes = !!opts.withFileTypes;
71        this.signal = opts.signal;
72        this.follow = !!opts.follow;
73        this.dot = !!opts.dot;
74        this.dotRelative = !!opts.dotRelative;
75        this.nodir = !!opts.nodir;
76        this.mark = !!opts.mark;
77        if (!opts.cwd) {
78            this.cwd = '';
79        }
80        else if (opts.cwd instanceof URL || opts.cwd.startsWith('file://')) {
81            opts.cwd = (0, node_url_1.fileURLToPath)(opts.cwd);
82        }
83        this.cwd = opts.cwd || '';
84        this.root = opts.root;
85        this.magicalBraces = !!opts.magicalBraces;
86        this.nobrace = !!opts.nobrace;
87        this.noext = !!opts.noext;
88        this.realpath = !!opts.realpath;
89        this.absolute = opts.absolute;
90        this.includeChildMatches = opts.includeChildMatches !== false;
91        this.noglobstar = !!opts.noglobstar;
92        this.matchBase = !!opts.matchBase;
93        this.maxDepth =
94            typeof opts.maxDepth === 'number' ? opts.maxDepth : Infinity;
95        this.stat = !!opts.stat;
96        this.ignore = opts.ignore;
97        if (this.withFileTypes && this.absolute !== undefined) {
98            throw new Error('cannot set absolute and withFileTypes:true');
99        }
100        if (typeof pattern === 'string') {
101            pattern = [pattern];
102        }
103        this.windowsPathsNoEscape =
104            !!opts.windowsPathsNoEscape ||
105                opts.allowWindowsEscape ===
106                    false;
107        if (this.windowsPathsNoEscape) {
108            pattern = pattern.map(p => p.replace(/\\/g, '/'));
109        }
110        if (this.matchBase) {
111            if (opts.noglobstar) {
112                throw new TypeError('base matching requires globstar');
113            }
114            pattern = pattern.map(p => (p.includes('/') ? p : `./**/${p}`));
115        }
116        this.pattern = pattern;
117        this.platform = opts.platform || defaultPlatform;
118        this.opts = { ...opts, platform: this.platform };
119        if (opts.scurry) {
120            this.scurry = opts.scurry;
121            if (opts.nocase !== undefined &&
122                opts.nocase !== opts.scurry.nocase) {
123                throw new Error('nocase option contradicts provided scurry option');
124            }
125        }
126        else {
127            const Scurry = opts.platform === 'win32' ? path_scurry_1.PathScurryWin32
128                : opts.platform === 'darwin' ? path_scurry_1.PathScurryDarwin
129                    : opts.platform ? path_scurry_1.PathScurryPosix
130                        : path_scurry_1.PathScurry;
131            this.scurry = new Scurry(this.cwd, {
132                nocase: opts.nocase,
133                fs: opts.fs,
134            });
135        }
136        this.nocase = this.scurry.nocase;
137        // If you do nocase:true on a case-sensitive file system, then
138        // we need to use regexps instead of strings for non-magic
139        // path portions, because statting `aBc` won't return results
140        // for the file `AbC` for example.
141        const nocaseMagicOnly = this.platform === 'darwin' || this.platform === 'win32';
142        const mmo = {
143            braceExpandMax: 10_000,
144            ...opts,
145            dot: this.dot,
146            matchBase: this.matchBase,
147            nobrace: this.nobrace,
148            // default nocase based on platform
149            nocase: this.nocase,
150            nocaseMagicOnly,
151            nocomment: true,
152            noext: this.noext,
153            nonegate: true,
154            optimizationLevel: 2,
155            platform: this.platform,
156            windowsPathsNoEscape: this.windowsPathsNoEscape,
157            debug: !!this.opts.debug,
158        };
159        const mms = this.pattern.map(p => new minimatch_1.Minimatch(p, mmo));
160        const [matchSet, globParts] = mms.reduce((set, m) => {
161            set[0].push(...m.set);
162            set[1].push(...m.globParts);
163            return set;
164        }, [[], []]);
165        this.patterns = matchSet.map((set, i) => {
166            const g = globParts[i];
167            /* c8 ignore start */
168            if (!g)
169                throw new Error('invalid pattern object');
170            /* c8 ignore stop */
171            return new pattern_js_1.Pattern(set, g, 0, this.platform);
172        });
173    }
174    async walk() {
175        // Walkers always return array of Path objects, so we just have to
176        // coerce them into the right shape.  It will have already called
177        // realpath() if the option was set to do so, so we know that's cached.
178        // start out knowing the cwd, at least
179        return [
180            ...(await new walker_js_1.GlobWalker(this.patterns, this.scurry.cwd, {
181                ...this.opts,
182                maxDepth: this.maxDepth !== Infinity ?
183                    this.maxDepth + this.scurry.cwd.depth()
184                    : Infinity,
185                platform: this.platform,
186                nocase: this.nocase,
187                includeChildMatches: this.includeChildMatches,
188            }).walk()),
189        ];
190    }
191    walkSync() {
192        return [
193            ...new walker_js_1.GlobWalker(this.patterns, this.scurry.cwd, {
194                ...this.opts,
195                maxDepth: this.maxDepth !== Infinity ?
196                    this.maxDepth + this.scurry.cwd.depth()
197                    : Infinity,
198                platform: this.platform,
199                nocase: this.nocase,
200                includeChildMatches: this.includeChildMatches,
201            }).walkSync(),
202        ];
203    }
204    stream() {
205        return new walker_js_1.GlobStream(this.patterns, this.scurry.cwd, {
206            ...this.opts,
207            maxDepth: this.maxDepth !== Infinity ?
208                this.maxDepth + this.scurry.cwd.depth()
209                : Infinity,
210            platform: this.platform,
211            nocase: this.nocase,
212            includeChildMatches: this.includeChildMatches,
213        }).stream();
214    }
215    streamSync() {
216        return new walker_js_1.GlobStream(this.patterns, this.scurry.cwd, {
217            ...this.opts,
218            maxDepth: this.maxDepth !== Infinity ?
219                this.maxDepth + this.scurry.cwd.depth()
220                : Infinity,
221            platform: this.platform,
222            nocase: this.nocase,
223            includeChildMatches: this.includeChildMatches,
224        }).streamSync();
225    }
226    /**
227     * Default sync iteration function. Returns a Generator that
228     * iterates over the results.
229     */
230    iterateSync() {
231        return this.streamSync()[Symbol.iterator]();
232    }
233    [Symbol.iterator]() {
234        return this.iterateSync();
235    }
236    /**
237     * Default async iteration function. Returns an AsyncGenerator that
238     * iterates over the results.
239     */
240    iterate() {
241        return this.stream()[Symbol.asyncIterator]();
242    }
243    [Symbol.asyncIterator]() {
244        return this.iterate();
245    }
246}
247exports.Glob = Glob;
248//# sourceMappingURL=glob.js.map
codekingpro/portable-devtools · Team Ai