|
1
|
import * as path from "path" |
|
2
|
|
|
3
|
/** |
|
4
|
* Normalizes a filesystem path to POSIX separators so path matching works |
|
5
|
* the same on Windows and POSIX hosts. |
|
6
|
*/ |
|
7
|
export const toPosix = (p: string): string => p.replace(/\\/g, "/") |
|
8
|
|
|
9
|
export const normalizePathForComparison = (p: string): string => toPosix(p) |
|
10
|
|
|
11
|
/** |
|
12
|
* True when `candidate` is inside (or exactly equals) `root`, after normalizing |
|
13
|
* path separators for host-independent comparisons. |
|
14
|
*/ |
|
15
|
export const isPathInsideOrEqual = ( |
|
16
|
candidate: string, |
|
17
|
root: string |
|
18
|
): boolean => { |
|
19
|
const normalizedCandidate = normalizePathForComparison(candidate).replace( |
|
20
|
/\/+$/, |
|
21
|
"" |
|
22
|
) |
|
23
|
const normalizedRoot = normalizePathForComparison(root).replace(/\/+$/, "") |
|
24
|
|
|
25
|
return ( |
|
26
|
normalizedCandidate === normalizedRoot || |
|
27
|
normalizedCandidate.startsWith(`${normalizedRoot}/`) |
|
28
|
) |
|
29
|
} |
|
30
|
|
|
31
|
/** |
|
32
|
* Returns the basename of `filename` without its extension, or `null` when |
|
33
|
* the filename is unusable as a naming hint — empty, synthetic (`<input>`), |
|
34
|
* or `index.<ext>` (carries no useful signal). |
|
35
|
*/ |
|
36
|
export const getFilenameStem = ( |
|
37
|
filename: string | undefined |
|
38
|
): string | null => { |
|
39
|
if (!filename) { |
|
40
|
return null |
|
41
|
} |
|
42
|
if (filename.startsWith("<")) { |
|
43
|
return null |
|
44
|
} |
|
45
|
const base = path.basename(filename, path.extname(filename)) |
|
46
|
if (!base || base === "index") { |
|
47
|
return null |
|
48
|
} |
|
49
|
return base |
|
50
|
} |
|
51
|
|
|
52
|
/** |
|
53
|
* True when `filename` is an `index.<ext>` file — a barrel / re-export module |
|
54
|
* that aggregates siblings rather than defining anything itself. Returns `false` |
|
55
|
* for empty or synthetic (`<input>`) filenames. |
|
56
|
*/ |
|
57
|
export const isIndexFile = (filename: string | undefined): boolean => { |
|
58
|
if (!filename || filename.startsWith("<")) { |
|
59
|
return false |
|
60
|
} |
|
61
|
return path.basename(filename, path.extname(filename)) === "index" |
|
62
|
} |