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

projects.ts

232 lines8.0 KB

1import {
2 isExplicitRelativePath,
3 isUncPath,
4 isWindowsAbsolutePath,
5 isWindowsDrivePath,
6 normalizeProjectPathForComparison,
7 normalizeProjectPathForDispatch,
8} from "@t3tools/shared/path";
9​
10export { normalizeProjectPathForComparison };
11​
12export const isWindowsPlatform = (platform: string): boolean => {
13 return /^win(dows)?/i.test(platform);
14};
15​
16export const collapsePosixRoot = (value: string, platform: string): string =>
17 isWindowsPlatform(platform) ? value : value.replace(/^\/{2,}/, "/");
18​
19function getAbsolutePathKind(value: string): "unix" | "windows" | null {
20 if (isWindowsDrivePath(value) || isUncPath(value)) {
21 return "windows";
22 }
23 if (value.startsWith("/")) {
24 return "unix";
25 }
26 return null;
28​
29export function preferredPathSeparator(value: string): "/" | "\\" {
30 const absolutePathKind = getAbsolutePathKind(value);
31 if (absolutePathKind === "windows") return "\\";
32 if (absolutePathKind === "unix") return "/";
33 return value.includes("\\") ? "\\" : "/";
35​
36export function hasTrailingPathSeparator(value: string): boolean {
37 return (getAbsolutePathKind(value) === "unix" ? /\/$/ : /[\\/]$/).test(value);
39​
40export { isExplicitRelativePath as isExplicitRelativeProjectPath };
41​
42function splitPathSegments(value: string, separator: "/" | "\\"): string[] {
43 return value.split(separator === "/" ? /\/+/ : /[\\/]+/).filter(Boolean);
45​
46function getLastPathSeparatorIndex(value: string): number {
47 if (getAbsolutePathKind(value) === "unix") {
48 return value.lastIndexOf("/");
49 }
50 return Math.max(value.lastIndexOf("/"), value.lastIndexOf("\\"));
52​
53function splitAbsolutePath(value: string): {
54 root: string;
55 separator: "/" | "\\";
56 segments: string[];
57} | null {
58 if (isWindowsDrivePath(value)) {
59 const root = `${value.slice(0, 2)}\\`;
60 const segments = splitPathSegments(value.slice(root.length), "\\");
61 return { root, separator: "\\", segments };
62 }
63 if (isUncPath(value) || /^\/\/(?!\/)/.test(value)) {
64 const separator = isUncPath(value) ? "\\" : "/";
65 const [server, share, ...segments] = splitPathSegments(value, separator);
66 if (!server || !share) return null;
67 return {
68 root: `${separator}${separator}${server}${separator}${share}${separator}`,
69 separator,
70 segments,
71 };
72 }
73 if (value.startsWith("/")) {
74 return {
75 root: "/",
76 separator: "/",
77 segments: splitPathSegments(value.slice(1), "/"),
78 };
79 }
80 return null;
82​
83export function isFilesystemBrowseQuery(value: string, platform = ""): boolean {
84 const allowWindowsPaths = isWindowsPlatform(platform);
85 return (
86 value.startsWith("./") ||
87 value.startsWith("../") ||
88 value.startsWith(".\\") ||
89 value.startsWith("..\\") ||
90 value.startsWith("/") ||
91 value.startsWith("~/") ||
92 (allowWindowsPaths && isWindowsAbsolutePath(value))
93 );
95​
96export function isUnsupportedWindowsProjectPath(value: string, platform: string): boolean {
97 return isWindowsAbsolutePath(value) && !isWindowsPlatform(platform);
99​
100export function resolveProjectPathForDispatch(
101 value: string,
102 cwd: string | null | undefined,
103 platform: string,
104): string {
105 const trimmedValue = collapsePosixRoot(value.trim(), platform);
106 const relativeTo = isExplicitRelativePath(trimmedValue) ? cwd : null;
107 // The destination server expands `~/`, so it is a root of its own, and `..` cannot leave it here.
108 const base =
109 !relativeTo && trimmedValue.startsWith("~/")
110 ? {
111 root: "~/",
112 separator: "/" as const,
113 segments: splitPathSegments(trimmedValue.slice(2), "/"),
114 }
115 : splitAbsolutePath(normalizeProjectPathForDispatch(relativeTo || trimmedValue));
116 if (!base) {
117 return normalizeProjectPathForDispatch(trimmedValue);
118 }
119 const segments = relativeTo
120 ? [...base.segments, ...splitPathSegments(trimmedValue, base.separator)]
121 : base.segments;
122 if (!relativeTo && !segments.some((segment) => segment === "." || segment === "..")) {
123 return normalizeProjectPathForDispatch(trimmedValue);
124 }
125​
126 const nextSegments: string[] = [];
127 for (const segment of segments) {
128 if (segment === ".") continue;
129 if (segment === "..") {
130 // `~/..` is the home folder's parent, which the server resolves; keep it.
131 const last = nextSegments.at(-1);
132 if (base.root === "~/" && (last === undefined || last === "..")) nextSegments.push(segment);
133 else nextSegments.pop();
134 continue;
135 }
136 nextSegments.push(segment);
137 }
138​
139 return normalizeProjectPathForDispatch(`${base.root}${nextSegments.join(base.separator)}`);
141​
142export function findProjectByPath<T extends { workspaceRoot?: string; cwd?: string }>(
143 projects: ReadonlyArray<T>,
144 candidatePath: string,
145): T | undefined {
146 const normalizedCandidate = normalizeProjectPathForComparison(candidatePath);
147 if (normalizedCandidate.length === 0) {
148 return undefined;
149 }
150 return projects.find((project) => {
151 const cwd = project.workspaceRoot ?? project.cwd;
152 return cwd ? normalizeProjectPathForComparison(cwd) === normalizedCandidate : false;
153 });
155​
156export function isScratchProject(
157 project: { readonly workspaceRoot: string; readonly isScratch?: true },
158 scratchWorkspaceRoot: string | null | undefined,
159): boolean {
160 return (
161 project.isScratch === true ||
162 (scratchWorkspaceRoot != null &&
163 findProjectByPath([project], scratchWorkspaceRoot) !== undefined)
164 );
166​
167export function inferProjectTitleFromPath(value: string): string {
168 const normalized = normalizeProjectPathForDispatch(value);
169 const absolutePath = splitAbsolutePath(normalized);
170 if (absolutePath) {
171 // A `//server/share` root has no segments of its own but is still named after its share.
172 const rootName =
173 absolutePath.separator === "/" ? normalized.split("/").findLast(Boolean) : undefined;
174 return absolutePath.segments.findLast(Boolean) ?? rootName ?? normalized;
175 }
176 const segments = normalized.split(/[/\\]/);
177 return segments.findLast(Boolean) ?? normalized;
179​
180export function appendBrowsePathSegment(currentPath: string, segment: string): string {
181 const separator = preferredPathSeparator(currentPath);
182 return `${getBrowseDirectoryPath(currentPath)}${segment}${separator}`;
184​
185export function getBrowseLeafPathSegment(currentPath: string): string {
186 const lastSeparatorIndex = getLastPathSeparatorIndex(currentPath);
187 return currentPath.slice(lastSeparatorIndex + 1);
189​
190export function getBrowseDirectoryPath(currentPath: string): string {
191 if (hasTrailingPathSeparator(currentPath)) {
192 return currentPath;
193 }
194 const lastSeparatorIndex = getLastPathSeparatorIndex(currentPath);
195 return lastSeparatorIndex < 0 ? currentPath : currentPath.slice(0, lastSeparatorIndex + 1);
197​
198export function ensureBrowseDirectoryPath(currentPath: string): string {
199 const trimmed = currentPath.trim();
200 if (trimmed.length === 0 || hasTrailingPathSeparator(trimmed)) {
201 return trimmed;
202 }
203 return `${trimmed}${preferredPathSeparator(trimmed)}`;
205​
206export function getBrowseParentPath(currentPath: string): string | null {
207 const trimmed = normalizeProjectPathForDispatch(currentPath);
208 const absolutePath = splitAbsolutePath(trimmed);
209 if (absolutePath) {
210 if (absolutePath.segments.length === 0) return null;
211 if (absolutePath.segments.length === 1) return absolutePath.root;
212 const parentSegments = absolutePath.segments.slice(0, -1).join(absolutePath.separator);
213 return `${absolutePath.root}${parentSegments}${absolutePath.separator}`;
214 }
215​
216 const separator = preferredPathSeparator(currentPath);
217 const lastSeparatorIndex = getLastPathSeparatorIndex(trimmed);
218 if (lastSeparatorIndex < 0) return null;
219 if (lastSeparatorIndex === 2 && /^[a-zA-Z]:/.test(trimmed)) {
220 return `${trimmed.slice(0, 2)}${separator}`;
221 }
222 return trimmed.slice(0, lastSeparatorIndex + 1);
224​
225export function canNavigateUp(currentPath: string): boolean {
226 return hasTrailingPathSeparator(currentPath) && getBrowseParentPath(currentPath) !== null;
228​
229export * from "./projectCommands.ts";
230export * from "./projectEntities.ts";
231export * from "./projectRemoval.ts";
232export * from "./projectTrackedFolders.ts";