-
-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathglob.ts
45 lines (36 loc) · 1.2 KB
/
glob.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import path from 'path'
import fg from 'fast-glob'
import { Context, ComponentsInfo } from './types'
function toArray<T>(arr: T | T[]): T[] {
if (Array.isArray(arr))
return arr
return [arr]
}
export async function searchComponents(ctx: Context, force = false) {
if (force || !ctx._searchingPromise) {
ctx._searchingPromise = (async() => {
const { dirs, deep, extensions } = ctx.options
const exts = toArray(extensions)
if (!exts.length)
throw new Error('[vite-plugin-components] extensions are required to search for components')
const extsGlob = exts.length === 1 ? exts[0] : `{${exts.join(',')}}`
const globs = toArray(dirs).map(i =>
deep
? `${i}/**/*.${extsGlob}`
: `${i}/*.${extsGlob}`,
)
const files = await fg(globs, {
ignore: [
'node_modules',
],
onlyFiles: true,
})
if (!files.length)
console.warn('[vite-plugin-components] no components found')
const components: ComponentsInfo[] = files.map(f => [path.parse(f).name, `/${f}`])
ctx.components = components
ctx._searchingPromise = undefined
})()
}
return await ctx._searchingPromise
}