|
1
|
import { toPosix } from "./filename" |
|
2
|
|
|
3
|
/** |
|
4
|
* True when `filename` lives under an `api/` directory — either `src/api/` |
|
5
|
* (the canonical Medusa layout) or a bare `api/` (nested project layouts). |
|
6
|
* Bails on synthetic / empty filenames. |
|
7
|
*/ |
|
8
|
export const isUnderApiDir = (filename: string | undefined): boolean => { |
|
9
|
if (!filename || filename.startsWith("<")) { |
|
10
|
return false |
|
11
|
} |
|
12
|
const posix = toPosix(filename) |
|
13
|
return /(^|\/)src\/api\//.test(posix) || /(^|\/)api\//.test(posix) |
|
14
|
} |
|
15
|
|
|
16
|
/** |
|
17
|
* Returns the path segments of a file under `src/api/` (or a bare `api/`), |
|
18
|
* excluding the file's own basename. Returns `null` when the file is not |
|
19
|
* under an api directory. |
|
20
|
* |
|
21
|
* Example: `/proj/src/api/admin/customers/[id]/route.ts` |
|
22
|
* → `["admin", "customers", "[id]"]` |
|
23
|
*/ |
|
24
|
export function getApiRouteSegments(filename: string): string[] | null { |
|
25
|
const posix = toPosix(filename) |
|
26
|
const m = |
|
27
|
posix.match(/(?:^|\/)src\/api\/(.+)$/) ?? posix.match(/(?:^|\/)api\/(.+)$/) |
|
28
|
if (!m) { |
|
29
|
return null |
|
30
|
} |
|
31
|
const segments = m[1].split("/") |
|
32
|
segments.pop() |
|
33
|
return segments |
|
34
|
} |
|
35
|
|
|
36
|
/** |
|
37
|
* Absolute (POSIX) path to the `src/api` (or bare `api`) directory that |
|
38
|
* contains `filename`, or `null` if the file is not under one. |
|
39
|
*/ |
|
40
|
export function findApiRoot(filename: string): string | null { |
|
41
|
const posix = toPosix(filename) |
|
42
|
const m = posix.match(/^(.*\/src\/api)\//) ?? posix.match(/^(.*\/api)\//) |
|
43
|
if (!m) { |
|
44
|
return null |
|
45
|
} |
|
46
|
return m[1] |
|
47
|
} |