|
1 | 1 | import { glob as tinyGlob, globSync as tinyGlobSync } from "tinyglobby"; |
2 | 2 |
|
3 | | -import type { GlobOptions } from "tinyglobby"; |
| 3 | +import { isArray, isString, omit } from "@/remeda"; |
4 | 4 |
|
5 | | -export const glob = ( |
6 | | - patterns: string | readonly string[], |
| 5 | +import type { GlobOptions as TinyGlobOptions } from "tinyglobby"; |
| 6 | + |
| 7 | +export type { FileSystemAdapter } from "tinyglobby"; |
| 8 | + |
| 9 | +export interface GlobOptions extends Omit<TinyGlobOptions, "patterns"> { |
| 10 | + /** |
| 11 | + * Provide patterns as the first argument instead. |
| 12 | + */ |
| 13 | + patterns: string | string[]; |
| 14 | +} |
| 15 | + |
| 16 | +export { convertPathToPattern, escapePath, isDynamicPattern } from "tinyglobby"; |
| 17 | + |
| 18 | +const normalizeArgs = ( |
| 19 | + patternsOrOptions: string | string[] | GlobOptions, |
| 20 | + maybeOptions?: Omit<GlobOptions, "patterns">, |
| 21 | +) => { |
| 22 | + const patternsAsArgument = isString(patternsOrOptions) || isArray(patternsOrOptions); |
| 23 | + |
| 24 | + const patterns = patternsAsArgument ? patternsOrOptions : patternsOrOptions.patterns; |
| 25 | + const options = patternsAsArgument ? maybeOptions || {} : omit(patternsOrOptions, ["patterns"]); |
| 26 | + |
| 27 | + return { patterns, options }; |
| 28 | +}; |
| 29 | + |
| 30 | +export function glob( |
| 31 | + patterns: string | string[], |
7 | 32 | options?: Omit<GlobOptions, "patterns">, |
8 | | -): Promise<string[]> => |
9 | | - tinyGlob(patterns, { |
| 33 | +): Promise<string[]>; |
| 34 | +export function glob(options: GlobOptions): Promise<string[]>; |
| 35 | +export function glob(...args: [any, any?]): Promise<string[]> { |
| 36 | + const { patterns, options } = normalizeArgs(...args); |
| 37 | + |
| 38 | + return tinyGlob(patterns, { |
10 | 39 | expandDirectories: false, |
11 | 40 | ...options, |
12 | 41 | }); |
| 42 | +} |
| 43 | + |
| 44 | +export function globSync( |
| 45 | + patterns: string | string[], |
| 46 | + options?: Omit<GlobOptions, "patterns">, |
| 47 | +): string[]; |
| 48 | +export function globSync(options: GlobOptions): string[]; |
| 49 | +export function globSync(...args: [any, any?]): string[] { |
| 50 | + const { patterns, options } = normalizeArgs(...args); |
13 | 51 |
|
14 | | -export const globSync = ( |
15 | | - patterns: string | readonly string[], |
16 | | - options: Omit<GlobOptions, "patterns"> = {}, |
17 | | -): string[] => |
18 | | - tinyGlobSync(patterns, { |
| 52 | + return tinyGlobSync(patterns, { |
19 | 53 | expandDirectories: false, |
20 | 54 | ...options, |
21 | 55 | }); |
22 | | - |
23 | | -export { convertPathToPattern, escapePath, isDynamicPattern } from "tinyglobby"; |
24 | | - |
25 | | -export type { FileSystemAdapter, GlobOptions } from "tinyglobby"; |
| 56 | +} |
0 commit comments