(pattern: string)
| 15 | * Returns the directory portion and the remaining relative pattern. |
| 16 | */ |
| 17 | export function extractGlobBaseDirectory(pattern: string): { |
| 18 | baseDir: string |
| 19 | relativePattern: string |
| 20 | } { |
| 21 | // Find the first glob special character: *, ?, [, { |
| 22 | const globChars = /[*?[{]/ |
| 23 | const match = pattern.match(globChars) |
| 24 | |
| 25 | if (!match || match.index === undefined) { |
| 26 | // No glob characters - this is a literal path |
| 27 | // Return the directory portion and filename as pattern |
| 28 | const dir = dirname(pattern) |
| 29 | const file = basename(pattern) |
| 30 | return { baseDir: dir, relativePattern: file } |
| 31 | } |
| 32 | |
| 33 | // Get everything before the first glob character |
| 34 | const staticPrefix = pattern.slice(0, match.index) |
| 35 | |
| 36 | // Find the last path separator in the static prefix |
| 37 | const lastSepIndex = Math.max( |
| 38 | staticPrefix.lastIndexOf('/'), |
| 39 | staticPrefix.lastIndexOf(sep), |
| 40 | ) |
| 41 | |
| 42 | if (lastSepIndex === -1) { |
| 43 | // No path separator before the glob - pattern is relative to cwd |
| 44 | return { baseDir: '', relativePattern: pattern } |
| 45 | } |
| 46 | |
| 47 | let baseDir = staticPrefix.slice(0, lastSepIndex) |
| 48 | const relativePattern = pattern.slice(lastSepIndex + 1) |
| 49 | |
| 50 | // Handle root directory patterns (e.g., /*.txt on Unix or C:/*.txt on Windows) |
| 51 | // When lastSepIndex is 0, baseDir is empty but we need to use '/' as the root |
| 52 | if (baseDir === '' && lastSepIndex === 0) { |
| 53 | baseDir = '/' |
| 54 | } |
| 55 | |
| 56 | // Handle Windows drive root paths (e.g., C:/*.txt) |
| 57 | // 'C:' means "current directory on drive C" (relative), not root |
| 58 | // We need 'C:/' or 'C:\' for the actual drive root |
| 59 | if (getPlatform() === 'windows' && /^[A-Za-z]:$/.test(baseDir)) { |
| 60 | baseDir = baseDir + sep |
| 61 | } |
| 62 | |
| 63 | return { baseDir, relativePattern } |
| 64 | } |
| 65 | |
| 66 | export async function glob( |
| 67 | filePattern: string, |
no test coverage detected