1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/**
* Path helpers. A hooks module has no `path` module: these are the few
* string operations the hooks need, all over forward-slash paths.
*/
/** Forward slashes, no doubled separators, no trailing slash (root stays `/`). */
export function norm(path: string): string {
const clean = path.replace(/\\/g, '/').replace(/\/{2,}/g, '/')
return clean.length > 1 && clean.endsWith('/') ? clean.slice(0, -1) : clean
}
/** Resolves `.` and `..` segments lexically; absolute stays absolute. */
export function resolveDots(path: string): string {
const isAbsolute = path.startsWith('/')
const out: string[] = []
for (const part of norm(path).split('/')) {
if (part === '' || part === '.') {
continue
}
if (part === '..') {
if (out.length > 0 && out.at(-1) !== '..') {
out.pop()
} else if (!isAbsolute) {
out.push('..')
}
continue
}
out.push(part)
}
return (isAbsolute ? '/' : '') + out.join('/')
}
/** `path` made absolute against `cwd` when it is relative. */
export function absOf(cwd: string, path: string): string {
return resolveDots(path.startsWith('/') ? path : `${norm(cwd)}/${path}`)
}
/**
* `path` relative to `cwd` when it lies inside it, else null. Both may be
* given with or without the macOS `/private` prefix the engine sometimes adds.
*/
export function relTo(cwd: string, path: string): string | null {
const strip = (p: string) => p.replace(/^\/private(?=\/(?:tmp|var)\/)/, '')
const base = strip(norm(cwd))
const full = strip(absOf(cwd, path))
if (full === base) {
return ''
}
return full.startsWith(`${base}/`) ? full.slice(base.length + 1) : null
}
/** True when relative path `rel` is `dir` or lies beneath it. */
export function isUnder(rel: string, dir: string): boolean {
const d = norm(dir)
return rel === d || rel.startsWith(`${d}/`)
}
/** The last segment. */
export function baseName(path: string): string {
const parts = norm(path).split('/')
return parts.at(-1) ?? ''
}
/** The last segment without its final extension. */
export function stemOf(path: string): string {
const base = baseName(path)
const dot = base.lastIndexOf('.')
return dot > 0 ? base.slice(0, dot) : base
}
/** Everything but the last segment (`''` for a bare name). */
export function dirName(path: string): string {
const parts = norm(path).split('/')
parts.pop()
return parts.join('/')
}
/** Joins segments with single slashes. */
export function join(...parts: string[]): string {
return norm(parts.filter(part => part !== '').join('/'))
}
|