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

path.ts

162 lines6.1 KB

1export function isWindowsDrivePath(value: string): boolean {
2 return /^[a-zA-Z]:([/\\]|$)/.test(value);
3}
4​
5export function isUncPath(value: string): boolean {
6 return value.startsWith("\\\\");
7}
8​
9/**
10 * Windows treats `/` and `\` alike, so any two leading separators name a UNC
11 * or device path. Touching one makes Windows sign in to that host with the
12 * user's NTLM credentials, so agent-authored paths must not reach one.
13 */
14export function isNetworkPath(value: string): boolean {
15 return /^[\\/]{2}/.test(value);
17​
18export function isWindowsAbsolutePath(value: string): boolean {
19 return isUncPath(value) || isWindowsDrivePath(value);
21​
22export function isExplicitRelativePath(value: string): boolean {
23 return (
24 value === "." ||
25 value === ".." ||
26 value.startsWith("./") ||
27 value.startsWith("../") ||
28 value.startsWith(".\\") ||
29 value.startsWith("..\\")
30 );
32​
33function isRootPath(value: string): boolean {
34 // Bare C: means the current directory on C, not its root; require the drive separator.
35 return value === "/" || value === "\\" || /^[a-zA-Z]:[/\\]$/.test(value);
37​
38function trimTrailingPathSeparators(value: string): string {
39 if (value.length === 0 || isRootPath(value)) {
40 return value;
41 }
42 const trimmed = value.startsWith("/")
43 ? value.replace(/\/+$/g, "")
44 : value.replace(/[\\/]+$/g, "");
45 if (trimmed.length === 0) {
46 return value;
47 }
48 return /^[a-zA-Z]:$/.test(trimmed) ? `${trimmed}\\` : trimmed;
50​
51export function normalizeProjectPathForDispatch(value: string): string {
52 return trimTrailingPathSeparators(value.trim());
54​
55export function normalizeProjectPathForComparison(value: string): string {
56 const normalized = normalizeProjectPathForDispatch(value);
57 if (isWindowsDrivePath(normalized) || isUncPath(normalized)) {
58 return normalized.replaceAll("/", "\\").toLowerCase();
59 }
60 return normalized;
62​
63// Windows refuses these as file names, with or without an extension.
64const WINDOWS_RESERVED_NAME = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/;
65​
66export function newProjectFolderName(name: string): string {
67 const slug = name
68 .normalize("NFKD")
69 .replace(/[̀-ͯ]/g, "")
70 .toLowerCase()
71 .replace(/[^a-z0-9]+/g, "-")
72 .replace(/^-+/, "")
73 .slice(0, 64)
74 .replace(/-+$/, "");
75 if (slug.length === 0) return "project";
76 return WINDOWS_RESERVED_NAME.test(slug) ? `${slug}-project` : slug;
78​
79const SLASH_PREFIXED_WINDOWS_DRIVE_PATTERN = /^\/[A-Za-z]:[\\/]/;
80​
81/** Browser URL parsers write `C:/foo` as `/C:/foo` for file URLs. */
82export function stripSlashPrefixedWindowsDrive(path: string): string {
83 return SLASH_PREFIXED_WINDOWS_DRIVE_PATTERN.test(path) ? path.slice(1) : path;
85​
86export function fileBasename(path: string): string {
87 const trimmed = path.replace(/[/\\]+$/, "");
88 if (trimmed.length === 0) return path;
89 const separatorIndex = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
90 return separatorIndex >= 0 ? trimmed.slice(separatorIndex + 1) : trimmed;
92​
93export function workspaceRelativeFilePath(
94 path: string,
95 workspaceRoot: string | null | undefined,
96): string | null {
97 if (!workspaceRoot) return null;
98 const normalizedPath = stripSlashPrefixedWindowsDrive(path.replaceAll("\\", "/"));
99 const normalizedRoot = stripSlashPrefixedWindowsDrive(
100 workspaceRoot.replaceAll("\\", "/"),
101 ).replace(/\/+$/, "");
102 const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
103 const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
104 const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
105 if (pathForCompare.replace(/\/+$/, "") === rootForCompare) return ".";
106 if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null;
107 const relativePath = normalizedPath.slice(normalizedRoot.length + 1);
108 // `/repo/../x` starts with the root but escapes it; the server rejects that as a workspace path.
109 let depth = 0;
110 for (const segment of relativePath.split("/")) {
111 if (segment === "..") depth -= 1;
112 else if (segment.length > 0 && segment !== ".") depth += 1;
113 if (depth < 0) return null;
114 }
115 return relativePath;
117​
118export function isAbsolutePath(value: string): boolean {
119 return value.startsWith("/") || isWindowsAbsolutePath(value);
121​
122/** Home-relative host paths are expanded by the destination server. */
123export function isHostPath(value: string): boolean {
124 return isAbsolutePath(value) || value.startsWith("~/");
126​
127// Finder and Explorer copy a path ready for a shell: `'/a b'`, `/a\'s\ b`, or `"C:\a b"`.
128const QUOTED_PATH_AT_END = /(?:'((?:[^']|'\\'')+)'|"([^"]+)")\s*$/;
129const SHELL_ESCAPE = /\\([\s'"\\()[\]{}<>|&;$`!*?#~^=])/g;
130​
131/**
132 * What a folder box holds after a change. A full path pasted into it replaces the box, even after
133 * the text already there, because `~/'/a b'` would create that nested folder.
134 */
135export function resolvePastedPath(previous: string, next: string): string {
136 const isTyping = next.startsWith(previous)
137 ? next.length === previous.length + 1
138 : previous.startsWith(next);
139 if (isTyping) return next;
140​
141 const match = QUOTED_PATH_AT_END.exec(next);
142 // Text before the quote that wasn't in the box is a search, not a folder to paste after.
143 if (match && previous.startsWith(next.slice(0, match.index).trimEnd())) {
144 const quoted = match[1]?.replaceAll("'\\''", "'") ?? match[2] ?? "";
145 if (isHostPath(quoted)) return quoted;
146 }
147​
148 // Without a trailing separator, `/a` then `/a/b` is a longer path, not one pasted after a folder.
149 const afterFolder = /[/\\]$/.test(previous) && next.startsWith(previous);
150 const path = afterFolder ? next.slice(previous.length) : next;
151 if (!isHostPath(path)) return next;
152 // `\` separates folders in Windows paths, so it only escapes in `/` and `~/` paths.
153 return isWindowsAbsolutePath(path) ? path : path.replace(SHELL_ESCAPE, "$1");
155​
156export function resolveWorkspaceFilePath(path: string, workspaceRoot: string): string {
157 if (isAbsolutePath(path) || path.startsWith("~/") || path.startsWith("~\\")) return path;
158 const base = workspaceRoot.replace(/[/\\]+$/, "");
159 return isWindowsAbsolutePath(workspaceRoot)
160 ? `${base}\\${path.replaceAll("/", "\\")}`
161 : `${base}/${path}`;