Open History to see the last commit
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);
16}
17
18export function isWindowsAbsolutePath(value: string): boolean {
19 return isUncPath(value) || isWindowsDrivePath(value);
20}
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 );
31}
32
33function isRootPath(value: string): boolean {
34 // The drive separator is required: a bare `C:` is not the drive root (it
35 // means "current directory on C:"), and treating it as already-canonical
36 // would leave it as `C:` while `C:\` and `C:/` normalize to the drive root,
37 // so the same location would fail project identity/dedup comparisons.
38 return value === "/" || value === "\\" || /^[a-zA-Z]:[/\\]$/.test(value);
39}
40
41function trimTrailingPathSeparators(value: string): string {
42 if (value.length === 0 || isRootPath(value)) {
43 return value;
44 }
45 const trimmed = value.startsWith("/")
46 ? value.replace(/\/+$/g, "")
47 : value.replace(/[\\/]+$/g, "");
48 if (trimmed.length === 0) {
49 return value;
50 }
51 return /^[a-zA-Z]:$/.test(trimmed) ? `${trimmed}\\` : trimmed;
52}
53
54export function normalizeProjectPathForDispatch(value: string): string {
55 return trimTrailingPathSeparators(value.trim());
56}
57
58export function normalizeProjectPathForComparison(value: string): string {
59 const normalized = normalizeProjectPathForDispatch(value);
60 if (isWindowsDrivePath(normalized) || isUncPath(normalized)) {
61 return normalized.replaceAll("/", "\\").toLowerCase();
62 }
63 return normalized;
64}
65
66// Windows refuses these as file names, with or without an extension.
67const WINDOWS_RESERVED_NAME = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/;
68
69export function newProjectFolderName(name: string): string {
70 const slug = name
71 .normalize("NFKD")
72 .replace(/[̀-ͯ]/g, "")
73 .toLowerCase()
74 .replace(/[^a-z0-9]+/g, "-")
75 .replace(/^-+/, "")
76 .slice(0, 64)
77 .replace(/-+$/, "");
78 if (slug.length === 0) return "project";
79 return WINDOWS_RESERVED_NAME.test(slug) ? `${slug}-project` : slug;
80}
81
82const SLASH_PREFIXED_WINDOWS_DRIVE_PATTERN = /^\/[A-Za-z]:[\\/]/;
83
84/** Browser URL parsers write `C:/foo` as `/C:/foo` for file URLs. */
85export function stripSlashPrefixedWindowsDrive(path: string): string {
86 return SLASH_PREFIXED_WINDOWS_DRIVE_PATTERN.test(path) ? path.slice(1) : path;
87}
88
89export function fileBasename(path: string): string {
90 const trimmed = path.replace(/[/\\]+$/, "");
91 if (trimmed.length === 0) return path;
92 const separatorIndex = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
93 return separatorIndex >= 0 ? trimmed.slice(separatorIndex + 1) : trimmed;
94}
95
96export function workspaceRelativeFilePath(
97 path: string,
98 workspaceRoot: string | null | undefined,
99): string | null {
100 if (!workspaceRoot) return null;
101 const normalizedPath = stripSlashPrefixedWindowsDrive(path.replaceAll("\\", "/"));
102 const normalizedRoot = stripSlashPrefixedWindowsDrive(
103 workspaceRoot.replaceAll("\\", "/"),
104 ).replace(/\/+$/, "");
105 const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
106 const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
107 const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
108 if (pathForCompare.replace(/\/+$/, "") === rootForCompare) return ".";
109 if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null;
110 const relativePath = normalizedPath.slice(normalizedRoot.length + 1);
111 // `/repo/../x` starts with the root but escapes it; the server rejects that as a workspace path.
112 let depth = 0;
113 for (const segment of relativePath.split("/")) {
114 if (segment === "..") depth -= 1;
115 else if (segment.length > 0 && segment !== ".") depth += 1;
116 if (depth < 0) return null;
117 }
118 return relativePath;
119}
120
121export function isAbsolutePath(value: string): boolean {
122 return value.startsWith("/") || isWindowsAbsolutePath(value);
123}
124
125/** Home-relative host paths are expanded by the destination server. */
126export function isHostPath(value: string): boolean {
127 return isAbsolutePath(value) || value.startsWith("~/");
128}
129
130export function resolveWorkspaceFilePath(path: string, workspaceRoot: string): string {
131 if (isAbsolutePath(path) || path.startsWith("~/") || path.startsWith("~\\")) return path;
132 const base = workspaceRoot.replace(/[/\\]+$/, "");
133 return isWindowsAbsolutePath(workspaceRoot)
134 ? `${base}\\${path.replaceAll("/", "\\")}`
135 : `${base}/${path}`;
136}