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
9export function isWindowsAbsolutePath(value: string): boolean {
10 return isUncPath(value) || isWindowsDrivePath(value);
11}
12
13export function isExplicitRelativePath(value: string): boolean {
14 return (
15 value === "." ||
16 value === ".." ||
17 value.startsWith("./") ||
18 value.startsWith("../") ||
19 value.startsWith(".\\") ||
20 value.startsWith("..\\")
21 );
22}
23
24function isRootPath(value: string): boolean {
25 // The drive separator is required: a bare `C:` is not the drive root (it
26 // means "current directory on C:"), and treating it as already-canonical
27 // would leave it as `C:` while `C:\` and `C:/` normalize to the drive root,
28 // so the same location would fail project identity/dedup comparisons.
29 return value === "/" || value === "\\" || /^[a-zA-Z]:[/\\]$/.test(value);
30}
31
32function trimTrailingPathSeparators(value: string): string {
33 if (value.length === 0 || isRootPath(value)) {
34 return value;
35 }
36 const trimmed = value.startsWith("/")
37 ? value.replace(/\/+$/g, "")
38 : value.replace(/[\\/]+$/g, "");
39 if (trimmed.length === 0) {
40 return value;
41 }
42 return /^[a-zA-Z]:$/.test(trimmed) ? `${trimmed}\\` : trimmed;
43}
44
45export function normalizeProjectPathForDispatch(value: string): string {
46 return trimTrailingPathSeparators(value.trim());
47}
48
49export function normalizeProjectPathForComparison(value: string): string {
50 const normalized = normalizeProjectPathForDispatch(value);
51 if (isWindowsDrivePath(normalized) || isUncPath(normalized)) {
52 return normalized.replaceAll("/", "\\").toLowerCase();
53 }
54 return normalized;
55}
56
57// Windows refuses these as file names, with or without an extension.
58const WINDOWS_RESERVED_NAME = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/;
59
60export function newProjectFolderName(name: string): string {
61 const slug = name
62 .normalize("NFKD")
63 .replace(/[̀-ͯ]/g, "")
64 .toLowerCase()
65 .replace(/[^a-z0-9]+/g, "-")
66 .replace(/^-+/, "")
67 .slice(0, 64)
68 .replace(/-+$/, "");
69 if (slug.length === 0) return "project";
70 return WINDOWS_RESERVED_NAME.test(slug) ? `${slug}-project` : slug;
71}
72
73const SLASH_PREFIXED_WINDOWS_DRIVE_PATTERN = /^\/[A-Za-z]:[\\/]/;
74
75/** Browser URL parsers write `C:/foo` as `/C:/foo` for file URLs. */
76export function stripSlashPrefixedWindowsDrive(path: string): string {
77 return SLASH_PREFIXED_WINDOWS_DRIVE_PATTERN.test(path) ? path.slice(1) : path;
78}
79
80export function fileBasename(path: string): string {
81 const trimmed = path.replace(/[/\\]+$/, "");
82 if (trimmed.length === 0) return path;
83 const separatorIndex = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"));
84 return separatorIndex >= 0 ? trimmed.slice(separatorIndex + 1) : trimmed;
85}
86
87export function workspaceRelativeFilePath(
88 path: string,
89 workspaceRoot: string | null | undefined,
90): string | null {
91 if (!workspaceRoot) return null;
92 const normalizedPath = stripSlashPrefixedWindowsDrive(path.replaceAll("\\", "/"));
93 const normalizedRoot = stripSlashPrefixedWindowsDrive(
94 workspaceRoot.replaceAll("\\", "/"),
95 ).replace(/\/+$/, "");
96 const caseInsensitive = isWindowsAbsolutePath(stripSlashPrefixedWindowsDrive(workspaceRoot));
97 const pathForCompare = caseInsensitive ? normalizedPath.toLowerCase() : normalizedPath;
98 const rootForCompare = caseInsensitive ? normalizedRoot.toLowerCase() : normalizedRoot;
99 if (pathForCompare.replace(/\/+$/, "") === rootForCompare) return ".";
100 if (!pathForCompare.startsWith(`${rootForCompare}/`)) return null;
101 return normalizedPath.slice(normalizedRoot.length + 1);
102}
103
104export function isAbsolutePath(value: string): boolean {
105 return value.startsWith("/") || isWindowsAbsolutePath(value);
106}
107
108export function resolveWorkspaceFilePath(path: string, workspaceRoot: string): string {
109 if (isAbsolutePath(path) || path.startsWith("~/") || path.startsWith("~\\")) return path;
110 const base = workspaceRoot.replace(/[/\\]+$/, "");
111 return isWindowsAbsolutePath(workspaceRoot)
112 ? `${base}\\${path.replaceAll("/", "\\")}`
113 : `${base}/${path}`;
114}