Skip to content

Commit 9b136f4

Browse files
author
Brad Cornes
committed
replace glob
1 parent 14ffaf4 commit 9b136f4

File tree

5 files changed

+139
-12
lines changed

5 files changed

+139
-12
lines changed

packages/tailwindcss-class-names/package-lock.json

Lines changed: 27 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tailwindcss-class-names/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
"chokidar": "^3.3.1",
1717
"dlv": "^1.1.3",
1818
"dset": "^2.0.1",
19-
"glob": "^7.1.6",
19+
"globalyzer": "^0.1.4",
20+
"globrex": "^0.1.2",
2021
"import-from": "^3.0.0",
2122
"pkg-up": "^3.1.0",
2223
"postcss-selector-parser": "^6.0.2",

packages/tailwindcss-class-names/src/getPlugins.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as path from 'path'
22
import stackTrace from 'stack-trace'
33
import pkgUp from 'pkg-up'
4-
import { glob } from './glob'
54
import { isObject } from './isObject'
65
import importFrom from 'import-from'
76

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

packages/tailwindcss-class-names/src/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import getVariants from './getVariants'
1111
import resolveConfig from './resolveConfig'
1212
import * as util from 'util'
1313
import * as path from 'path'
14-
import { glob } from './glob'
14+
import { globSingle } from './globSingle'
1515
import { getUtilityConfigMap } from './getUtilityConfigMap'
1616

1717
function TailwindConfigError(error) {
@@ -45,10 +45,11 @@ export default async function getClassNames(
4545
let tailwindcss
4646
let version
4747

48-
configPath = await glob(CONFIG_GLOB, {
48+
configPath = await globSingle(CONFIG_GLOB, {
4949
cwd,
50-
ignore: '**/node_modules/**',
51-
max: 1,
50+
filesOnly: true,
51+
absolute: true,
52+
flush: true,
5253
})
5354
invariant(configPath.length === 1, 'No Tailwind CSS config found.')
5455
configPath = configPath[0]

0 commit comments

Comments
 (0)