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 // Bare C: means the current directory on C, not its root; require the drive separator.
35 return value === "/" || value === "\\" || /^[a-zA-Z]:[/\\]$/.test(value);
36}
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;
49}
50
51export function normalizeProjectPathForDispatch(value: string): string {
52 return trimTrailingPathSeparators(value.trim());
53}
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;
61}
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;
77}
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;
84}
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;
91}
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;
116}
117
118export function isAbsolutePath(value: string): boolean {
119 return value.startsWith("/") || isWindowsAbsolutePath(value);
120}
121
122/** Home-relative host paths are expanded by the destination server. */
123export function isHostPath(value: string): boolean {
124 return isAbsolutePath(value) || value.startsWith("~/");
125}
126
127export function resolveWorkspaceFilePath(path: string, workspaceRoot: string): string {
128 if (isAbsolutePath(path) || path.startsWith("~/") || path.startsWith("~\\")) return path;
129 const base = workspaceRoot.replace(/[/\\]+$/, "");
130 return isWindowsAbsolutePath(workspaceRoot)
131 ? `${base}\\${path.replaceAll("/", "\\")}`
132 : `${base}/${path}`;
133}