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

projectPaths.ts

259 lines7.3 KB

1import {
2 isExplicitRelativePath,
3 isUncPath,
4 isWindowsAbsolutePath,
5 isWindowsDrivePath,
6} from "@workbench/shared/path";
7import { isWindowsPlatform } from "./utils";
8​
9function isRootPath(value: string): boolean {
10 return value === "/" || value === "\\" || /^[a-zA-Z]:[/\\]?$/.test(value);
12​
13function getAbsolutePathKind(value: string): "unix" | "windows" | null {
14 if (isWindowsDrivePath(value) || isUncPath(value)) {
15 return "windows";
16 }
17​
18 if (value.startsWith("/")) {
19 return "unix";
20 }
21​
22 return null;
24​
25function trimTrailingPathSeparators(value: string): string {
26 if (value.length === 0 || isRootPath(value)) {
27 return value;
28 }
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​
38 return /^[a-zA-Z]:$/.test(trimmed) ? `${trimmed}\\` : trimmed;
40​
41function preferredPathSeparator(value: string): "/" | "\\" {
42 const absolutePathKind = getAbsolutePathKind(value);
43 if (absolutePathKind === "windows") {
44 return "\\";
45 }
46 if (absolutePathKind === "unix") {
47 return "/";
48 }
49​
50 return value.includes("\\") ? "\\" : "/";
52​
53export function hasTrailingPathSeparator(value: string): boolean {
54 return (getAbsolutePathKind(value) === "unix" ? /\/$/ : /[\\/]$/).test(value);
56​
57export { isExplicitRelativePath as isExplicitRelativeProjectPath };
58​
59function splitPathSegments(value: string, separator: "/" | "\\"): string[] {
60 return value.split(separator === "/" ? /\/+/ : /[\\/]+/).filter(Boolean);
62​
63function getLastPathSeparatorIndex(value: string): number {
64 if (getAbsolutePathKind(value) === "unix") {
65 return value.lastIndexOf("/");
66 }
67​
68 return Math.max(value.lastIndexOf("/"), value.lastIndexOf("\\"));
70​
71function splitAbsolutePath(value: string): {
72 root: string;
73 separator: "/" | "\\";
74 segments: string[];
75} | null {
76 if (isWindowsDrivePath(value)) {
77 const root = `${value.slice(0, 2)}\\`;
78 const segments = splitPathSegments(value.slice(root.length), "\\");
79 return { root, separator: "\\", segments };
80 }
81 if (isUncPath(value)) {
82 const segments = splitPathSegments(value, "\\");
83 const [server, share, ...rest] = segments;
84 if (!server || !share) {
85 return null;
86 }
87 return {
88 root: `\\\\${server}\\${share}\\`,
89 separator: "\\",
90 segments: rest,
91 };
92 }
93 if (value.startsWith("/")) {
94 return {
95 root: "/",
96 separator: "/",
97 segments: splitPathSegments(value.slice(1), "/"),
98 };
99 }
100 return null;
102​
103export function isFilesystemBrowseQuery(
104 value: string,
105 platform = typeof navigator === "undefined" ? "" : navigator.platform,
106): boolean {
107 const allowWindowsPaths = isWindowsPlatform(platform);
108 return (
109 value.startsWith("./") ||
110 value.startsWith("../") ||
111 value.startsWith(".\\") ||
112 value.startsWith("..\\") ||
113 value.startsWith("/") ||
114 value.startsWith("~/") ||
115 (allowWindowsPaths && isWindowsAbsolutePath(value))
116 );
118​
119export function isUnsupportedWindowsProjectPath(value: string, platform: string): boolean {
120 return isWindowsAbsolutePath(value) && !isWindowsPlatform(platform);
122​
123export function normalizeProjectPathForDispatch(value: string): string {
124 return trimTrailingPathSeparators(value.trim());
126​
127export function resolveProjectPathForDispatch(value: string, cwd?: string | null): string {
128 const trimmedValue = value.trim();
129 if (!isExplicitRelativePath(trimmedValue) || !cwd) {
130 return normalizeProjectPathForDispatch(trimmedValue);
131 }
132​
133 const absoluteBase = splitAbsolutePath(normalizeProjectPathForDispatch(cwd));
134 if (!absoluteBase) {
135 return normalizeProjectPathForDispatch(trimmedValue);
136 }
137​
138 const nextSegments = [...absoluteBase.segments];
139 for (const segment of trimmedValue.split(/[\\/]+/)) {
140 if (segment.length === 0 || segment === ".") {
141 continue;
142 }
143 if (segment === "..") {
144 nextSegments.pop();
145 continue;
146 }
147 nextSegments.push(segment);
148 }
149​
150 const joinedPath = nextSegments.join(absoluteBase.separator);
151 if (joinedPath.length === 0) {
152 return normalizeProjectPathForDispatch(absoluteBase.root);
153 }
154​
155 return normalizeProjectPathForDispatch(`${absoluteBase.root}${joinedPath}`);
157​
158export function normalizeProjectPathForComparison(value: string): string {
159 const normalized = normalizeProjectPathForDispatch(value);
160 if (isWindowsDrivePath(normalized) || normalized.startsWith("\\\\")) {
161 return normalized.replaceAll("/", "\\").toLowerCase();
162 }
163 return normalized;
165​
166export function findProjectByPath<T extends { cwd: string }>(
167 projects: ReadonlyArray<T>,
168 candidatePath: string,
169): T | undefined {
170 const normalizedCandidate = normalizeProjectPathForComparison(candidatePath);
171 if (normalizedCandidate.length === 0) {
172 return undefined;
173 }
174​
175 return projects.find(
176 (project) => normalizeProjectPathForComparison(project.cwd) === normalizedCandidate,
177 );
179​
180export function inferProjectTitleFromPath(value: string): string {
181 const normalized = normalizeProjectPathForDispatch(value);
182 const absolutePath = splitAbsolutePath(normalized);
183 if (absolutePath) {
184 return absolutePath.segments.findLast(Boolean) ?? normalized;
185 }
186​
187 const segments = normalized.split(/[/\\]/);
188 return segments.findLast(Boolean) ?? normalized;
190​
191export function appendBrowsePathSegment(currentPath: string, segment: string): string {
192 const separator = preferredPathSeparator(currentPath);
193 return `${getBrowseDirectoryPath(currentPath)}${segment}${separator}`;
195​
196export function getBrowseLeafPathSegment(currentPath: string): string {
197 const lastSeparatorIndex = getLastPathSeparatorIndex(currentPath);
198 return currentPath.slice(lastSeparatorIndex + 1);
200​
201export function getBrowseDirectoryPath(currentPath: string): string {
202 if (hasTrailingPathSeparator(currentPath)) {
203 return currentPath;
204 }
205​
206 const lastSeparatorIndex = getLastPathSeparatorIndex(currentPath);
207 if (lastSeparatorIndex < 0) {
208 return currentPath;
209 }
210​
211 return currentPath.slice(0, lastSeparatorIndex + 1);
213​
214export function ensureBrowseDirectoryPath(currentPath: string): string {
215 const trimmed = currentPath.trim();
216 if (trimmed.length === 0) {
217 return trimmed;
218 }
219​
220 if (hasTrailingPathSeparator(trimmed)) {
221 return trimmed;
222 }
223​
224 return `${trimmed}${preferredPathSeparator(trimmed)}`;
226​
227export function getBrowseParentPath(currentPath: string): string | null {
228 const trimmed = trimTrailingPathSeparators(currentPath);
229 const absolutePath = splitAbsolutePath(trimmed);
230 if (absolutePath) {
231 if (absolutePath.segments.length === 0) {
232 return null;
233 }
234​
235 if (absolutePath.segments.length === 1) {
236 return absolutePath.root;
237 }
238​
239 const parentSegments = absolutePath.segments.slice(0, -1).join(absolutePath.separator);
240 return `${absolutePath.root}${parentSegments}${absolutePath.separator}`;
241 }
242​
243 const separator = preferredPathSeparator(currentPath);
244 const lastSeparatorIndex = getLastPathSeparatorIndex(trimmed);
245​
246 if (lastSeparatorIndex < 0) {
247 return null;
248 }
249​
250 if (lastSeparatorIndex === 2 && /^[a-zA-Z]:/.test(trimmed)) {
251 return `${trimmed.slice(0, 2)}${separator}`;
252 }
253​
254 return trimmed.slice(0, lastSeparatorIndex + 1);
256​
257export function canNavigateUp(currentPath: string): boolean {
258 return hasTrailingPathSeparator(currentPath) && getBrowseParentPath(currentPath) !== null;