mirror of
https://github.com/sst/opencode.git
synced 2025-09-06 13:10:33 +00:00
52 lines
1.4 KiB
TypeScript
52 lines
1.4 KiB
TypeScript
import { exists } from "fs/promises"
|
|
import { dirname, join, relative } from "path"
|
|
|
|
export namespace Filesystem {
|
|
export function overlaps(a: string, b: string) {
|
|
const relA = relative(a, b)
|
|
const relB = relative(b, a)
|
|
return !relA || !relA.startsWith("..") || !relB || !relB.startsWith("..")
|
|
}
|
|
|
|
export function contains(parent: string, child: string) {
|
|
return relative(parent, child).startsWith("..")
|
|
}
|
|
|
|
export async function findUp(target: string, start: string, stop?: string) {
|
|
let current = start
|
|
const result = []
|
|
while (true) {
|
|
const search = join(current, target)
|
|
if (await exists(search)) result.push(search)
|
|
if (stop === current) break
|
|
const parent = dirname(current)
|
|
if (parent === current) break
|
|
current = parent
|
|
}
|
|
return result
|
|
}
|
|
|
|
export async function globUp(pattern: string, start: string, stop?: string) {
|
|
let current = start
|
|
const result = []
|
|
while (true) {
|
|
try {
|
|
const glob = new Bun.Glob(pattern)
|
|
for await (const match of glob.scan({
|
|
cwd: current,
|
|
onlyFiles: true,
|
|
dot: true,
|
|
})) {
|
|
result.push(join(current, match))
|
|
}
|
|
} catch {
|
|
// Skip invalid glob patterns
|
|
}
|
|
if (stop === current) break
|
|
const parent = dirname(current)
|
|
if (parent === current) break
|
|
current = parent
|
|
}
|
|
return result
|
|
}
|
|
}
|