MCPcopy Create free account
hub / github.com/Noumena-Network/code / extractGlobBaseDirectory

Function extractGlobBaseDirectory

src/utils/glob.ts:19–66  ·  view source on GitHub ↗
(pattern: string)

Source from the content-addressed store, hash-verified

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

Callers 1

globFunction · 0.85

Calls 2

getPlatformFunction · 0.85
maxMethod · 0.80

Tested by

no test coverage detected