Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/Aquaticat/Monochromatic. TypeScript and Rust monorepo: Claude Code and Pi coding-agent plugins, a m
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142/** Glob resolution for task-depends item resolution. Splits glob patterns into base directories and relative suffixes, then expands them to absolute file paths using `tiny-readdir-glob`. @module */
import { resolve, } from 'node:path';
import readdirGlob from 'tiny-readdir-glob';
//region Glob resolution
/** Glob metacharacters that mark where a static prefix ends. */const GLOB_META_CHARS = '*?{[';
/** Returns the index of the first glob metacharacter in `s`, or `-1` when none are present. Exported for direct equivalence testing of the linear scan. @param s - candidate glob pattern @returns first metacharacter index @example ```ts firstGlobMetaIndex('src/*.ts'); // 4 firstGlobMetaIndex('src/index.ts'); // -1 ``` @internal */export function firstGlobMetaIndex(s: string,): number { // Single linear pass; returns at the first metacharacter, or -1 once the cursor runs past the end. for (let cursorIndex = 0; cursorIndex < s .length; cursorIndex += 1) { if (GLOB_META_CHARS.includes(s.charAt(cursorIndex,),)) return cursorIndex; } return -1;}
/** Splits a glob pattern into a base directory and a relative glob suffix, using {@link firstGlobMetaIndex} to locate the first wildcard. Everything before the first wildcard segment becomes the `cwd`; the remainder becomes the pattern passed to the matcher. @param pattern - Glob pattern, absolute or relative @returns Tuple of `[resolvedCwd, relativeGlob]` @example ```ts splitGlob('/tmp/foo/*.ts') // ['/tmp/foo', '*.ts'] splitGlob('src/**') // ['/abs/path/src', '**'] ``` */function splitGlob(pattern: string,): readonly [ cwd: string, relativeGlob: string,] { /** Position of the first wildcard character; `-1` means the pattern is a literal path. */ const metaIndex = firstGlobMetaIndex(pattern,);
if (metaIndex === (-1)) { return [ resolve(pattern,), '', ]; }
/** Literal portion of the pattern preceding the first wildcard; the matcher's `cwd` is derived from it. */ const staticPrefix = pattern.slice( 0, metaIndex, ); /** Last `/` inside the static prefix; splits the directory `cwd` from the remaining glob suffix. */ const lastSep = staticPrefix.lastIndexOf('/',);
if (lastSep === (-1)) { return [ resolve('.',), pattern, ]; }
return [ resolve(staticPrefix.slice( 0, lastSep, ),), pattern.slice(lastSep + 1,), ];}
/** Resolves a glob pattern into file paths using `tiny-readdir-glob`. @param pattern - Glob pattern to expand @returns Array of matched absolute file paths @example ```ts const files = await resolveGlobFiles('src/*.ts'); ``` */export async function resolveGlobFiles(pattern: string,): Promise<string[]> { /** Base directory and relative glob suffix produced by {@link splitGlob}. */ const [cwd, relativeGlob,] = splitGlob(pattern,);
if (relativeGlob === '') return [cwd,];
/** Files matched by `tiny-readdir-glob`; rebound here so destructuring carries TSDoc above the const block. */ const { files, } = await readdirGlob( relativeGlob, { cwd, }, ); return files;}
//endregion Glob resolution