Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

projectPaths.ts

221 lines7.2 KB

1import {
2 isExplicitRelativePath,
3 isUncPath,
4 isWindowsAbsolutePath,
5 isWindowsDrivePath,
6} from "@t3tools/shared/path";
7​
8function isWindowsPlatform(platform: string): boolean {
9 return /^win(dows)?/i.test(platform);
11​
12function isRootPath(value: string): boolean {
13 return value === "/" || value === "\\" || /^[a-zA-Z]:[/\\]?$/.test(value);
15​
16function getAbsolutePathKind(value: string): "unix" | "windows" | null {
17 if (isWindowsDrivePath(value) || isUncPath(value)) {
18 return "windows";
19 }
20 if (value.startsWith("/")) {
21 return "unix";
22 }
23 return null;
25​
26function trimTrailingPathSeparators(value: string): string {
27 if (value.length === 0 || isRootPath(value)) {
28 return value;
29 }
30 const trimmed =
31 getAbsolutePathKind(value) === "unix"
32 ? value.replace(/\/+$/g, "")
33 : value.replace(/[\\/]+$/g, "");
34 if (trimmed.length === 0) {
35 return value;
36 }
37 return /^[a-zA-Z]:$/.test(trimmed) ? `${trimmed}\\` : trimmed;
39​
40function preferredPathSeparator(value: string): "/" | "\\" {
41 const absolutePathKind = getAbsolutePathKind(value);
42 if (absolutePathKind === "windows") return "\\";
43 if (absolutePathKind === "unix") return "/";
44 return value.includes("\\") ? "\\" : "/";
46​
47export function hasTrailingPathSeparator(value: string): boolean {
48 return (getAbsolutePathKind(value) === "unix" ? /\/$/ : /[\\/]$/).test(value);
50​
51export { isExplicitRelativePath as isExplicitRelativeProjectPath };
52​
53function splitPathSegments(value: string, separator: "/" | "\\"): string[] {
54 return value.split(separator === "/" ? /\/+/ : /[\\/]+/).filter(Boolean);
56​
57function getLastPathSeparatorIndex(value: string): number {
58 if (getAbsolutePathKind(value) === "unix") {
59 return value.lastIndexOf("/");
60 }
61 return Math.max(value.lastIndexOf("/"), value.lastIndexOf("\\"));
63​
64function splitAbsolutePath(value: string): {
65 root: string;
66 separator: "/" | "\\";
67 segments: string[];
68} | null {
69 if (isWindowsDrivePath(value)) {
70 const root = `${value.slice(0, 2)}\\`;
71 const segments = splitPathSegments(value.slice(root.length), "\\");
72 return { root, separator: "\\", segments };
73 }
74 if (isUncPath(value)) {
75 const segments = splitPathSegments(value, "\\");
76 const [server, share, ...rest] = segments;
77 if (!server || !share) return null;
78 return {
79 root: `\\\\${server}\\${share}\\`,
80 separator: "\\",
81 segments: rest,
82 };
83 }
84 if (value.startsWith("/")) {
85 return {
86 root: "/",
87 separator: "/",
88 segments: splitPathSegments(value.slice(1), "/"),
89 };
90 }
91 return null;
93​
94export function isFilesystemBrowseQuery(value: string, platform = ""): boolean {
95 const allowWindowsPaths = isWindowsPlatform(platform);
96 return (
97 value.startsWith("./") ||
98 value.startsWith("../") ||
99 value.startsWith(".\\") ||
100 value.startsWith("..\\") ||
101 value.startsWith("/") ||
102 value.startsWith("~/") ||
103 (allowWindowsPaths && isWindowsAbsolutePath(value))
104 );
106​
107export function isUnsupportedWindowsProjectPath(value: string, platform: string): boolean {
108 return isWindowsAbsolutePath(value) && !isWindowsPlatform(platform);
110​
111export function normalizeProjectPathForDispatch(value: string): string {
112 return trimTrailingPathSeparators(value.trim());
114​
115export function resolveProjectPathForDispatch(value: string, cwd?: string | null): string {
116 const trimmedValue = value.trim();
117 if (!isExplicitRelativePath(trimmedValue) || !cwd) {
118 return normalizeProjectPathForDispatch(trimmedValue);
119 }
120​
121 const absoluteBase = splitAbsolutePath(normalizeProjectPathForDispatch(cwd));
122 if (!absoluteBase) {
123 return normalizeProjectPathForDispatch(trimmedValue);
124 }
125​
126 const nextSegments = [...absoluteBase.segments];
127 for (const segment of trimmedValue.split(/[\\/]+/)) {
128 if (segment.length === 0 || segment === ".") continue;
129 if (segment === "..") {
130 nextSegments.pop();
131 continue;
132 }
133 nextSegments.push(segment);
134 }
135​
136 const joinedPath = nextSegments.join(absoluteBase.separator);
137 return normalizeProjectPathForDispatch(
138 joinedPath.length === 0 ? absoluteBase.root : `${absoluteBase.root}${joinedPath}`,
139 );
141​
142export function normalizeProjectPathForComparison(value: string): string {
143 const normalized = normalizeProjectPathForDispatch(value);
144 if (isWindowsDrivePath(normalized) || normalized.startsWith("\\\\")) {
145 return normalized.replaceAll("/", "\\").toLowerCase();
146 }
147 return normalized;
149​
150export function findProjectByPath<T extends { workspaceRoot?: string; cwd?: string }>(
151 projects: ReadonlyArray<T>,
152 candidatePath: string,
153): T | undefined {
154 const normalizedCandidate = normalizeProjectPathForComparison(candidatePath);
155 if (normalizedCandidate.length === 0) {
156 return undefined;
157 }
158 return projects.find((project) => {
159 const cwd = project.workspaceRoot ?? project.cwd;
160 return cwd ? normalizeProjectPathForComparison(cwd) === normalizedCandidate : false;
161 });
163​
164export function inferProjectTitleFromPath(value: string): string {
165 const normalized = normalizeProjectPathForDispatch(value);
166 const absolutePath = splitAbsolutePath(normalized);
167 if (absolutePath) {
168 return absolutePath.segments.findLast(Boolean) ?? normalized;
169 }
170 const segments = normalized.split(/[/\\]/);
171 return segments.findLast(Boolean) ?? normalized;
173​
174export function appendBrowsePathSegment(currentPath: string, segment: string): string {
175 const separator = preferredPathSeparator(currentPath);
176 return `${getBrowseDirectoryPath(currentPath)}${segment}${separator}`;
178​
179export function getBrowseLeafPathSegment(currentPath: string): string {
180 const lastSeparatorIndex = getLastPathSeparatorIndex(currentPath);
181 return currentPath.slice(lastSeparatorIndex + 1);
183​
184export function getBrowseDirectoryPath(currentPath: string): string {
185 if (hasTrailingPathSeparator(currentPath)) {
186 return currentPath;
187 }
188 const lastSeparatorIndex = getLastPathSeparatorIndex(currentPath);
189 return lastSeparatorIndex < 0 ? currentPath : currentPath.slice(0, lastSeparatorIndex + 1);
191​
192export function ensureBrowseDirectoryPath(currentPath: string): string {
193 const trimmed = currentPath.trim();
194 if (trimmed.length === 0 || hasTrailingPathSeparator(trimmed)) {
195 return trimmed;
196 }
197 return `${trimmed}${preferredPathSeparator(trimmed)}`;
199​
200export function getBrowseParentPath(currentPath: string): string | null {
201 const trimmed = trimTrailingPathSeparators(currentPath);
202 const absolutePath = splitAbsolutePath(trimmed);
203 if (absolutePath) {
204 if (absolutePath.segments.length === 0) return null;
205 if (absolutePath.segments.length === 1) return absolutePath.root;
206 const parentSegments = absolutePath.segments.slice(0, -1).join(absolutePath.separator);
207 return `${absolutePath.root}${parentSegments}${absolutePath.separator}`;
208 }
209​
210 const separator = preferredPathSeparator(currentPath);
211 const lastSeparatorIndex = getLastPathSeparatorIndex(trimmed);
212 if (lastSeparatorIndex < 0) return null;
213 if (lastSeparatorIndex === 2 && /^[a-zA-Z]:/.test(trimmed)) {
214 return `${trimmed.slice(0, 2)}${separator}`;
215 }
216 return trimmed.slice(0, lastSeparatorIndex + 1);
218​
219export function canNavigateUp(currentPath: string): boolean {
220 return hasTrailingPathSeparator(currentPath) && getBrowseParentPath(currentPath) !== null;