|
| 1 | +const fs = require('fs') |
| 2 | +const globrex = require('globrex') |
| 3 | +const { promisify } = require('util') |
| 4 | +const globalyzer = require('globalyzer') |
| 5 | +const { join, resolve, relative } = require('path') |
| 6 | +const isHidden = /(^|[\\\/])\.[^\\\/\.]/g |
| 7 | +const readdir = promisify(fs.readdir) |
| 8 | +const stat = promisify(fs.stat) |
| 9 | +let CACHE = {} |
| 10 | + |
| 11 | +async function walk(output, prefix, lexer, opts, dirname = '', level = 0) { |
| 12 | + if (output.length === 1) return |
| 13 | + const rgx = lexer.segments[level] |
| 14 | + const dir = join(opts.cwd, prefix, dirname) |
| 15 | + const files = await readdir(dir) |
| 16 | + const { dot, filesOnly } = opts |
| 17 | + |
| 18 | + let i = 0, |
| 19 | + len = files.length, |
| 20 | + file |
| 21 | + let fullpath, relpath, stats, isMatch |
| 22 | + |
| 23 | + for (; i < len; i++) { |
| 24 | + file = files[i] |
| 25 | + if (file === 'node_modules') continue |
| 26 | + fullpath = join(dir, file) |
| 27 | + relpath = dirname ? join(dirname, file) : file |
| 28 | + if (!dot && isHidden.test(relpath)) continue |
| 29 | + isMatch = lexer.regex.test(relpath) |
| 30 | + |
| 31 | + if ((stats = CACHE[relpath]) === void 0) { |
| 32 | + CACHE[relpath] = stats = fs.lstatSync(fullpath) |
| 33 | + } |
| 34 | + |
| 35 | + if (!stats.isDirectory()) { |
| 36 | + if (isMatch) { |
| 37 | + output.push(relative(opts.cwd, fullpath)) |
| 38 | + return |
| 39 | + } |
| 40 | + continue |
| 41 | + } |
| 42 | + |
| 43 | + if (rgx && !rgx.test(file)) continue |
| 44 | + if (!filesOnly && isMatch) { |
| 45 | + output.push(join(prefix, relpath)) |
| 46 | + return |
| 47 | + } |
| 48 | + |
| 49 | + await walk( |
| 50 | + output, |
| 51 | + prefix, |
| 52 | + lexer, |
| 53 | + opts, |
| 54 | + relpath, |
| 55 | + rgx && rgx.toString() !== lexer.globstar && ++level |
| 56 | + ) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Find files using bash-like globbing. |
| 62 | + * All paths are normalized compared to node-glob. |
| 63 | + * @param {String} str Glob string |
| 64 | + * @param {String} [options.cwd='.'] Current working directory |
| 65 | + * @param {Boolean} [options.dot=false] Include dotfile matches |
| 66 | + * @param {Boolean} [options.absolute=false] Return absolute paths |
| 67 | + * @param {Boolean} [options.filesOnly=false] Do not include folders if true |
| 68 | + * @param {Boolean} [options.flush=false] Reset cache object |
| 69 | + * @returns {Array} array containing matching files |
| 70 | + */ |
| 71 | +export async function globSingle(str, opts = {}) { |
| 72 | + if (!str) return [] |
| 73 | + |
| 74 | + let glob = globalyzer(str) |
| 75 | + |
| 76 | + opts.cwd = opts.cwd || '.' |
| 77 | + |
| 78 | + if (!glob.isGlob) { |
| 79 | + try { |
| 80 | + let resolved = resolve(opts.cwd, str) |
| 81 | + let dirent = await stat(resolved) |
| 82 | + if (opts.filesOnly && !dirent.isFile()) return [] |
| 83 | + |
| 84 | + return opts.absolute ? [resolved] : [str] |
| 85 | + } catch (err) { |
| 86 | + if (err.code != 'ENOENT') throw err |
| 87 | + |
| 88 | + return [] |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (opts.flush) CACHE = {} |
| 93 | + |
| 94 | + let matches = [] |
| 95 | + const { path } = globrex(glob.glob, { |
| 96 | + filepath: true, |
| 97 | + globstar: true, |
| 98 | + extended: true, |
| 99 | + }) |
| 100 | + |
| 101 | + path.globstar = path.globstar.toString() |
| 102 | + await walk(matches, glob.base, path, opts, '.', 0) |
| 103 | + |
| 104 | + return opts.absolute ? matches.map((x) => resolve(opts.cwd, x)) : matches |
| 105 | +} |
0 commit comments